共用方式為


教學課程:使用 Resource Manager 範本部署多容器群組

Azure 容器執行個體支援使用 容器群組將多個容器部署至單一主機。 當建立用於記錄、監測或任何需要第二個附加程序的服務組態的應用程式側車時,容器群組非常有用。

在本教學課程中,您會遵循使用 Azure CLI 部署 Azure Resource Manager 範本的步驟來執行雙容器側車設定。 您將學習如何:

  • 設定多容器群組範本
  • 部署容器群組
  • 檢視容器的日誌

Resource Manager 範本可以輕鬆適應您需要使用容器群組部署更多 Azure 服務資源 (例如 Azure 檔案儲存體共用或虛擬網路) 的案例。

備註

多容器群組目前僅限於 Linux 容器。

如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶

先決條件

設定範本

首先,將下列 JSON 複製到名為 azuredeploy.json的新檔案中。 在 Azure Cloud Shell 中,您可以使用 Visual Studio Code 在工作目錄中建立檔案:

code azuredeploy.json

此 Resource Manager 範本會定義具有兩個容器、公用 IP 位址和兩個公開埠的容器群組。 群組中的第一個容器會執行網際網路對向的 Web 應用程式。 第二個容器,即側車,透過群組的區域網路向主要 Web 應用程式發出 HTTP 請求。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "containerGroupName": {
      "type": "string",
      "defaultValue": "myContainerGroup",
      "metadata": {
        "description": "Container Group name."
      }
    }
  },
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "mcr.microsoft.com/azuredocs/aci-helloworld:latest",
    "container2name": "aci-tutorial-sidecar",
    "container2image": "mcr.microsoft.com/azuredocs/aci-tutorial-sidecar"
  },
  "resources": [
    {
      "name": "[parameters('containerGroupName')]",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2019-12-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "containers": [
          {
            "name": "[variables('container1name')]",
            "properties": {
              "image": "[variables('container1image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              },
              "ports": [
                {
                  "port": 80
                },
                {
                  "port": 8080
                }
              ]
            }
          },
          {
            "name": "[variables('container2name')]",
            "properties": {
              "image": "[variables('container2image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              }
            }
          }
        ],
        "osType": "Linux",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "protocol": "tcp",
              "port": 80
            },
            {
                "protocol": "tcp",
                "port": 8080
            }
          ]
        }
      }
    }
  ],
  "outputs": {
    "containerIPv4Address": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.ContainerInstance/containerGroups/', parameters('containerGroupName'))).ipAddress.ip]"
    }
  }
}

若要使用私人容器映像登錄,請使用下列格式將物件新增至 JSON 文件。 如需此設定的範例實作,請參閱 ACI Resource Manager 範本參考 檔。

"imageRegistryCredentials": [
  {
    "server": "[parameters('imageRegistryLoginServer')]",
    "username": "[parameters('imageRegistryUsername')]",
    "password": "[parameters('imageRegistryPassword')]"
  }
]

部署範本

使用 az group create 命令來建立資源群組。

az group create --name myResourceGroup --location eastus

使用 az deployment group create 命令部署範本。

az deployment group create --resource-group myResourceGroup --template-file azuredeploy.json

在幾秒鐘內,您應該會收到來自 Azure 的初始回應。

檢視部署狀態

若要檢視部署的狀態,請使用下列 az container show 命令:

az container show --resource-group myResourceGroup --name myContainerGroup --output table

如果您想查看正在運行的應用程序,請在瀏覽器中導航到其 IP 位址。 例如,IP位於 52.168.26.124 以下示例輸出中:

Name              ResourceGroup    Status    Image                                                                                               IP:ports              Network    CPU/Memory       OsType    Location
----------------  ---------------  --------  --------------------------------------------------------------------------------------------------  --------------------  ---------  ---------------  --------  ----------
myContainerGroup  danlep0318r      Running   mcr.microsoft.com/azuredocs/aci-tutorial-sidecar,mcr.microsoft.com/azuredocs/aci-helloworld:latest  20.42.26.114:80,8080  Public     1.0 core/1.5 gb  Linux     eastus

檢視容器日誌

使用 az container logs 命令檢視容器的記錄輸出。 引 --container-name 數會指定要從中提取記錄的容器。 在此範例中, aci-tutorial-app 會指定容器。

az container logs --resource-group myResourceGroup --name myContainerGroup --container-name aci-tutorial-app

輸出:

listening on port 80
::1 - - [02/Jul/2020:23:17:48 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"
::1 - - [02/Jul/2020:23:17:51 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"
::1 - - [02/Jul/2020:23:17:54 +0000] "HEAD / HTTP/1.1" 200 1663 "-" "curl/7.54.0"

若要查看側車容器的日誌,請執行類似的命令,並指定使用 aci-tutorial-sidecar 容器。

az container logs --resource-group myResourceGroup --name myContainerGroup --container-name aci-tutorial-sidecar

輸出:

Every 3s: curl -I http://localhost                          2020-07-02 20:36:41

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  1663    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Wed, 29 Nov 2017 06:40:40 GMT
ETag: W/"67f-16006818640"
Content-Type: text/html; charset=UTF-8
Content-Length: 1663
Date: Thu, 02 Jul 2020 20:36:41 GMT
Connection: keep-alive

如您所見,Sidecar 會定期透過群組的區域網路向主 Web 應用程式發出 HTTP 請求,以確保其執行。 此側車範例可以擴展,以在收到除200 OK之外的 HTTP 回應碼時觸發警示。

後續步驟

在本教學課程中,您使用 Azure Resource Manager 範本在 Azure 容器執行個體中部署多容器群組。 您已經學會如何:

  • 設定多容器群組範本
  • 部署容器群組
  • 檢視容器的日誌

如需更多範本範例,請參閱適用於 Azure 容器執行個體的 Azure Resource Manager 範本

您也可以使用 YAML 檔案來指定多儲存器群組。 由於 YAML 格式的本質更簡潔,當您的部署僅包含容器執行個體時,使用 YAML 檔案進行部署是不錯的選擇。