虚拟机规模集是一种 Azure 计算资源,可用于部署和管理一组相同的 VM。 规模集中的所有 VM 采用相同的配置,无需对 VM 进行预配就可以进行自动横向扩展。 这样就可以更方便地构建面向大型计算、大型数据、容器化工作负荷的大规模服务。

如果用户需要通过一个带数据盘的自定义镜像来创建规模集,数据盘里可能存放用户定制应用,同时又希望办规模集置于应用程序网关之后,而不是简单的使用负载均衡服务,那么下面这段模板,可以帮助用户实现以上功能。

 

{

    "$schema": "http://schema.management.azure.com/schemas/2015-01-01-preview/deploymentTemplate.json",

    "contentVersion": "1.0.0.0",

    "parameters": {

        "vmSku": {

            "defaultValue": "Standard_A0",

            "type": "String",

            "metadata": {

                "description": "Size of VMs in the VM Scale Set."

            }

        },

        "sourceImageID": {

            "type": "String",

            "metadata": {

                "description": "The source Image ID, must be in the same region of the deployment."

            }

        },

        "vmssName": {

            "maxLength": 61,

            "type": "String",

            "metadata": {

                "description": "String used as a base for naming resources. Must be 3-61 characters in length and globally unique across Azure. A hash is prepended to this string for some resources, and resource-specific information is appended."

            }

        },

        "instanceCount": {

            "maxValue": 100,

            "type": "Int",

            "metadata": {

                "description": "Number of VM instances (100 or less)."

            }

        },

        "adminUsername": {

            "type": "String",

            "metadata": {

                "description": "Admin username on all VMs."

            }

        },

        "adminPassword": {

            "type": "SecureString",

            "metadata": {

                "description": "Admin password on all VMs."

            }

        },

        "existingSubnetResourceId": {

            "type": "String",

            "metadata": {

                "description": "Resource ID of the existing subnet to deploy the scale set into. Should be of the form: /subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP/providers/Microsoft.Network/virtualNetworks/YOUR_VNET_NAME/subnets/YOUR_SUBNET_NAME"

            }

        },

        "AppgwSubnetResourceId": {

            "type": "String",

            "metadata": {

                "description": "Resource ID of the existing subnet to deploy the scale set into. Should be of the form: /subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/YOUR_RESOURCE_GROUP/providers/Microsoft.Network/virtualNetworks/YOUR_VNET_NAME/subnets/YOUR_SUBNET_NAME"

            }

        }

    },

    "variables": {

        "storageAccountType": "Standard_LRS",

        "namingInfix": "[toLower(substring(concat(parameters('vmssName'), uniqueString(resourceGroup().id)), 0, 9))]",

        "wadnameInfix": "[concat(variables('namingInfix'), 'autoscalewad')]",

        "longNamingInfix": "[toLower(parameters('vmssName'))]",

        "newStorageAccountSuffix": "[concat(variables('namingInfix'), 'sa')]",

        "publicIPAddressName": "[concat(variables('namingInfix'), 'pip')]",

        "loadBalancerName": "[concat(variables('namingInfix'), 'appgw')]",

        "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",

        "lbID": "[resourceId('Microsoft.Network/applicationGateways',variables('loadBalancerName'))]",

        "natPoolName": "[concat(variables('namingInfix'), 'natpool')]",

        "bePoolName": "appGatewayBackendPool",

        "natStartPort": 50000,

        "natEndPort": 50119,

        "natBackendPort": 22,

        "nicName": "[concat(variables('namingInfix'), 'nic')]",

        "ipConfigName": "[concat(variables('namingInfix'), 'ipconfig')]",

        "frontEndIPConfigID": "[concat(variables('lbID'),'/frontendIPConfigurations/appGatewayFrontendIP')]",

        "diagnosticsStorageAccountName": "[concat(uniqueString(concat(resourceGroup().id, variables('newStorageAccountSuffix'))), variables('newStorageAccountSuffix'))]",

        "diagnosticsStorageAccountResourceGroup": "[resourceGroup().name]",

        "accountid": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('diagnosticsStorageAccountResourceGroup'),'/providers/','Microsoft.Storage/storageAccounts/', variables('diagnosticsStorageAccountName'))]",

        "wadlogs": "<WadCfg><DiagnosticMonitorConfiguration>",

        "wadperfcounters1": "<PerformanceCounters scheduledTransferPeriod=\"PT1M\"><PerformanceCounterConfiguration counterSpecifier=\"\\Memory\\AvailableMemory\" sampleRate=\"PT15S\" unit=\"Bytes\"><annotation displayName=\"Memory available\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Memory\\PercentAvailableMemory\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"Mem. percent available\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Memory\\UsedMemory\" sampleRate=\"PT15S\" unit=\"Bytes\"><annotation displayName=\"Memory used\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Memory\\PercentUsedMemory\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"Memory percentage\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Memory\\PercentUsedByCache\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"Mem. used by cache\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Processor\\PercentIdleTime\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"CPU idle time\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Processor\\PercentUserTime\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"CPU user time\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Processor\\PercentProcessorTime\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"CPU percentage guest OS\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\Processor\\PercentIOWaitTime\" sampleRate=\"PT15S\" unit=\"Percent\"><annotation displayName=\"CPU IO wait time\" locale=\"en-us\"/></PerformanceCounterConfiguration>",

        "wadperfcounters2": "<PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\BytesPerSecond\" sampleRate=\"PT15S\" unit=\"BytesPerSecond\"><annotation displayName=\"Disk total bytes\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\ReadBytesPerSecond\" sampleRate=\"PT15S\" unit=\"BytesPerSecond\"><annotation displayName=\"Disk read guest OS\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\WriteBytesPerSecond\" sampleRate=\"PT15S\" unit=\"BytesPerSecond\"><annotation displayName=\"Disk write guest OS\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\TransfersPerSecond\" sampleRate=\"PT15S\" unit=\"CountPerSecond\"><annotation displayName=\"Disk transfers\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\ReadsPerSecond\" sampleRate=\"PT15S\" unit=\"CountPerSecond\"><annotation displayName=\"Disk reads\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\WritesPerSecond\" sampleRate=\"PT15S\" unit=\"CountPerSecond\"><annotation displayName=\"Disk writes\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\AverageReadTime\" sampleRate=\"PT15S\" unit=\"Seconds\"><annotation displayName=\"Disk read time\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\AverageWriteTime\" sampleRate=\"PT15S\" unit=\"Seconds\"><annotation displayName=\"Disk write time\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\AverageTransferTime\" sampleRate=\"PT15S\" unit=\"Seconds\"><annotation displayName=\"Disk transfer time\" locale=\"en-us\"/></PerformanceCounterConfiguration><PerformanceCounterConfiguration counterSpecifier=\"\\PhysicalDisk\\AverageDiskQueueLength\" sampleRate=\"PT15S\" unit=\"Count\"><annotation displayName=\"Disk queue length\" locale=\"en-us\"/></PerformanceCounterConfiguration></PerformanceCounters>",

        "wadcfgxstart": "[concat(variables('wadlogs'),variables('wadperfcounters1'),variables('wadperfcounters2'),'<Metrics resourceId=\"')]",

        "wadmetricsresourceid": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',resourceGroup().name ,'/providers/','Microsoft.Compute/virtualMachineScaleSets/',variables('namingInfix'))]",

        "wadcfgxend": "[concat('\"><MetricAggregation scheduledTransferPeriod=\"PT1H\"/><MetricAggregation scheduledTransferPeriod=\"PT1M\"/></Metrics></DiagnosticMonitorConfiguration></WadCfg>')]",

        "computeApiVersion": "2016-04-30-preview",

        "networkApiVersion": "2016-03-30",

        "storageApiVersion": "2015-06-15",

        "insightsApiVersion": "2015-04-01"

    },

    "resources": [

        {

            "type": "Microsoft.Storage/storageAccounts",

            "name": "[variables('diagnosticsStorageAccountName')]",

            "apiVersion": "2015-06-15",

            "location": "[resourceGroup().location]",

            "properties": {

                "accountType": "[variables('storageAccountType')]"

            }

        },

        {

            "type": "Microsoft.Network/publicIPAddresses",

            "name": "[variables('publicIPAddressName')]",

            "apiVersion": "[variables('networkApiVersion')]",

            "location": "[resourceGroup().location]",

            "properties": {

                "publicIPAllocationMethod": "Dynamic",

                "dnsSettings": {

                    "domainNameLabel": "[variables('longNamingInfix')]"

                }

            }

        },

        {

            "type": "Microsoft.Network/applicationGateways",

            "name": "[variables('loadBalancerName')]",

            "apiVersion": "[variables('networkApiVersion')]",

            "location": "[resourceGroup().location]",

            "properties": {

                "sku": {

                    "name": "WAF_Medium",

                    "tier": "WAF",

                    "capacity": "1"

                },

                "gatewayIPConfigurations": [

                    {

                        "name": "appGatewayIpConfig",

                        "properties": {

                            "subnet": {

                                "id": "[parameters('AppgwSubnetResourceId')]"

                            }

                        }

                    }

                ],

                "frontendIPConfigurations": [

                    {

                        "name": "appGatewayFrontendIP",

                        "properties": {

                            "PublicIPAddress": {

                                "id": "[variables('publicIPAddressID')]"

                            }

                        }

                    }

                ],

                "frontendPorts": [

                    {

                        "name": "appGatewayFrontendPort",

                        "properties": {

                            "Port": 80

                        }

                    }

                ],

                "backendAddressPools": [

                    {

                        "name": "[variables('bePoolName')]"

                    }

                ],

                "backendHttpSettingsCollection": [

                    {

                        "name": "appGatewayBackendHttpSettings",

                        "properties": {

                            "Port": 80,

                            "Protocol": "Http",

                            "CookieBasedAffinity": "Disabled"

                        }

                    }

                ],

                "httpListeners": [

                    {

                        "name": "appGatewayHttpListener",

                        "properties": {

                            "FrontendIPConfiguration": {

                                "Id": "[concat(variables('lbID'), '/frontendIPConfigurations/appGatewayFrontendIP')]"

                            },

                            "FrontendPort": {

                                "Id": "[concat(variables('lbID'), '/frontendPorts/appGatewayFrontendPort')]"

                            },

                            "Protocol": "Http",

                            "SslCertificate": null

                        }

                    }

                ],

                "requestRoutingRules": [

                    {

                        "Name": "rule1",

                        "properties": {

                            "RuleType": "Basic",

                            "httpListener": {

                                "id": "[concat(variables('lbID'), '/httpListeners/appGatewayHttpListener')]"

                            },

                            "backendAddressPool": {

                                "id": "[concat(variables('lbID'), '/backendAddressPools/appGatewayBackendPool')]"

                            },

                            "backendHttpSettings": {

                                "id": "[concat(variables('lbID'), '/backendHttpSettingsCollection/appGatewayBackendHttpSettings')]"

                            }

                        }

                    }

                ]

            },

            "dependsOn": [

                "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"

            ]

        },

        {

            "type": "Microsoft.Compute/virtualMachineScaleSets",

            "sku": {

                "name": "[parameters('vmSku')]",

                "tier": "Standard",

                "capacity": "[parameters('instanceCount')]"

            },

            "name": "[variables('namingInfix')]",

            "apiVersion": "[variables('computeApiVersion')]",

            "location": "[resourceGroup().location]",

            "properties": {

                "overprovision": "true",

                "upgradePolicy": {

                    "mode": "Manual"

                },

                "virtualMachineProfile": {

                    "storageProfile": {

                        "osDisk": {

                            "createOption": "FromImage",

                            "managedDisk": {

                                "storageAccountType": "Standard_LRS"

                            }

                        },

                        "imageReference": {

                            "id": "[parameters('sourceImageID')]"

                        }

                    },

                    "osProfile": {

                        "computerNamePrefix": "[variables('namingInfix')]",

                        "adminUsername": "[parameters('adminUsername')]",

                        "adminPassword": "[parameters('adminPassword')]"

                    },

                    "networkProfile": {

                        "networkInterfaceConfigurations": [

                            {

                                "name": "[variables('nicName')]",

                                "properties": {

                                    "primary": "true",

                                    "ipConfigurations": [

                                        {

                                            "name": "[variables('ipConfigName')]",

                                            "properties": {

                                                "subnet": {

                                                    "id": "[parameters('existingSubnetResourceId')]"

                                                },

                                                "applicationGatewayBackendAddressPools": [

                                                    {

                                                        "id": "[concat('/subscriptions/', subscription().subscriptionId,'/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Network/applicationGateways/', variables('loadBalancerName'), '/backendAddressPools/', variables('bePoolName'))]"

                                                    }

                                                ]

                                            }

                                        }

                                    ]

                                }

                            }

                        ]

                    },

                    "extensionProfile": {

                        "extensions": [

                            {

                                "name": "LinuxDiagnostic",

                                "properties": {

                                    "publisher": "Microsoft.OSTCExtensions",

                                    "type": "LinuxDiagnostic",

                                    "typeHandlerVersion": "2.3",

                                    "autoUpgradeMinorVersion": true,

                                    "settings": {

                                        "xmlCfg": "[base64(concat(variables('wadcfgxstart'),variables('wadmetricsresourceid'),variables('wadcfgxend')))]",

                                        "storageAccount": "[variables('diagnosticsStorageAccountName')]"

                                    },

                                    "protectedSettings": {

                                        "storageAccountName": "[variables('diagnosticsStorageAccountName')]",

                                        "storageAccountKey": "[listkeys(variables('accountid'), variables('storageApiVersion')).key1]",

                                        "storageAccountEndPoint": "https://core.chinacloudapi.cn"

                                    }

                                }

                            }

                        ]

                    }

                }

            },

            "dependsOn": [

                "[concat('Microsoft.Storage/storageAccounts/', variables('diagnosticsStorageAccountName'))]",

                "[concat('Microsoft.Network/applicationGateways/', variables('loadBalancerName'))]"

            ]

        },

        {

            "type": "Microsoft.Insights/autoscaleSettings",

            "name": "[variables('wadnameInfix')]",

            "apiVersion": "[variables('insightsApiVersion')]",

            "location": "[resourceGroup().location]",

            "properties": {

                "name": "[variables('wadnameInfix')]",

                "targetResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/',  resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', variables('namingInfix'))]",

                "enabled": true,

                "profiles": [

                    {

                        "name": "Profile1",

                        "capacity": {

                            "minimum": "1",

                            "maximum": "100",

                            "default": "1"

                        },

                        "rules": [

                            {

                                "metricTrigger": {

                                    "metricName": "\\Processor\\PercentProcessorTime",

                                    "metricNamespace": "",

                                    "metricResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/',  resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', variables('namingInfix'))]",

                                    "timeGrain": "PT1M",

                                    "statistic": "Average",

                                    "timeWindow": "PT5M",

                                    "timeAggregation": "Average",

                                    "operator": "GreaterThan",

                                    "threshold": 50

                                },

                                "scaleAction": {

                                    "direction": "Increase",

                                    "type": "ChangeCount",

                                    "value": "1",

                                    "cooldown": "PT1M"

                                }

                            },

                            {

                                "metricTrigger": {

                                    "metricName": "\\Processor\\PercentProcessorTime",

                                    "metricNamespace": "",

                                    "metricResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/',  resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', variables('namingInfix'))]",

                                    "timeGrain": "PT1M",

                                    "statistic": "Average",

                                    "timeWindow": "PT5M",

                                    "timeAggregation": "Average",

                                    "operator": "LessThan",

                                    "threshold": 30

                                },

                                "scaleAction": {

                                    "direction": "Decrease",

                                    "type": "ChangeCount",

                                    "value": "1",

                                    "cooldown": "PT5M"

                                }

                            }

                        ]

                    }

                ]

            },

            "dependsOn": [

                "[concat('Microsoft.Compute/virtualMachineScaleSets/', variables('namingInfix'))]"

            ]

        }

    ]

}

 

请注意,由于目前Portal功能限制,用户是无法修改规模集在应用程序网关中的设置的,

 

 

posted on 2018-04-05 16:22  johntoo  阅读(154)  评论(0编辑  收藏  举报