教學:將無伺服器沙盒連接至虛擬網路(預覽)

您可以將 Azure 容器執行個體 (ACI) 中無伺服器沙盒的 AI 代理程式群組 連線到 Azure 虛擬網路中的子網路。 群組中的每個沙盒都會從子網路取得私有 IP 位址,因此各個沙盒都能存取虛擬網路中的私用資源,例如內部服務、資料庫和私有端點。

在本教學中,您將部署僅有私人 IP 位址的 Web 應用程式。 接著,您會在相同的虛擬網路中建立 AI 代理程式群組,並從沙盒存取該 Web 應用程式。

在這個教學中,你會學到如何:

  • 建立虛擬網路,並設定用於沙箱的委派子網路
  • 在虛擬網路中部署私有 Web 應用程式
  • 建立一個已連線至子網路的 AI 代理程式群組
  • 從沙箱存取私人 Web 應用程式

Prerequisites

  • Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。

  • Azure CLI(2.60 或更新版本),已使用 az login 登入,或其他可將經過驗證的 REST 請求傳送至 Azure Resource Manager 的工具。

  • 在您的訂用帳戶中註冊的 Microsoft.ContainerInstance 資源提供者。

    az provider register --namespace Microsoft.ContainerInstance --wait
    
  • 將 Contributor、Azure Deployment Stack Owner 及 Storage Blob Data Contributor 角色指派給您訂閱中 Compute Sandbox Groups 服務主體,讓該服務能在您的訂閱中佈建受控資源群組和快照儲存體帳戶。 要指派角色,您需要具備在訂閱中建立角色指派的權限,例如 擁有者 或 使用者存取管理員 角色:

    subscriptionId=$(az account show --query id --output tsv)
    spObjectId=$(az ad sp show --id 1eeacc2b-ec4a-4b1e-977a-2b5fef2131c6 --query id --output tsv 2>/dev/null || \
      az ad sp create --id 1eeacc2b-ec4a-4b1e-977a-2b5fef2131c6 --query id --output tsv)
    
    for role in "Contributor" "Azure Deployment Stack Owner" "Storage Blob Data Contributor"; do
      az role assignment create --assignee-object-id $spObjectId --assignee-principal-type ServicePrincipal \
        --role "$role" --scope /subscriptions/$subscriptionId
    done
    

    角色指派可能需要最多 15 分鐘才能生效。

  • 您訂用帳戶中 Azure 容器執行個體 的無伺服器沙箱所佈建的配額:

    1. 在 Azure 入口網站中開啟新的支援要求(將問題類型設為服務和訂閱限制 (配額),並將配額類型設為容器執行個體),選取您的訂閱,然後前往附加詳細資料索引標籤。

    2. 選取輸入詳細資訊以開啟配額詳細資料窗格,選擇您的區域,並在配額類型中選取AIAgentsGroups - StandardCores (Preview),然後填寫網路類型、容器類型、AIAgentsGroups 群組總限制及AIAgentsGroups 核心總限制:

      Azure 入口網站中新增支援要求頁面的螢幕擷取畫面,顯示已選取 AIAgentsGroups - StandardCores (預覽版) 的配額詳細資料窗格。

    3. 提交支援申請。 在您的配額申請獲核准後,請重新註冊 Microsoft.ContainerInstance 資源提供者(az provider register --namespace Microsoft.ContainerInstance --wait),以便您的訂閱能接收新啟用的功能。

  • 在建立 AI 代理群組和沙箱後(請參閱 快速入門:建立您的第一個無伺服器沙箱)後,若要連接正在執行中的沙箱,請使用由沙箱閘道 protobuf 檔案 sandboxgateway.proto產生的 gRPC 用戶端。 需要使用 gRPC 用戶端,因為沙箱閘道會公開 gRPC 服務(aci.sandboxgateway.SandboxGatewayService),以便執行沙箱內作業,例如等待就緒、執行命令、傳輸檔案,以及讀取日誌。 若要使用本文件中的跨平台範例 C# CLI(支援 Linux、macOS 和 Windows),請安裝 .NET 8 SDK,並編譯 CLI。

    Note

    如果你透過 Azure Kubernetes Service (AKS) 排程和管理無伺服器沙盒,則不需要 gRPC 用戶端。

建立虛擬網路

建立包含兩個子網路的虛擬網路:

子網路 委派 使用對象
sandbox-subnet Microsoft.ContainerInstance/AIAgentsGroups 這些沙盒。 每個沙盒使用一個 IP 位址。
app-subnet Microsoft.ContainerInstance/containerGroups 本教學中的私人網頁應用程式。
az group create --name myResourceGroup --location centralus

