教程:使用 YAML 文件部署多容器组Tutorial: Deploy a multi-container group using a YAML file

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 部署 YAML 文件来运行简单的双容器 Sidecar 配置。In this tutorial, you follow steps to run a simple two-container sidecar configuration by deploying a YAML file using the Azure CLI. YAML 文件提供了用来指定实例设置的简洁格式。A YAML file provides a concise format for specifying the instance settings. 学习如何:You learn how to:

  • 配置 YAML 文件Configure a YAML file
  • 部署容器组Deploy the container group
  • 查看容器的日志View the logs of the containers

备注

多容器组当前仅限于 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.

配置 YAML 文件Configure a YAML file

若要在 Azure CLI 中使用 az container create 命令部署多容器组,必须在 YAML 文件中指定容器组配置,To deploy a multi-container group with the az container create command in the Azure CLI, you must specify the container group configuration in a YAML file. 然后将该 YAML 文件作为参数传递给命令。Then pass the YAML file as a parameter to the command.

首先将以下 YAML 复制到一个名为 deploy-aci.yaml 的新文件中。Start by copying the following YAML into a new file named deploy-aci.yaml.

code deploy-aci.yaml

此 YAML 文件定义了一个名为“myContainerGroup”的容器组,其中包含两个容器、一个公共 IP 地址和两个公开端口。This YAML file defines a container group named "myContainerGroup" with two containers, a public IP address, and two exposed ports. 这些容器从公共 Azure 映像部署。The containers are deployed from public Azure images. 该组中的第一个容器运行面向 Internet 的 Web 应用程序。The first container in the group runs an internet-facing web application. 第二个容器 sidecar 定期通过容器组的本地网络向在第一个容器中运行的 Web 应用程序发出 HTTP 请求。The second container, the sidecar, periodically makes HTTP requests to the web application running in the first container via the container group's local network.

apiVersion: 2019-12-01
location: chinaeast2
name: myContainerGroup
properties:
  containers:
  - name: aci-tutorial-app
    properties:
      image: mcr.microsoft.com/azuredocs/aci-helloworld:latest
      resources:
        requests:
          cpu: 1
          memoryInGb: 1.5
      ports:
      - port: 80
      - port: 8080
  - name: aci-tutorial-sidecar
    properties:
      image: mcr.microsoft.com/azuredocs/aci-tutorial-sidecar
      resources:
        requests:
          cpu: 1
          memoryInGb: 1.5
  osType: Linux
  ipAddress:
    type: Public
    ports:
    - protocol: tcp
      port: 80
    - protocol: tcp
      port: 8080
tags: {exampleTag: tutorial}
type: Microsoft.ContainerInstance/containerGroups

若要使用专用容器映像注册表,请将 imageRegistryCredentials 属性添加到容器组,其中包含针对你的环境修改的值:To use a private container image registry, add the imageRegistryCredentials property to the container group, with values modified for your environment:

  imageRegistryCredentials:
  - server: imageRegistryLoginServer
    username: imageRegistryUsername
    password: imageRegistryPassword

部署容器组Deploy the container group

使用 az group create 命令创建资源组:Create a resource group with the az group create command:

az group create --name myResourceGroup --location chinaeast2

使用 az container create 命令部署容器组并传递 YAML 文件作为参数:Deploy the container group with the az container create command, passing the YAML file as an argument:

az container create --resource-group myResourceGroup --file deploy-aci.yaml

将在几秒钟内收到来自 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

在本教程中,你使用了 YAML 文件在 Azure 容器实例中部署多容器组。In this tutorial, you used a YAML file to deploy a multi-container group in Azure Container Instances. 你已了解如何执行以下操作:You learned how to:

  • 为多容器组配置 YAML 文件Configure a YAML file for a multi-container group
  • 部署容器组Deploy the container group
  • 查看容器的日志View the logs of the containers

还可以使用资源管理器模板指定多容器组。You can also specify a multi-container group using a Resource Manager template. 当你需要使用容器组部署其他 Azure 服务资源时,可以很容易地根据场景调整资源管理器模板。A Resource Manager template can be readily adapted for scenarios when you need to deploy additional Azure service resources with the container group.