Deploying an Auto-Scaling VM Scale Set with a Custom Disk Image
With the disk image location on hand; I was able to use an ARM template to deploy my scale set while specifying my custom disk image.
Creating the Scale Set Template
Although I was a coder at one time and know my way around ARM templates a bit, I will be the first to admit that I am not an ARM template expert. Lucky for me, there are scale set ARM templates published all over the internet. All I had to do was pull one down and modify it for my needs.
After pulling down this scale set ARM template, I went ahead and made a few minor modifications:
- Added custom disk reference (see orange code below)
- Added autoscale code (see red code below)
- Changed name reference (just some template cleanup)
- Changed instance values (see blue code below)
The complete template, including the autoscale settings is below:
- {
- "$schema": "http://schema.management.azure.com/schemas/2015-01-01-preview/deploymentTemplate.json",
- "contentVersion": "1.0.0.0",
- "parameters": {
- "vmSku": {
- "defaultValue": "Standard_DS1_V2",
- "type": "String",
- "metadata": {
- "description": "Size of VMs in the VM Scale Set."
- }
- },
- "vmssName": {
- "type": "string",
- "metadata": {
- "description": "Name of the VM scale set."
- }
- },
- "instanceCount": {
- "maxValue": 3,
- "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."
- }
- },
- "osType": {
- "allowedValues": [
- "Windows",
- "Linux"
- ],
- "type": "String"
- }
- },
- "variables": {
- "namingInfix": "[toLower(substring(concat(parameters('vmssName'), uniqueString(resourceGroup().id)), 0, 9))]",
- "longNamingInfix": "[toLower(parameters('vmssName'))]",
- "vhdContainerName": "[concat(variables('namingInfix'), 'vhd')]",
- "addressPrefix": "10.0.0.0/16",
- "subnetPrefix": "10.0.0.0/24",
- "virtualNetworkName": "[concat(variables('namingInfix'), 'vnet')]",
- "publicIPAddressName": "[concat(variables('namingInfix'), 'pip')]",
- "subnetName": "[concat(variables('namingInfix'), 'subnet')]",
- "loadBalancerName": "[concat(variables('namingInfix'), 'lb')]",
- "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
- "lbID": "[resourceId('Microsoft.Network/loadBalancers',variables('loadBalancerName'))]",
- "natPoolName": "[concat(variables('namingInfix'), 'natpool')]",
- "bePoolName": "[concat(variables('namingInfix'), 'bepool')]",
- "natStartPort": 50000,
- "natEndPort": 50119,
- "natBackendPort": 22,
- "nicName": "[concat(variables('namingInfix'), 'nic')]",
- "ipConfigName": "[concat(variables('namingInfix'), 'ipconfig')]",
- "frontEndIPConfigID": "[concat(variables('lbID'),'/frontendIPConfigurations/loadBalancerFrontEnd')]",
- "computeApiVersion": "2016-04-30-preview",
- "networkApiVersion": "2016-03-30",
- "insightsApiVersion": "2015-04-01",
- "location": "[resourceGroup().location]"
- },
- "resources": [
- {
- "type": "Microsoft.Network/virtualNetworks",
- "name": "[variables('virtualNetworkName')]",
- "apiVersion": "[variables('networkApiVersion')]",
- "location": "[resourceGroup().location]",
- "properties": {
- "addressSpace": {
- "addressPrefixes": [
- "[variables('addressPrefix')]"
- ]
- },
- "subnets": [
- {
- "name": "[variables('subnetName')]",
- "properties": {
- "addressPrefix": "[variables('subnetPrefix')]"
- }
- }
- ]
- }
- },
- {
- "type": "Microsoft.Network/publicIPAddresses",
- "name": "[variables('publicIPAddressName')]",
- "apiVersion": "[variables('networkApiVersion')]",
- "location": "[resourceGroup().location]",
- "properties": {
- "publicIPAllocationMethod": "Dynamic",
- "dnsSettings": {
- "domainNameLabel": "[variables('longNamingInfix')]"
- }
- }
- },
- {
- "type": "Microsoft.Network/loadBalancers",
- "name": "[variables('loadBalancerName')]",
- "apiVersion": "[variables('networkApiVersion')]",
- "location": "[resourceGroup().location]",
- "properties": {
- "frontendIPConfigurations": [
- {
- "name": "LoadBalancerFrontEnd",
- "properties": {
- "publicIPAddress": {
- "id": "[variables('publicIPAddressID')]"
- }
- }
- }
- ],
- "backendAddressPools": [
- {
- "name": "[variables('bePoolName')]"
- }
- ],
- "inboundNatPools": [
- {
- "name": "[variables('natPoolName')]",
- "properties": {
- "frontendIPConfiguration": {
- "id": "[variables('frontEndIPConfigID')]"
- },
- "protocol": "tcp",
- "frontendPortRangeStart": "[variables('natStartPort')]",
- "frontendPortRangeEnd": "[variables('natEndPort')]",
- "backendPort": "[variables('natBackendPort')]"
- }
- }
- ]
- },
- "dependsOn": [
- "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
- ]
- },
- {
- "type": "Microsoft.Compute/virtualMachineScaleSets",
- "sku": {
- "name": "[parameters('vmSku')]",
- "tier": "Standard",
- "capacity": "[parameters('instanceCount')]"
- },
- "name": "[parameters('vmssName')]",
- "apiVersion": "[variables('computeApiVersion')]",
- "location": "[resourceGroup().location]",
- "properties": {
- "overprovision": "true",
- "upgradePolicy": {
- "mode": "Manual"
- },
- "virtualMachineProfile": {
- "storageProfile": {
- "imageReference": {
- "id": "/subscriptions/9cbf5438-a4a1-4227-8a0d-18e92fe2e472/resourceGroups/vmssdemo/providers/Microsoft.Compute/images/GoldImage"
- }
- },
- "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": "[concat('/subscriptions/', subscription().subscriptionId,'/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'), '/subnets/', variables('subnetName'))]"
- },
- "loadBalancerBackendAddressPools": [
- {
- "id": "[concat('/subscriptions/', subscription().subscriptionId,'/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Network/loadBalancers/', variables('loadBalancerName'), '/backendAddressPools/', variables('bePoolName'))]"
- }
- ],
- "loadBalancerInboundNatPools": [
- {
- "id": "[concat('/subscriptions/', subscription().subscriptionId,'/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Network/loadBalancers/', variables('loadBalancerName'), '/inboundNatPools/', variables('natPoolName'))]"
- }
- ]
- }
- }
- ]
- }
- }
- ]
- }
- }
- },
- "dependsOn": [
- "[concat('Microsoft.Network/loadBalancers/', variables('loadBalancerName'))]",
- "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
- ]
- },
- {
- "type": "Microsoft.Insights/autoscaleSettings",
- "apiVersion": "[variables('insightsApiVersion')]",
- "name": "autoscalehosts",
- "location": "[variables('location')]",
- "dependsOn": [
- "[concat('Microsoft.Compute/virtualMachineScaleSets/', parameters('vmssName'))]"
- ],
- "properties": {
- "enabled": true,
- "name": "autoscalehosts",
- "profiles": [
- {
- "name": "Profile1",
- "capacity": {
- "minimum": "1",
- "maximum": "3",
- "default": "1"
- },
- "rules": [
- {
- "metricTrigger": {
- "metricName": "Percentage CPU",
- "metricNamespace": "",
- "metricResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', parameters('vmSSName'))]",
- "timeGrain": "PT1M",
- "statistic": "Average",
- "timeWindow": "PT5M",
- "timeAggregation": "Average",
- "operator": "GreaterThan",
- "threshold": 60.0
- },
- "scaleAction": {
- "direction": "Increase",
- "type": "ChangeCount",
- "value": "1",
- "cooldown": "PT1M"
- }
- },
- {
- "metricTrigger": {
- "metricName": "Percentage CPU",
- "metricNamespace": "",
- "metricResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', parameters('vmSSName'))]",
- "timeGrain": "PT1M",
- "statistic": "Average",
- "timeWindow": "PT5M",
- "timeAggregation": "Average",
- "operator": "LessThan",
- "threshold": 30.0
- },
- "scaleAction": {
- "direction": "Decrease",
- "type": "ChangeCount",
- "value": "1",
- "cooldown": "PT1M"
- }
- }
- ]
- }
- ],
- "targetResourceUri": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', parameters('vmSSName'))]"
- }
- }
- ]
- }
Click here to join the Understanding Azure Facebook group or here for the latest Azure practice questions, answers, explanations, and reference materials.