Azure – Add and remove instances from azure load balancer programmatically

ansibleazuredeploymentload balancing

I'm new to Azure, and my team and me are setting a production environment in the Azure cloud, there will be a load balancer with a few instances behind, in the deployment process using Ansible I'd like to remove a instance from the balancer while updating the code, it is posible to do that programmatically using the azure cli or an api, and then add it back to the balancer when the deploy in the instance is done?

It can be done in the web interface but that will not work for the automated deployments.

What i have tried is to force the instance to appear unhealthy to the load balancer, setting the health-check end point to return 500 or something not 200, but I think is not the right way to do it.

Best Answer

Disabling the service should be enough because once the health check fails, the affected VM will be automatically "out" of the LB.

However, you can use PowerShell or CLI to add or remove servers from the backend pool.

ARM IaaS:

Get started creating an Internet facing load balancer in Resource Manager using PowerShell

ASM IaaS:

Get started creating an Internet facing load balancer (classic) in PowerShell