教程:使用资源管理器模板部署多容器组Tutorial: Deploy a multi-container group using a Resource Manager template

Azure 容器实例支持使用容器组将多个容器部署到单台主机上。Azure Container Instances supports the deployment of multiple containers onto a single host using a container group. 当生成应用程序 Sidecar 以用于日志记录、监视或某些其他配置(其中的服务需要第二个附加进程)时,容器组很有用。A container group is useful when building an application sidecar for logging, monitoring, or any other configuration where a service needs a second attached process.

在本教程中,按照以下步骤,使用 Azure CLI 部署 Azure 资源管理器模板,以运行简单的双容器 Sidecar 配置。In this tutorial, you follow steps to run a simple two-container sidecar configuration by deploying an Azure Resource Manager template using the Azure CLI. 学习如何:You learn how to:

  • 配置多容器组模板Configure a multi-container group template
  • 部署容器组Deploy the container group
  • 查看容器的日志View the logs of the containers

当你需要使用容器组部署其他 Azure 服务资源(例如 Azure 文件存储或虚拟网络)时,可以很容易地根据场景调整资源管理器模板。A Resource Manager template can be readily adapted for scenarios when you need to deploy additional Azure service resources (for example, an Azure Files share or a virtual network) with the container group.

备注

多容器组当前仅限于 Linux 容器。Multi-container groups are currently restricted to Linux containers.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

配置模板Configure a template

首先将以下 JSON 复制到名为 azuredeploy.json 的新文件。Start by copying the following JSON into a new file named azuredeploy.json.

code azuredeploy.json

此资源管理器模板定义的容器组包含两个容器、一个公共 IP 地址和两个公开端口。This Resource Manager template defines a container group with two containers, a public IP address, and two exposed ports. 该组中的第一个容器运行面向 Internet 的 Web 应用程序。The first container in the group runs an internet-facing web application. 第二个容器 sidecar 通过该组的本地网络向主要 Web 应用程序发出 HTTP 请求。The second container, the sidecar, makes an HTTP request to the main web application via the group's local network.

{
  "$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 文档添加对象。To use a private container image registry, add an object to the JSON document with the following format.

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

部署模板Deploy the template

使用“az group create”命令创建资源组。Create a resource group with the az group create command.

az group create --name myResourceGroup --location chinaeast2

使用 az deployment group create 命令部署模板。Deploy the template with the az deployment group create command.

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

将在几秒钟内收到来自 Azure 的初始响应。Within a few seconds, you should receive an initial response from Azure.

查看部署状态View deployment state

若要查看部署状态,请运行下面的 az container show 命令:To view the state of the deployment, use the following az container show command:

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

若要查看正在运行的应用程序,请在浏览器中转到它的 IP 地址。If you'd like to view the running application, navigate to its IP address in your browser. 例如,在此示例输出中,IP 为 52.168.26.124For example, the IP is 52.168.26.124 in this example output:

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     chinaeast2

查看容器日志View container logs

使用 az container logs 命令查看容器的日志输出。View the log output of a container using the az container logs command. --container-name 参数指定从中拉取日志的容器。The --container-name argument specifies the container from which to pull logs. 在此示例中,指定 aci-tutorial-app 容器。In this example, the aci-tutorial-app container is specified.

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

输出:Output:

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"

若要查看 Sidecar 容器的日志,请运行指定 aci-tutorial-sidecar 容器的类似命令。To see the logs for the sidecar container, run a similar command specifying the aci-tutorial-sidecar container.

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

输出:Output:

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 请求,确保其正在运行。As you can see, the sidecar is periodically making an HTTP request to the main web application via the group's local network to ensure that it is running. 如果此 Sidecar 示例收到的 HTTP 响应代码不是 200 OK,则可将其扩展以触发警报。This sidecar example could be expanded to trigger an alert if it received an HTTP response code other than 200 OK.

后续步骤Next steps

在本教程中,你使用了 Azure 资源管理器模板在 Azure 容器实例中部署多容器组。In this tutorial, you used an Azure Resource Manager template to deploy a multi-container group in Azure Container Instances. 你已了解如何执行以下操作:You learned how to:

  • 配置多容器组模板Configure a multi-container group template
  • 部署容器组Deploy the container group
  • 查看容器的日志View the logs of the containers

有关模板示例的详细信息,请参阅适用于 Azure 容器实例的 Azure 资源管理器模板For additional template samples, see Azure Resource Manager templates for Azure Container Instances.

还可使用 YAML 文件指定多容器组。You can also specify a multi-container group using a YAML file. 由于 YAML 格式更简洁,因此,当部署仅包含容器实例时,使用 YAML 文件进行部署是很好的选择。Due to the YAML format's more concise nature, deployment with a YAML file is a good choice when your deployment includes only container instances.