你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure 容器实例的 ConfigMap(预览版)
ConfigMap 是一个属性,可用于应用类似于环境变量和机密卷的容器配置。 但是,与使用环境变量或机密卷时需要重新启动 pod 才能应用设置不同,使用 ConfigMap 应用设置不需要重新启动即可使更改生效。
无论是否使用 ConfigMap 都可创建 Azure 容器实例,并且可使用 ConfigMap 在创建后的任何时间点更新这些实例。 可以快速更新现有正在运行的容器组中的 ConfigMap,而不会危及容器的运行时间。
工作原理
ConfigMap 可以包含在容器属性或容器组配置文件中。 使用 ConfigMap 设置创建容器组配置文件使得应用这些设置变得简单且易于自动化。
使用 ConfigMap 设置创建容器组配置文件
使用 az container container-group-profile create 通过 ConfigMap 设置创建容器组配置文件。
az container container-group-profile create \
--resource-group myResourceGroup \
--name myContainerGroupProfile \
--location WestCentralUS \
--image nginx \
--os-type Linux \
--ip-address Public \
--ports 8000 \
--cpu 1 \
--memory 1.5 \
--restart-policy never \
--config-map key1=value1 key2=value2
使用 New-AzContainerInstanceContainerGroupProfile 通过 ConfigMap 设置创建容器组配置文件。
$port1 = New-AzContainerInstancePortObject -Port 8000 -Protocol TCP
$port2 = New-AzContainerInstancePortObject -Port 8001 -Protocol TCP
$container = New-AzContainerInstanceObject -Name myContainer -Image nginx -RequestCpu 1 -RequestMemoryInGb 1.5 -Port @($port1, $port2) -ConfigMapKeyValuePair @{"key1"="value1"}
New-AzContainerInstanceContainerGroupProfile `
-ResourceGroupName myResourceGroup `
-Name myContainerGroupProfile `
-Location WestCentralUS `
-Container $container `
-OsType Linux `
-RestartPolicy "Never" `
-IpAddressType Public
使用 az deployment group create 或 New-AzResourceGroupDeployment 通过 ConfigMap 设置创建容器组配置文件并部署模板。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.ContainerInstance/containerGroups",
"apiVersion": "2024-05-01-preview",
"name": "[parameters('profileName')]",
"location": "[parameters('location')]",
"properties": {
"containers": [
{
"name": "myContainer",
"properties": {
"image": "[parameters('containerImage')]",
"ports": [
{
"port": 8000
}
],
"resources": {
"requests": {
"cpu": 1,
"memoryInGB": 1.5
}
},
"command": [],
"configMap": {
"keyValuePairs": {
"key1": "value1",
"key2": "value2"
}
"environmentVariables": []
}
}
],
"osType": "Linux",
"ipAddress": {
"type": "Public",
"ports": [
{
"protocol": "TCP",
"port": 8000
}
]
},
"imageRegistryCredentials": [],
"sku": "Standard"
}
}
],
"parameters": {
"profileName": {
"type": "string",
"defaultValue": "myContainerGroupProfile",
"metadata": {
"description": "Name of the container profile"
}
},
"location": {
"type": "string",
"defaultValue": "West Central US",
"metadata": {
"description": "Location for the resource"
}
},
"containerImage": {
"type": "string",
"defaultValue": "mcr.microsoft.com/azuredocs/aci-helloworld:latest",
"metadata": {
"description": "The container image used"
}
}
}
}
使用 Create or Update 通过 ConfigMap 设置创建容器组配置文件。
PUT https://management.azure.com/subscriptions/{SubscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroupProfiles/myContainerGroupProfile?api-version=2024-05-01-preview
Request Body
{
"location": "West Central US",
"properties":{
"containers": [
{
"name":"myContainerGroupProfile",
"properties": {
"command":[],
"configMap": {
"keyValuePairs": {
"key1": "value1",
"key2": "value2"
}
},
"environmentVariables":[],
"image":"mcr.microsoft.com/azuredocs/aci-helloworld:latest",
"ports":[
{
"port":8000
}
],
"resources": {
"requests": {
"cpu":1,
"memoryInGB":1.5
}
}
}
}
],
"imageRegistryCredentials":[],
"ipAddress":{
"ports":[
{
"protocol":"TCP",
"port":8000
}
],
"type":"Public"
},
"osType":"Linux",
"sku":"Standard"
}
}
使用容器组配置文件应用 ConfigMap 设置
应用存储在容器组配置文件中的 ConfigMap 设置需要更新容器并指定应与更新关联的容器组配置文件。
使用 az container create 应用存储在容器组配置文件中的 ConfigMap 设置。
az container create
--resource-group myResourceGroup \
--name myContainer \
--location WestCentralUS \
--container-group-profile-id "/subscriptions/{SubscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroupProfiles/myContainerGroupProfile" \
--container-group-profile-revision 1
使用 New-AzContainerGroup 应用存储在容器组配置文件中的 ConfigMap 设置。
$container = New-AzContainerInstancenoDefaultObject -Name myContainer
New-AzContainerGroup `
-ResourceGroupName myResourceGroup `
-Name myContainer`
-Container $container `
-Location WestCentralUS `
-ContainerGroupProfileId "/subscriptions/{SubscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroupProfiles/myContainerGroupProfile" `
-ContainerGroupProfileRevision 1
使用 az deployment group create 或 New-AzResourceGroupDeployment 应用存储在容器组配置文件中的 ConfigMap 设置。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"subscriptionId": {
"type": "string",
"metadata": {
"description": "The subscription ID."
}
},
"resourceGroup": {
"type": "string",
"metadata": {
"description": "The name of the resource group."
}
},
"location": {
"type": "string",
"metadata": {
"description": "Location for the resource."
},
"defaultValue": "West Central US"
},
"containerGroupName": {
"type": "string",
"metadata": {
"description": "The name of the container group."
}
},
},
"containerGroupProfileName": {
"type": "string",
"metadata": {
"description": "The name of the container group profile."
}
},
"newKey": {
"type": "string",
"metadata": {
"description": "The new key for the config map."
}
},
"newValue": {
"type": "string",
"metadata": {
"description": "The new value for the config map."
}
},
"revisionNumber": {
"type": "int",
"metadata": {
"description": "The revision number for the container group profile."
}
}
},
"resources": [
{
"type": "Microsoft.ContainerInstance/containerGroups",
"apiVersion": "2024-05-01-preview",
"name": "[parameters('containerGroupName')]",
"location": "[parameters('location')]",
"properties": {
"containerGroupProfile": {
"id": "[concat('/subscriptions/', parameters('subscriptionId'), '/resourceGroups/', parameters('resourceGroup'), '/providers/Microsoft.ContainerInstance/containerGroupProfiles/', parameters('containerGroupProfileName'))]",
"revision": "[parameters('revisionNumber')]"
},
"containers": [
{
"name": "[parameters('myContainerProfile')]",
"properties": {
"configMap": {
"keyValuePairs": {
"[parameters('newKey')]": "[parameters('newValue')]"
}
}
}
}
]
}
}
],
"outputs": {
"containerGroupId": {
"type": "string",
"value": "[resourceId('Microsoft.ContainerInstance/containerGroups', parameters('containerGroupName'))]"
}
}
}
使用 Create or Update 应用存储在容器组配置文件中的 ConfigMap 设置。
PUT
https://management.azure.com/subscriptions/{SubscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroups/myContainerGroup?api-version=2024-05-01-preview
Request Body
{
"location": "{location}",
"properties": {
"containerGroupProfile": {
"id": "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroupProfiles/myContainerGroupProfile",
"revision": {revisionNumber}
},
"containers": [
{
"name": "{myContainerGroupProfile}",
"properties": {
}
}
]
}
}
不使用容器组配置文件应用 ConfigMap 设置
通过在 create 命令中指定 ConfigMap 设置,也可以将 ConfigMap 设置直接应用于实例。
使用 az container create 应用 ConfigMap 设置。
az container create \
--resource-group myResourceGroup \
--name myContainer \
--location WestCentralUS \
--config-map key1=value1 key2=value2
使用 New-AzContainerGrouop 应用 ConfigMap 设置。
$container = New-AzContainerInstancenoDefaultObject -Name myContainer -ConfigMapKeyValuePair @{"key1"="value1"}
New-AzContainerGroup `
-ResourceGroupName myResourceGroup `
-Name myContainerGroup `
-Container $container `
-Location WestCentralUS
使用 az deployment group create 或 New-AzResourceGroupDeployment 应用 ConfigMap 设置。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"subscriptionId": {
"type": "string",
"metadata": {
"description": "The subscription ID."
}
},
"resourceGroup": {
"type": "string",
"metadata": {
"description": "The name of the resource group."
}
},
"location": {
"type": "string",
"metadata": {
"description": "Location for the resource."
},
"defaultValue": "West Central US"
},
"containerGroupName": {
"type": "string",
"metadata": {
"description": "The name of the container group."
}
},
"myContainerProfile": {
"type": "string",
"metadata": {
"description": "The name of the container profile."
}
},
"newKey": {
"type": "string",
"metadata": {
"description": "The new key for the config map."
}
},
"newValue": {
"type": "string",
"metadata": {
"description": "The new value for the config map."
}
},
},
"resources": [
{
"type": "Microsoft.ContainerInstance/containerGroups",
"apiVersion": "2024-05-01-preview",
"name": "[parameters('containerGroupName')]",
"location": "[parameters('location')]",
"properties": {
"containers": [
{
"name": "[parameters('myContainerProfile')]",
"properties": {
"configMap": {
"keyValuePairs": {
"[parameters('newKey')]": "[parameters('newValue')]"
}
}
}
}
]
}
}
],
"outputs": {
"containerGroupId": {
"type": "string",
"value": "[resourceId('Microsoft.ContainerInstance/containerGroups', parameters('containerGroupName'))]"
}
}
}
使用 Create or Update 应用 ConfigMap 设置。
PUT
https://management.azure.com/subscriptions/{SubscriptionID}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/containerGroups/myContainerGroup?api-version=2024-05-01-preview
Request Body
{
"location": "{location}",
"properties": {
"containers": [
{
"name": "{myContainerGroupProfile}",
"properties": {
"configMap": {
"keyValuePairs": {
"{newKey}": "{newValue}"
}
}
}
}
]
}
}
更新应用于现有容器后,无需重启即可看到容器中装载的值。
/mnt/configmap/<containername>/key1 with value as “value1”
/mnt/configmap/<containername>/key2 with value as “value2”
后续步骤
了解如何将 ConfigMap 与备用池结合使用,以增加规模和可用性