az network vnet create --resource-group myResourceGroup --name myVnet --location centralus --address-prefix 10.10.0.0/16

az network vnet subnet create --resource-group myResourceGroup --vnet-name myVnet --name sandbox-subnet \
  --address-prefixes 10.10.0.0/21 --delegations Microsoft.ContainerInstance/AIAgentsGroups

az network vnet subnet create --resource-group myResourceGroup --vnet-name myVnet --name app-subnet \
  --address-prefixes 10.10.8.0/24 --delegations Microsoft.ContainerInstance/containerGroups

根據您計畫同時執行的沙箱數量,為沙箱子網路設定大小。 每個沙盒使用該子網路中的一個 IP 位址。 這個例子中的 /21子網路可容納約 2,000 個沙盒。

Important

對於外出網際網路存取(出口),請將 NAT 閘道與虛擬網路中的沙箱子網路關聯,並將該子網路的預設外出存取設定defaultOutboundAccess()設定為 true。 有關 NAT 閘道設定的指引,請參閱 〈使用 NAT 閘道設定容器群組出口〉。

部署私有網頁應用程式

在 app-subnet 中部署一個容器群組。 容器群組只有私人 IP 位址,因此無法從網際網路存取:

az container create --resource-group myResourceGroup --name private-web \
  --image mcr.microsoft.com/azuredocs/aci-helloworld --vnet myVnet --subnet app-subnet --os-type Linux --cpu 1 --memory 1.5

az container show --resource-group myResourceGroup --name private-web --query ipAddress.ip -o tsv

輸出為來自 app-subnet的私有 IP 位址,例如 10.10.8.4。

在虛擬網路中建立 AI 代理群組

  1. 建立資源 ID 為 sandbox-subnet 的 AI 代理群組於 properties.networkProfile.subnets 中:

    PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myVnetAgentsGroup?api-version=2026-08-01-preview
    
    {
      "location": "centralus",
      "properties": {
        "networkProfile": {
          "subnets": [
            {
              "id": "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/sandbox-subnet"
            }
          ]
        }
      }
    }
    
  2. 檢查 AI 代理群組直到 properties.provisioningState 是 Succeeded,然後撥打 connect 來取得它的 {endpoint} 和 {accessToken}:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myVnetAgentsGroup?api-version=2026-08-01-preview
    
    POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myVnetAgentsGroup/connect?api-version=2026-08-01-preview
    

群組中的所有沙盒都使用該子網路。 你在建立 AI 代理群組後,就無法更改它的網路設定檔。

從沙箱存取私人 Web 應用程式

  1. 在群組中建立一個沙盒。 圖片 azure-cli 包含 curl:

    PUT {endpoint}/sandboxes/vnet1?api-version=2026-08-01-preview
    Authorization: Bearer {accessToken}
    Content-Type: application/json
    
    {
      "location": "centralus",
      "properties": {
        "image": "mcr.microsoft.com/azure-cli"
      }
    }
    

    等到properties.provisioningState為Succeeded。 接著從回覆中複製 properties.connectionString,並等待來賓準備就緒 (WaitForReady)。 透過使用 範例 C# CLI:

    dotnet run -- -cs "<vnet1-connection-string>" wait
    
  2. 在 vnet1 中,要求存取私人 Web 應用程式 (ExecSandbox)。 將 <private-web-ip> 替換為 private-web 的私人 IP 位址:

    dotnet run -- -cs "<vnet1-connection-string>" exec curl -s http://<private-web-ip>
    

    回應包含網頁應用程式的 HTML,其中包含以下文字:

    Welcome to Azure Container Instances!
    

沙盒透過虛擬網路抵達網頁應用程式。 未連接虛擬網路的 AI 代理群組中的沙盒無法存取此私有 IP 位址。

Note

對於連接到虛擬網路的 ACI 無伺服器沙盒,網路隔離取決於所委派的子網路。 若要阻擋或限制任何方向的流量(入站或出站),請在沙箱子網路上設定網路安全群組(NSG)。

清理資源

  1. 刪除 AI 代理群組。 刪除 AI 代理群組是一項長期作業。 它也會刪除群組中的沙盒:

    DELETE https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myVnetAgentsGroup?api-version=2026-08-01-preview
    
  2. 刪除網頁應用程式和虛擬網路。 在 AI 代理程式群組刪除完成後,刪除虛擬網路。 當 AI 代理群組使用子網路時,你無法刪除該子網路。

    az container delete --resource-group myResourceGroup --name private-web --yes
    az network vnet delete --resource-group myResourceGroup --name myVnet
    

下一步