標準沙箱提供預先定義的程式庫,可為執行程式碼和電腦使用等特定工作提供現成解決方案。對於需要特定依附元件、程式庫、自訂工具或專用資料平面介面的代理工作負載,您可以提供自己的容器映像檔 (也稱為自備容器或 BYOC)。這項功能可讓您在沙箱環境的安全範圍內執行自訂工作負載。
事前準備
設定專案、映像檔存放區和權限。
設定容器映像檔
建構容器映像檔:建立包含自訂環境的 Docker 映像檔。為確保與 Gemini Enterprise Agent Platform 沙箱相容,自訂映像檔必須符合下列規定:
- 作業系統:以 Linux 為基礎 (例如 Debian 或 Ubuntu)。
- 執行器:必須包含相容的執行器或進入點,供 Gemini Enterprise Agent Platform 執行指令。
- 安全性:不得要求根權限或存取受限的系統資源,因為映像檔會在安全隔離的沙箱中執行。
將映像檔新增至 Artifact Registry:將映像檔新增至 Cloud de Confiance by S3NSArtifact Registry,Agent Platform 即可存取。佈建沙箱時,請提供 Artifact Registry 映像檔 URI。
設定權限
如要允許 Agent Sandbox 從 Artifact Registry 提取自訂容器映像檔,請將包含該映像檔的存放區的 Artifact Registry 讀取者 (roles/artifactregistry.reader) 角色授予 Agent Sandbox 服務代理:
- Agent Sandbox 服務代理程式:
service-PROJECT_NUMBER@gcp-sa-vertex-sandbox.s3ns-system.iam.gserviceaccount.com
將 PROJECT_NUMBER 替換為專案的專案編號。
如要進一步瞭解如何授予角色,請參閱「授予服務代理程式權限」。
建立 Agent Platform 執行個體
如果沒有 Agent Platform 執行個體,請建立一個。
import agentplatform
client = agentplatform.Client(
project='PROJECT_ID',
location='LOCATION',
http_options={
"api_version": "v1beta1",
}
)
remote_agent = client.runtimes.create()
remote_agent_name = remote_agent.api_resource.name
更改下列內容:
PROJECT_ID: Cloud de Confiance 專案 ID。LOCATION:Agent Platform 執行個體的 Cloud de Confiance 區域。請參閱「支援的區域」。
建立自訂容器沙箱
如要建立自訂容器沙箱,請先建立沙箱範本 (SandboxEnvironmentTemplate)。建立範本後,Agent Platform 會建立與範本相關聯的預先暖機集區,以提供更快、更可靠的啟動時間。
如要建立範本,請定義 SandboxEnvironmentTemplate 資源,指定自訂容器的設定:
- 容器映像檔:Artifact Registry 中代管的預先建構自訂映像檔 URI。
- 通訊埠:容器要對外公開的網路通訊埠。
建立範本
# Create a custom sandbox template
templates_client = client.sandboxes.templates
operation = templates_client.create(
name=remote_agent_name,
display_name="DISPLAY_NAME",
config={
"custom_container_environment": {
"custom_container_spec": {
"image_uri": "IMAGE_LOCATION"
},
"resources": {
"requests": {
"cpu": "1",
"memory": "500Mi"
},
"limits": {
"cpu": "1",
"memory": "500Mi"
}
},
"ports": [
{
"port": PORT_NUMBER,
"protocol": "TCP"
}
]
},
"egress_control_config": {
"internet_access": True
}
}
)
template_name = operation.response.name
print(f"Template created: {template_name}")
建立沙箱
定義範本後,您就可以參照範本資源名稱,佈建新的沙箱環境。
# Provision a sandbox referencing the template
create_operation = client.sandboxes.create(
name=remote_agent_name,
config={
"sandbox_environment_template": template_name,
"display_name": "DISPLAY_NAME"
}
)
sandbox = create_operation.response
print(f"Sandbox environment provisioned: {sandbox.name}")