如何使用azure资源管理器模板在linux的虚拟机规模集上创建自定义脚本扩展?

时间:2019-11-13 13:20:30

标签: azure azure-virtual-machine azure-template azure-autoscaling-block

这是我的template1.json,用于创建具有虚拟机规模集的集群。我想使用相同的arm template1向其添加扩展名。我尝试在托管群集的属性部分(template2.json)中包括扩展脚本,但它抛出找不到父资源vnetName 的错误。

我尝试在托管群集的“属性”部分中添加extensionProfile,但这也不起作用。

有人可以帮助我如何使用同一手臂模板设置自定义脚本扩展名吗?

template1.json

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vnetName": {
            "type": "string",
            "metadata": {
                "description": "VNet name"
              }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Azure resource location"
              }
        },
        "vnetAddressPrefix": {
            "type": "string",
            "metadata": {
                "description": "Virtual network address range"
              }
        },
        "subnetName": {
            "type": "string",
            "metadata": {
                "description": "Azure subnet Name"
            }
        },
        "subnetPrefix": {
            "type": "string",
            "metadata": {
                "description": "Azure subnet prefix"
            }
        },
        "kubernetesVersion": {
            "type": "string",
            "metadata": {
                "description": "kubernetesVersion"
            }
        },
        "enableRBAC": {
            "defaultValue": false,
            "type": "bool",
            "metadata": {
                "description": "boolean flag to turn on and off of RBAC"
            }
        },
        "dnsPrefix": {
            "type": "string",
            "metadata": {
                "description": "Optional DNS prefix to use with hosted Kubernetes API server FQDN."
            }
        },
        "enableHttpApplicationRouting": {
            "defaultValue": false,
            "type": "bool",
            "metadata": {
                "description": "boolean flag to turn on and off of http application routing"
            }
        },
        "osDiskSizeGB": {
            "defaultValue": 0,
            "minValue": 0,
            "maxValue": 1023,
            "type": "int",
            "metadata": {
                "description": "Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize."
            }
        },
        "agentCount": {
            "minValue": 1,
            "maxValue": 50,
            "type": "int",
            "metadata": {
                "description": "The number of agent nodes for the cluster. Production workloads have a recommended minimum of 3."
            }
        },
        "agentVMSize": {
            "type": "string",
            "metadata": {
                "description": "The size of the Virtual Machine."
            }
        },
        "masterCount": {
            "type": "int",
            "allowedValues": [
              1
            ],
            "metadata": {
              "description": "The number of Kubernetes masters for the cluster."
            }
        },
        "adminUsername": {
            "type": "string",
            "defaultValue": "azureuser",
            "metadata": {
               "description": "User name for the Linux Virtual Machines."
            }
        },
        "osType": {
            "defaultValue": "Linux",
            "allowedValues": [
                "Linux"
            ],
            "type": "string",
            "metadata": {
                "description": "The type of operating system."
            }
        },
        "maxPods": {
            "defaultValue": 30,
            "type": "int",
            "metadata": {
                "description": "Maximum number of pods that can run on a node."
            }
        },
        "ServicePrincipalClientId": {
            "type": "string",
            "metadata": {
                "description": "ServicePrincipalClientId"
            }
        },
        "ServicePrincipalClientSecret": {
            "type": "string",
            "metadata": {
                "description": "ServicePrincipalClientId"
            }
        },
        "ServicePrincipalObjectId": {
            "type": "string",
            "metadata": {
                "description": "ServicePrincipalClientId"
            }
        },
        "sshRSAPublicKey": {
            "type": "securestring",
            "metadata": {
               "description": "Configure all linux machines with the SSH RSA public key string"
            }
        },
        "networkPlugin": {
            "allowedValues": [
                "azure",
                "kubenet"
            ],
            "defaultValue": "kubenet",
            "type": "string",
            "metadata": {
                "description": "Network plugin used for building Kubernetes network."
            }
        },
        "dockerBridgeCidr": {
            "type": "string",
            "metadata": {
                "description": "A CIDR notation IP for Docker bridge."
            }
        },
        "dnsServiceIP": {
            "type": "string",
            "metadata": {
                "description": "Containers DNS server IP address."
            }
        },
        "serviceCidr": {
            "type": "string",
            "metadata": {
                "description": "A CIDR notation IP range from which to assign service cluster IPs."
            }
        },
        "resourceGroup": {
            "type": "string",
            "defaultValue": "[resourceGroup().name]",
            "metadata": {
                "description": "Azure resource group."
            }
        },
       "autoscalingEnabled": {
            "type": "bool",
            "defaultValue": true,
            "metadata": {
                "description": "Flag to enable autoscaling."
            }
        },
       "nodesMinimum": {
            "type": "int",
            "defaultValue": 2,
            "metadata": {
                "description": "Minimum number of nodes for autoscaling."
            }
        },
      "nodesMaximum": {
            "type": "int",
            "defaultValue": 4,
            "metadata": {
                "description": "Maximum number of nodes for autoscaling."
            }
        }
    },
    "variables": {
    "mastersEndpointDNSNamePrefix":"[concat(parameters('dnsPrefix'),'mgmt')]"
    },
    "resources": [
               {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2019-08-01",
            "name": "nestedTemplate",
            "properties": {
              "mode": "Incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "resources": [
                    {
                        "apiVersion": "2019-06-01",
                        "type": "Microsoft.Network/virtualNetworks",
                        "name": "[parameters('vnetName')]",
                        "location": "[parameters('location')]",
                        "properties": {
                            "addressSpace": {
                                "addressPrefixes": [
                                    "[parameters('vnetAddressPrefix')]"
                                ]
                            },
                            "subnets": [
                                {
                                    "name": "[parameters('subnetName')]",
                                    "properties": {
                                        "addressPrefix": "[parameters('subnetPrefix')]"
                                    }
                                }
                            ]
                        }
                   },
                   {
                        "type": "Microsoft.ContainerService/managedClusters",
                        "name":"[concat('Kluster-',resourceGroup().name)]",
                        "apiVersion": "2019-08-01",
                        "location": "[parameters('location')]",
                        "tags": {},
                        "dependsOn": [
                            "[concat('Microsoft.Resources/deployments/', 'ClusterSubnetRoleAssignmentDeployment')]"
                        ],
                        "properties": {
                            "kubernetesVersion": "[parameters('kubernetesVersion')]",
                            "enableRBAC": "[parameters('enableRBAC')]",
                            "dnsPrefix": "[parameters('dnsPrefix')]",
                            "addonProfiles": {
                                "httpApplicationRouting": {
                                    "enabled": "[parameters('enableHttpApplicationRouting')]"
                                }
                            },
                "masterProfile": {
                "count": "[parameters('masterCount')]",
                "vmSize": "[parameters('agentVMSize')]",
                "dnsPrefix": "[variables('mastersEndpointDNSNamePrefix')]"
                    },
                            "agentPoolProfiles": [
                                {
                                    "name": "agentpool",
                                    "osDiskSizeGB": "[parameters('osDiskSizeGB')]",
                                    "count": "[parameters('agentCount')]",
                                    "vmSize": "[parameters('agentVMSize')]",
                                    "osType": "[parameters('osType')]",
                                    "storageProfile": "ManagedDisks",
                                    "vnetSubnetID": "[concat(resourceGroup().id,'/providers/Microsoft.Network/virtualNetworks/',parameters('vnetName'),'/subnets/',parameters('subnetName'))]",
                                    "maxPods": "[parameters('maxPods')]",
                                    "enableAutoScaling": "[parameters('autoscalingEnabled')]",
                                    "minCount": "[parameters('nodesMinimum')]",
                                    "maxCount": "[parameters('nodesMaximum')]",
                                    "type": "VirtualMachineScaleSets"
                                }
                            ],
                "linuxProfile": {
                    "adminUsername": "[parameters('adminUsername')]",
                        "ssh": {
                          "publicKeys": [
                            {   
                              "keyData": "[parameters('sshRSAPublicKey')]"
                            }
                              ]
                            }
                    },
                            "servicePrincipalProfile": {
                                "clientId": "[parameters('ServicePrincipalClientId')]",
                                "secret": "[parameters('ServicePrincipalClientSecret')]"
                            },
                            "networkProfile": {
                                "networkPlugin": "[parameters('networkPlugin')]",
                                "serviceCidr": "[parameters('serviceCidr')]",
                                "dnsServiceIP": "[parameters('dnsServiceIP')]",
                                "dockerBridgeCidr": "[parameters('dockerBridgeCidr')]"
                            }
                        }
                    },
                    {
                        "type": "Microsoft.Resources/deployments",
                        "name": "ClusterSubnetRoleAssignmentDeployment",
                        "apiVersion": "2019-08-01",
                        "subscriptionId": "[subscription().subscriptionId]",
                        "resourceGroup": "[parameters('resourceGroup')]",
                        "dependsOn": [
                            "[concat('Microsoft.Network/virtualNetworks/', parameters('vnetName'))]"
                        ],
                        "properties": {
                            "mode": "Incremental",
                            "template": {
                                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
                                "contentVersion": "1.0.0.0",
                                "parameters": {},
                                "variables": {},
                                "resources": [
                                    {
                                        "type": "Microsoft.Network/virtualNetworks/subnets/providers/roleAssignments",
                                        "apiVersion": "2019-04-01-preview",
                                        "name": "[concat(parameters('vnetName'), '/', parameters('subnetName'), '/Microsoft.Authorization/', guid(resourceGroup().id, deployment().name))]",
                                        "properties": {
                                            "roleDefinitionId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '4d97b98b-1d4f-4787-a291-c67834d212e7')]",
                                            "principalId": "[parameters('ServicePrincipalObjectId')]",
                                            "scope": "[concat(resourceGroup().id,'/providers/Microsoft.Network/virtualNetworks/',parameters('vnetName'),'/subnets/',parameters('subnetName'))]"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                ]
              }
            }
        }

    ]
}

template2.json

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        Please copy from template1.json (due to characters limit I have removed)
    },
    "variables": {
    "mastersEndpointDNSNamePrefix":"[concat(parameters('dnsPrefix'),'mgmt')]",
        "vmssName":"aks-agentpool-43678249-vmss"
    },
    "resources": [
              {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2019-08-01",
            "name": "nestedTemplate",
            "properties": {
              "mode": "Incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "resources": [
                    {
                        "apiVersion": "2019-06-01",
                        "type": "Microsoft.Network/virtualNetworks",
                        "name": "[parameters('vnetName')]",
                        "location": "[parameters('location')]",
                        "properties": {
                            "addressSpace": {
                                "addressPrefixes": [
                                    "[parameters('vnetAddressPrefix')]"
                                ]
                            },
                            "subnets": [
                                {
                                    "name": "[parameters('subnetName')]",
                                    "properties": {
                                        "addressPrefix": "[parameters('subnetPrefix')]"
                                    }
                                }
                            ]
                        }
                   },
                   {
                        "type": "Microsoft.ContainerService/managedClusters",
                        "name":"[concat('Kluster-',resourceGroup().name)]",
                        "apiVersion": "2019-08-01",
                        "location": "[parameters('location')]",
                        "tags": {},
                        "dependsOn": [
                            "[concat('Microsoft.Resources/deployments/', 'ClusterSubnetRoleAssignmentDeployment')]"
                        ],
                        "properties": {
                            "kubernetesVersion": "[parameters('kubernetesVersion')]",
                            "enableRBAC": "[parameters('enableRBAC')]",
                            "dnsPrefix": "[parameters('dnsPrefix')]",
                            "addonProfiles": {
                                "httpApplicationRouting": {
                                    "enabled": "[parameters('enableHttpApplicationRouting')]"
                                }
                            },
                "masterProfile": {
                "count": "[parameters('masterCount')]",
                "vmSize": "[parameters('agentVMSize')]",
                "dnsPrefix": "[variables('mastersEndpointDNSNamePrefix')]"
                    },
                            "agentPoolProfiles": [
                                {
                                    "name": "agentpool",
                                    "osDiskSizeGB": "[parameters('osDiskSizeGB')]",
                                    "count": "[parameters('agentCount')]",
                                    "vmSize": "[parameters('agentVMSize')]",
                                    "osType": "[parameters('osType')]",
                                    "storageProfile": "ManagedDisks",
                                    "vnetSubnetID": "[concat(resourceGroup().id,'/providers/Microsoft.Network/virtualNetworks/',parameters('vnetName'),'/subnets/',parameters('subnetName'))]",
                                    "maxPods": "[parameters('maxPods')]",
                                    "enableAutoScaling": "[parameters('autoscalingEnabled')]",
                                    "minCount": "[parameters('nodesMinimum')]",
                                    "maxCount": "[parameters('nodesMaximum')]",
                                    "type": "virtualMachineScaleSets"
                                }
                            ],
                "linuxProfile": {
                    "adminUsername": "[parameters('adminUsername')]",
                        "ssh": {
                          "publicKeys": [
                            {   
                              "keyData": "[parameters('sshRSAPublicKey')]"
                            }
                              ]
                            }
                    },
                            "servicePrincipalProfile": {
                                "clientId": "[parameters('ServicePrincipalClientId')]",
                                "secret": "[parameters('ServicePrincipalClientSecret')]"
                            },
                            "networkProfile": {
                                "networkPlugin": "[parameters('networkPlugin')]",
                                "serviceCidr": "[parameters('serviceCidr')]",
                                "dnsServiceIP": "[parameters('dnsServiceIP')]",
                                "dockerBridgeCidr": "[parameters('dockerBridgeCidr')]"
                            }
                        },
                        "resources": [
                         {
                           "apiVersion": "2019-03-01",
                           "type": "Microsoft.Compute/virtualMachines/extensions",
                           "name": "[concat('aks-agentpool-43678249-vmss','/', 'Extensions')]",
                           "location": "[parameters('location')]",
                           "dependsOn": [
                               "[concat('Microsoft.Network/virtualNetworks/', parameters('vnetName'))]",
                               "[concat('Microsoft.ContainerService/managedClusters/', concat('Kluster-',resourceGroup().name))]"
                            ],
                           "properties": {
                               "publisher": "Microsoft.Azure.Extensions",
                               "type": "CustomScript",
                               "typeHandlerVersion": "2.0",
                               "autoUpgradeMinorVersion":true,
                               "settings": {
                                   "skipDos2Unix":false,
                                   "fileUris": ["filelink"],
                                   "commandToExecute": "./filename.sh"
                            }
                          }
                        }
                      ]
                    },
                    {
                        "type": "Microsoft.Resources/deployments",
                        "name": "ClusterSubnetRoleAssignmentDeployment",
                        "apiVersion": "2019-08-01",
                        "subscriptionId": "[subscription().subscriptionId]",
                        "resourceGroup": "[parameters('resourceGroup')]",
                        "dependsOn": [
                            "[concat('Microsoft.Network/virtualNetworks/', parameters('vnetName'))]"
                        ],
                        "properties": {
                            "mode": "Incremental",
                            "template": {
                                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
                                "contentVersion": "1.0.0.0",
                                "parameters": {},
                                "variables": {},
                                "resources": [
                                    {
                                        "type": "Microsoft.Network/virtualNetworks/subnets/providers/roleAssignments",
                                        "apiVersion": "2019-04-01-preview",
                                        "name": "[concat(parameters('vnetName'), '/', parameters('subnetName'), '/Microsoft.Authorization/', guid(resourceGroup().id, deployment().name))]",
                                        "properties": {
                                            "roleDefinitionId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '4d97b98b-1d4f-4787-a291-c67834d212e7')]",
                                            "principalId": "[parameters('ServicePrincipalObjectId')]",
                                            "scope": "[concat(resourceGroup().id,'/providers/Microsoft.Network/virtualNetworks/',parameters('vnetName'),'/subnets/',parameters('subnetName'))]"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                ]
              }
            }
        }
    ]
}
  

Error thrown : Parent resource vnetname not found as shown below

template1_params.json

    {
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "value": "aks-vnet"
    },
    "vnetAddressPrefix": {
      "value": "10.0.0.0/8"
    },
    "subnetName": {
      "value": "aks-subnet"
    },
    "subnetPrefix": {
      "value": "10.240.0.0/16"
    },
    "kubernetesVersion": {
      "value": "1.13.12"
    },
    "dnsPrefix": {
      "value": "test"
    },
    "agentCount": {
      "value": 2
    },
    "agentVMSize": {
      "value": "Standard_E2_v3"
    },
    "masterCount": {
      "value": 1
    },
    "ServicePrincipalClientId": {
      "value": "clientid..."
    },
    "ServicePrincipalClientSecret": {
      "value": "clientsecret..."
    },
    "ServicePrincipalObjectId": {
      "value": "objectid.."
    },
    "sshRSAPublicKey": {
      "value": "sshrsa-...."
    },
    "dnsServiceIP": {
      "value": "10.0.0.10"
    },
    "serviceCidr": {
      "value": "10.0.0.0/16"
    },
    "dockerBridgeCidr": {
      "value": "172.17.0.1/16"
    },
    "nodesMinimum": {
      "value": 2
    },
    "nodesMaximum": {
      "value": 4
    }
  }
}

1 个答案:

答案 0 :(得分:0)

对于您的问题,据我所知,模板中存在两个错误,也许不是全部。

一个是AKS集群中的resources块,它不是用于扩展,仅用于代理程序池。您可以在Microsoft.ContainerService/managedClusters object中了解其定义。

另一种方法是使用虚拟机规模集创建AKS群集,因此扩展名应为Microsoft.Compute/virtualMachineScaleSets/extensions类型。

也许您可以提供有关该问题的更多详细信息,以获得确切的解决方案。但是在此之前,您可以尝试更改模板以解决我在上面发现的错误。