{"id":7121,"date":"2016-05-31T19:12:56","date_gmt":"2016-05-31T17:12:56","guid":{"rendered":"https:\/\/blog.redbaronofazure.com\/?p=7121"},"modified":"2016-05-31T20:24:27","modified_gmt":"2016-05-31T18:24:27","slug":"getting-a-vm-out-the-azure-load-balancer-pool","status":"publish","type":"post","link":"https:\/\/blog.redbaronofazure.com\/?p=7121","title":{"rendered":"Getting a VM out the Azure Load Balancer pool"},"content":{"rendered":"<p>When it&#8217;s time for maintenance you need to take your VMs out of rotation in the Azure Load Balancer backend pool. If you&#8217;ve deployed your solution via the portal, a JSON Template or via ARM scripting, you know that there are many moving parts involved. There is no API specific for this and what you have to do is unbind the network interface card (NIC) from the backend address pool. Script wise, this is not a one-liner although not as complex as you might think at first.<\/p>\n<p><strong>Steps to remove a VM from the Load Balancer pool<\/strong><\/p>\n<p>First step is to find the NIC on the VM. Assuming your VM only have one NIC this is not that complicated. Once you have the NIC, you just update it and discard the binding to the load balancer.<\/p>\n<p>In Azure CLI code this would be as below. You name the NIC and you pass in nothing for the parameter &#8211;lb-address-pool-ids.<\/p>\n<pre class=\"theme:classic lang:default decode:true \">azure network nic set -g resourceGroup -n nickname --lb-address-pool-ids<\/pre>\n<p>In PowerShell this is bit more complex as you have to nullify the property instead<\/p>\n<pre class=\"theme:classic lang:default decode:true\">$nic = Get-AzureRmNetworkInterface -ResourceGroupName $rgName -Name $nicName\r\n\r\n$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $null\r\n\r\nSet-AzureRmNetworkInterface -NetworkInterface $nic<\/pre>\n<p>Bear in mind that the CLI version expects a resource-id for it&#8217;s parameter value and not just the name. The resource-id is the long path containing the subscription-guid, etc.<\/p>\n<p><strong>Steps to add a VM to the Load Balancer pool<\/strong><\/p>\n<p>Once you are done with your maintenance of your VM it is time to put it back in the pool. This is basically no different from the above steps except this time we need to supply a value of the load balancer&#8217;s backend pool instead of nullifying it.<\/p>\n<p>In Azure CLI on a Mac\/Linux, this becomes a somewhat tricky task if we want to stick with using just a bash script. The CLI can emit JSON, which makes parsing it simpler, but bash has no build in JSON parser functionality, so for this I use jq (see refs for Github).<\/p>\n<pre class=\"theme:classic lang:default decode:true\"> # get vm nic id\r\nnicID=$(azure vm show -g $resourceGroupName -n $vmName --json | jq '.networkProfile.networkInterfaces[0].id')\r\n# remove double quotes at start\/end\r\nnicID=\"${nicID%\\\"}\"\r\nnicID=\"${nicID#\\\"}\"\r\n# get just the name and not the long resourceId\r\nnicName=$(echo $nicID | cut -f9 -d '\/')\r\n\r\n# get id of LB BE pool\r\nlbbeID=$(azure network lb address-pool list  -g $resourceGroupName -l $loadBalancerName --json | jq '.[0].id')\r\n# remove double quotes at start\/end\r\nlbbeID=\"${lbbeID%\\\"}\"\r\nlbbeID=\"${lbbeID#\\\"}\"\r\n\r\n# add nic to lb\r\nazure network nic set -g $resourceGroupName -n $nicName --lb-address-pool-ids $lbbeID\r\n<\/pre>\n<p>In PowerShell this is cleaner\u00a0and means looking up the LoadBalancer and finding it&#8217;s backend address pool to the NIC.<\/p>\n<pre class=\"theme:classic lang:default decode:true \">$nic = Get-AzureRmNetworkInterface -ResourceGroupName $rgName -Name $nicName\r\n\r\n$lb = Get-AzureRmLoadBalancer -Name $LoadBalancerName -ResourceGroupName $rgName\r\n\r\n$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $lb.BackendAddressPools\r\n\r\nSet-AzureRmNetworkInterface -NetworkInterface $nic\r\n<\/pre>\n<p><strong>Putting it together<\/strong><\/p>\n<p>In my github repo, I&#8217;ve added small bash and PowerShell scripts that carries out the operation of adding and removing a VM from the load balancer.<\/p>\n<p>Removing a VM script wise from the Load Balancer\u00a0<a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-remove.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7122\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-remove.png\" alt=\"es-lb-remove\" width=\"822\" height=\"422\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-remove.png 822w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-remove-300x154.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-remove-768x394.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/p>\n<p>Adding the VM back to the Load Balancer pool<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-add.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-7123\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-add.png\" alt=\"es-lb-add\" width=\"799\" height=\"463\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-add.png 799w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-add-300x174.png 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2016\/05\/es-lb-add-768x445.png 768w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/a><\/p>\n<p><strong>References<\/strong><\/p>\n<p>JSON parser on Github<br \/>\n<a href=\"https:\/\/stedolan.github.io\/jq\/\">https:\/\/stedolan.github.io\/jq\/<\/a><\/p>\n<p>Script Github repo<br \/>\n<a href=\"https:\/\/github.com\/cljung\/az-search-cluster\">https:\/\/github.com\/cljung\/az-search-cluster<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it&#8217;s time for maintenance you need to take your VMs out of rotation in the Azure Load Balancer backend pool. If you&#8217;ve deployed your solution via the portal, a JSON Template or via ARM scripting, you know that there are many moving parts involved. There is no API specific for this and what you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[91,131,121,141],"tags":[398,399,71],"_links":{"self":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7121"}],"collection":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7121"}],"version-history":[{"count":4,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7121\/revisions"}],"predecessor-version":[{"id":7127,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/7121\/revisions\/7127"}],"wp:attachment":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}