【Azure App Service】在App Service中使用az cli命令创建 Sidecar Container - LuBu0505/My-Code GitHub Wiki

问题描述

在使用 Azure App Service 时,参考官方文档发现支持 Sidecar Container。

但在实际创建或已有的 App Service 实例中,无法在门户中找到“Container”的相关选项,无法启动Sidecar。 image.png

文档上提出使用ARM模板的方式启动,虽然也是可以,但想知道有没有更简洁的方式(使用az cli命令)来实现这个目的呢?

问题解答

当然可以实现。

使用 az webapp sitecontainers create/update 就可以实现创建 Sidecar Container。

执行步骤

第一步:固定云和订阅

az cloud set --name AzureChinaCloud

az login

az account set --subscription <your subscription id> 

az account show --query "{sub:id,name:name,user:user.name}" -o table

第二步:设置变量(可改)

$loc = "chinaeast2"
$rg = "your resource group name"
$plan = "your app service plan"
$app = "your app service name"

第三步:创建资源组 + Linux Plan

az group create -n $rg -l $loc 
az appservice plan create -g $rg -n $plan --is-linux --sku B1 -l $loc

第四步:创建 WebApp(注意策略要求 https-only)

az webapp create -g $rg -p $plan -n $app --runtime PYTHON:3.11 --https-only true 

az webapp log config -g $rg -n $app --docker-container-logging filesystem

第五步:创建主容器 main(sitecontainers)

 az webapp sitecontainers create -g $rg -n $app --container-name main --image mcr.microsoft.com/dotnet/samples:aspnetapp --target-port 8080 --is-main true --startup-cmd "dotnet aspnetapp.dll" -o none

第六步:创建 sidecar(最小镜像、可探活)

az webapp sitecontainers create -g $rg -n $app --container-name sidecar --image mcr.microsoft.com/azuredocs/aci-helloworld --target-port 80 --is-main false

验证方式

当以上六步执行完成后,可以使用如下的命令来验证当前app下的container及状态

az webapp sitecontainers list -g $rg -n $app -o table
az webapp sitecontainers status -g $rg -n $app -o table
az webapp sitecontainers status -n $app -g $rg --container-name "sidecar" -o json

以上命令,只需要在第二步修改变量名称后,就可以成功执行 image.png

附录

1:什么是 Sidecar

Sidecar 是一种容器架构模式:

  • 一个主容器(Main container)负责核心业务
  • 一个或多个 Sidecar 容器负责辅助能力,例如:
    • 日志收集
    • 代理(如 Envoy)
    • 配置同步
    • 安全或监控

它们共享:同一主机环境 和 网络命名空间(通常 localhost 通信)

2:在 App Service 中的实现(SiteContainers)

  • Sidecar 模式通过 sitecontainers 资源定义
  • 主容器与 sidecar 是独立声明的容器对象
  • 每个容器有自己的配置(镜像、registry、认证等)

不再依赖传统 WebApp 的 App Settings(如 DOCKER_REGISTRY), 所有容器配置必须在 sitecontainers 中重新定义。

参考资料

在 Azure 应用服务中为 Linux 应用配置边车容器:https://docs.azure.cn/zh-cn/app-service/tutorial-sidecar?tabs=portal#3-add-a-sidecar-container

az webapp sitecontainers create : https://learn.microsoft.com/en-us/cli/azure/webapp/sitecontainers?view=azure-cli-latest#az-webapp-sitecontainers-create

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!