我目前正在尝试通过 ARM 模板在 Azure 容器实例内部署容器组,所有模板均从 Azure Devops Yaml 构建管道调用。我发现我可以使用复制句子来创建多个资源组和/或属性。
这是我的 ARM 模板
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"containerGroupName": {
"type": "string",
"metadata": {
"description": "Emulators ACR name."
}
},
"copies":{
"type": "int",
"defaultValue": 2,
"metadata": {
"description": "Defines the number of container's copies."
}
},
"server":{
"type": "string",
"metadata": {
"description": "Defines the ACR server url."
}
},
"serverUser":{
"type": "string",
"metadata": {
"description": "Defines the ACR user."
}
},
"serverPassword":{
"type": "string",
"metadata": {
"description": "Defines the ACR password."
}
},
"imageName":{
"type": "string",
"metadata": {
"description": "Defines the ACR repository hosting the image."
}
},
"imageVersion":{
"type": "string",
"metadata": {
"description": "Defines the ACR image version."
}
},
"containerName":{
"type": "string",
"metadata": {
"description": "Defines the ACI containers name. This will be suffixed with the copy index."
}
}
},
"variables": {
"emulatorImage": "[concat(parameters('server'), '/', parameters('imageName'))]",
"emulatorImageVersion": "[parameters('imageVersion')]"
},
"resources": [
{
"name": "[concat(parameters('containerGroupName'), '-', copyIndex(1))]",
"type": "Microsoft.ContainerInstance/containerGroups",
"apiVersion": "2018-10-01",
"location": "[resourceGroup().location]",
"copy": {
"name": "acicopy",
"count": "[if(equals(mod(parameters('copies'), 60), 0), div(parameters('copies'), 60), add(div(parameters('copies'), 60), 1))]"
},
"properties": {
"copy": [
{
"name": "containers",
"count": "[if(equals(div(sub(parameters('copies'), mul(60, copyIndex())), 60), 0), if(equals(mod(sub(parameters('copies'), mul(60, copyIndex())), 60), 0), 60, mod(sub(parameters('copies'), mul(60, copyIndex())), 60)), 0)]",
"input": {
"name": "[concat(parameters('containerName'), '-', copyIndex(1), copyIndex('containers', 1))]",
"properties": {
"image": "[concat(variables('emulatorImage'), ':' ,variables('emulatorImageVersion'))]",
"resources": {
"requests": {
"cpu": 0.01,
"memoryInGB": 0.1
}
}
}
}
}
],
"imageRegistryCredentials": [
{
"server": "[parameters('server')]",
"username": "[parameters('serverUser')]",
"password": "[parameters('serverPassword')]"
}
],
"osType": "Linux"
}
}
]
}
如您所见,我有两次复制迭代。 RG 上的第一个用于生成足够的容器实例(因为我限制每个 ACI 60 个容器),第二个用于属性,用于生成多个容器(最多 60 个)。
因此,如果我需要 100 个容器,我应该创建 2 个 ACI,第一个将包含 60 个容器,第二个包含 40 个容器。
copy 的计数属性条件可能看起来有点难以阅读,因此这里是 C# 等效项。
public static void DefineNumber(int number)
{
Console.WriteLine("Number : " + number);
int mainLoop = number % 60 == 0 ? (int)(number / 60) : (int)(number / 60) + 1;
Console.WriteLine("MainLoop : " + mainLoop);
for(int i = 0; i < mainLoop; i++)
{
Console.WriteLine("----");
int div = (number - (60 * i)) / 60;
Console.WriteLine("Div : " + div);
int mod = (number - (60 * i)) % 60;
Console.WriteLine("Mod : " + mod);
int iteration = div == 0 ? (mod == 0 ? 60 : mod) : 60;
Console.WriteLine("Number of containers for main loop n°" + (i+1) + " will be : " + iteration);
}
}
主循环用于第一次复制迭代
迭代是第二次迭代
我目前面临的问题是,当我要求模板创建 100 个容器时,出现以下构建错误
消息已经很清楚了,但我不明白问题出在哪里。 imageRegistryCredentials 属性为每个 copy 迭代定义一次,并且与 containers 属性位于同一级别,那么为什么它在第一次迭代时会成功,然后失败?
最佳答案
据我所知(我很长一段时间没有重新访问这个特定场景) - 您不能在同一资源上使用副本和属性副本。您的解决方法是 - 创建链接部署(每 60 个容器 1 个),然后您只需要在每个容器内复制属性。
但鉴于此错误,我不确定目前这种情况是否可行。因为以前它会提示那个地方没有预期的 copyIndex() 。
关于azure - 使用 COPY 通过 Azure ARM 模板创建多个 ACI 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60618057/