使用 COPY 通过 Azure ARM 模板创建多个 ACI 时出错

Error when create multiple ACI via Azure ARM template using COPY

我目前正在尝试通过 ARM 模板在 Azure 容器实例中部署一个容器组,所有这些都是从 Azure Devops Yaml 构建管道调用的。我发现我可以使用 copy 语句来创建多个资源组 and/or 属性。

这是我的 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"
        }
    }
]

}

如您所见,我有两个 copy 迭代。 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);
    }
}

mainloop 用于第一个 copy 迭代

迭代是第二个

我目前面临的问题是,当我要求模板创建100个容器时,出现以下构建错误

楼上说的很清楚了,就是不明白问题出在哪里。 imageRegistryCredentials 属性 为每个 copy 迭代定义一次,并且与 containers 属性,为什么第一次迭代成功,然后失败?

据我所知(我已经很长时间没有重新访问这个特定场景了)- 不能在同一资源上使用副本和属性副本。您的解决方法是 - 创建链接部署(每 60 个容器 1 个),然后您只需要在每个容器中复制属性。

但考虑到这个错误,我不确定目前这种情况是否可行。因为以前它会抱怨 copyIndex() 在那个地方不被期待。

我发现我的 ARM 模板出了什么问题。这是一个愚蠢的错误输入....在我的 "properties" 复制迭代函数的最后,我输入了 0 值而不是 60

"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)]",

如果我用 C# 等效项替换它,

我应该放什么:

 int iteration = div == 0 ? (mod == 0 ? 60 : mod) : 60;

我放的是:

 int iteration = div == 0 ? (mod == 0 ? 60 : mod) : 0;

区别在最后...

无论如何谢谢你4c74356b41的帮助!