你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure 容器实例支持使用 容器组将多个容器部署到单个主机上。 当生成应用程序 Sidecar 以用于日志记录、监视或某些其他配置(其中的服务需要第二个附加进程)时,容器组很有用。
在本教程中,您将按照步骤使用 Azure CLI 部署 YAML 文件,运行双容器旁车配置。 YAML 文件提供用于指定实例设置的简洁格式。 你将学会如何:
- 配置 YAML 文件
- 部署容器组
- 查看容器的日志
注释
多容器组目前仅限于 Linux 容器。
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
先决条件
在 Azure Cloud Shell 中使用 Bash 环境。 有关详细信息,请参阅 Azure Cloud Shell 入门。
如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。
如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅 使用 Azure CLI 向 Azure 进行身份验证。
出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 使用和管理 Azure CLI 中的扩展。
运行az version命令,以查看已安装的版本和依赖库。 若要升级到最新版本,请运行az upgrade。
配置 YAML 文件
若要在 Azure CLI 中使用 az container create 命令部署多容器组,必须在 YAML 文件中指定容器组配置。 然后将 YAML 文件作为参数传递给命令。
首先,将以下 YAML 复制到名为 deploy-aci.yaml 的新文件中。 在 Azure Cloud Shell 中,可以使用 Visual Studio Code 在工作目录中创建文件:
code deploy-aci.yaml
此 YAML 文件定义名为“myContainerGroup”的容器组,其中包含两个容器、一个公共 IP 地址和两个公开的端口。 容器是从公共Microsoft镜像部署的。 组中的第一个容器运行面向 Internet 的 Web 应用程序。 第二个容器(sidecar)定期通过容器组的本地网络向在第一个容器中运行的 Web 应用程序发出 HTTP 请求。
apiVersion: 2019-12-01
location: eastus
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 属性添加到容器组,并为环境修改值:
imageRegistryCredentials:
- server: imageRegistryLoginServer
username: imageRegistryUsername
password: imageRegistryPassword
部署容器组
使用 az group create 命令创建资源组:
az group create --name myResourceGroup --location eastus
使用 az container create 命令部署容器组,并将 YAML 文件作为参数传递:
az container create --resource-group myResourceGroup --file deploy-aci.yaml
几秒钟内,应收到来自 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"
若要查看 sidecar 容器的日志,请运行类似 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 请求,以确保其运行。 如果此挎斗示例收到的 HTTP 响应代码不是 200 OK,则可将其扩展以触发警报。
后续步骤
本教程使用 YAML 文件在 Azure 容器实例中部署多容器组。 你已了解如何执行以下操作:
- 为多容器组配置 YAML 文件
- 部署容器组
- 查看容器的日志
还可以使用 资源管理器模板指定多容器组。 需要使用容器组部署更多 Azure 服务资源时,可以轻松调整资源管理器模板。