如何通过YAML文件创建Azure Container Instance并配置ACR凭证与环境变量
Azure Container Instance YAML配置方案
1. 私有Azure Container Registry(ACR)拉取配置
你需要在YAML配置的顶层根节点下添加imageRegistryCredentials数组字段,按以下格式填写你的ACR认证信息:
imageRegistryCredentials: - server: <你的ACR登录服务器地址,例如xxx.azurecr.io> username: <ACR的管理员用户名> password: <ACR的管理员密码或服务主体密码>
如果使用服务主体而非ACR管理员账号,username填服务主体的客户端ID,password填服务主体的客户端密钥即可。
2. 环境变量配置
环境变量配置在单个容器的properties节点下,通过environmentVariables数组字段定义,支持普通明文环境变量和敏感信息类环境变量两种格式:
containers: - name: <你的容器名称> properties: image: <你的私有镜像地址,例如xxx.azurecr.io/镜像名:标签> environmentVariables: # 普通明文环境变量 - name: ENV_NAME1 value: "环境变量值1" - name: ENV_NAME2 value: "环境变量值2" # 敏感信息环境变量(不会在容器实例详情中明文展示) - name: DB_PASSWORD secureValue: "你的数据库密码等敏感值" # 其余容器配置(端口、资源、挂载卷等) ports: - port: 80 resources: requests: cpu: 1 memoryInGB: 1.5
完整配置参考示例
以下是包含两种配置的最小可用YAML片段,你可以结合你已有的文件存储挂载配置合并使用:
apiVersion: 2021-10-01 location: <Azure区域,例如chinaeast2> name: <容器组名称> properties: imageRegistryCredentials: - server: xxx.azurecr.io username: acradmin password: xxxxxxxx containers: - name: my-app-container properties: image: xxx.azurecr.io/my-app:v1.0 environmentVariables: - name: ASPNETCORE_ENVIRONMENT value: Production - name: TZ value: Asia/Shanghai - name: API_KEY secureValue: abcdef123456 ports: - port: 80 resources: requests: cpu: 1 memoryInGB: 2 # 你已配置的Azure Files挂载配置保留在此处即可 volumeMounts: - name: azurefilevolume mountPath: /app/data readOnly: false osType: Linux ipAddress: type: Public ports: - protocol: tcp port: 80 volumes: - name: azurefilevolume azureFile: sharename: <文件共享名> storageAccountName: <存储账号名> storageAccountKey: <存储账号密钥> tags: {} type: Microsoft.ContainerInstance/containerGroups
配置完成后使用az container create --resource-group <资源组名> --file <你的YAML文件名>命令即可创建容器实例。
内容的提问来源于stack exchange,提问作者serge calderara
相关产品推荐
相关产品推荐

