Create a custom HTTP/HTTPS health probe for Azure Load Balancer

In this article, you learn to create a custom API for HTTP health probes using Python, FLASK, and psutil. Health checks are performed on backend instances using HTTP GET and marked as healthy or unhealthy based on the response. The custom probe in this article marks instances as unhealthy if their CPU usage is over 75%. HTTP health probes can be used for many purposes, not just CPU utilization, when combine with your own logic and health checks.

Prerequisites

Important

Hourly pricing starts from the moment that Bastion is deployed, regardless of outbound data usage. For more information, see Pricing and SKUs. If you're deploying Bastion as part of a tutorial or test, we recommend that you delete this resource after you finish using it.

Configure API on virtual machine

In this section, you configure the virtual machine to run the custom API for the HTTP health probe.

  1. Connect to the VM using SSH or Azure Bastion. This article uses SSH to connect to the VM.

  2. Create a new folder to store the code for the health API, and enter the new folder:

    mkdir health_API && cd health_API
    
  3. Create a new python file and paste the following code into the file:

    touch health.py && vim health.py
    
    # Import libraries  
    from flask import Flask 
    from flask_restful import Resource, Api 
    import psutil 
    
    # Define app and API 
        app = Flask(__name__) 
        api = Api(app) 
    
    # Define API GET method 
    class check_CPU_VM(Resource): 
        def get(self):
            # If VM CPU utilization is over 75%, throw an error 
            if psutil.cpu_percent() >= 75.0:
                return '',408 
            # Else keep the VM as healthy 
            else: 
                return '',200 
    
    # Add the GET method to the API at the path 'health_check' 
    api.add_resource(check_CPU_VM, '/health_check/') 
    
    # Expose the API on all available IP address on the VM 
    if __name__ == "__main__":
        app.run(debug=True, host ="0.0.0.0") 
    
  4. Once you have copied the code into the file, install python3 and the required packages (flask, flask_restful, psutil) if necessary. The following commands install python3 and the required packages on Ubuntu:

    #Install Python
    sudo apt-get update
    sudo apt-get install python3
    sudo apt-get install python3-pip
    pip3 install flask flask_restful psutil
    
  5. Run the API using the following command:

    python3 health.py
    
  6. Once the API starts running, you see two IP addresses that are exposed to the API on port 5000.

    • The first IP address is the local IP address that is only available to the VM.
    • The second IP address is the private IP address of the VM, the Load Balancer’s health probe tests this IP address.

    Screenshot of output from running API for health probe.

Note

The API will need to be running on the VM for the health probe to work. When you close the SSH session, the API will stop running. Keep the window open while creating the health probe or run the API in the background.

Create health probe

In this section, you create the health probe used to check the health of the backend instances running the custom API.

  1. Navigate to the Azure portal and select the load balancer that you would like to add the health probe to.

  2. Select Health probes under Settings.

  3. Select + Add.

  4. In the Add Health Probe page, enter or select the following information:

    Setting Value
    Name Enter HTTP_Health
    Protocol Select HTTP
    Port Enter 5000
    Path Enter /health_check/
    Interval (seconds) Enter 5
  5. Select OK to create the health probe.

Create the load balancer rule

In this section, you create the load balancer rule that uses the HTTP health probe.

  1. From the load balancer overview page, select Load balancing rules under Settings.

  2. Select + Add.

  3. On the Add load balancing rule page, enter the following information:

    Setting Value
    Name Enter custom_HTTP_rule
    Frontend IP address Select the frontend IP address of your load balancer.
    Backend pool Select the backend pool that you want to use.
    Protocol Select TCP
    Port Enter 5000
    Backend port Enter 5000
    Health probe Select HTTP_Health (HTTP:5000/health_checkk/)
    Session persistence Select None
    Idle timeout (minutes) Enter 5

    Screenshot of Add load balancing rule with settings for custom health probe.

  4. Select Save to create the load balancing rule.

Verify health probe

In this section, you verify that the health probe is working as expected by checking the running API and the load balancer metrics.

  1. Navigate back to the SSH session to the VM running the API.

  2. In the console window that is running the API, you should see a GET request every 5 seconds checking the health of the VM, and responding with a 200 status code if the VM is healthy.

    Screenshot of API output with a healthy GET response of 200.

  3. Enter ctrl+c to stop the API.

  4. Close the SSH session to the VM.

  5. Navigate back to the load balancer overview page.

  6. Select Metrics under Monitoring.

  7. Select + Add metric and enter/select the following information:

    Setting Value
    Scope Select the load balancer to monitor.
    Metric Namespace Select Load balancer standard
    Metric Select Health Probe status
    Aggregation Select Max
  8. Select checkmark to add the metric.

    Screenshot of metrics chart with health probe status for load balancer.

Clean up resources

When no longer needed, delete the resource group, load balancer, and all related resources.

Next steps