Create a custom HTTP/HTTPS health probe for Azure Load Balancer - Azure Load Balancer (2024)

  • Article

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

  • An Azure account with an active subscription. Create an account for free and access to the Azure portal.
  • An existing standard SKU Azure Load Balancer. For more information on creating a load balancer, see Create a public load balancer using the Azure portal.
  • An Azure Virtual Machine running linux in the backend pool of the Azure Load Balancer, see Create a virtual machine using the Azure portal.
  • Linux virtual machine has python3, pip and the following packages installed:
    • flask
    • flask_restful
    • psutil
  • Remote access to the virtual machine via SSH or Azure Bastion.

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 Pythonsudo apt-get updatesudo apt-get install python3sudo apt-get install python3-pippip3 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.

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:

    SettingValue
    NameEnter HTTP_Health
    ProtocolSelect HTTP
    PortEnter 5000
    PathEnter /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:

    SettingValue
    NameEnter custom_HTTP_rule
    Frontend IP addressSelect the frontend IP address of your load balancer.
    Backend poolSelect the backend pool that you want to use.
    ProtocolSelect TCP
    PortEnter 5000
    Backend portEnter 5000
    Health probeSelect HTTP_Health (HTTP:5000/health_checkk/)
    Session persistenceSelect None
    Idle timeout (minutes)Enter 5

    Create a custom HTTP/HTTPS health probe for Azure Load Balancer - Azure Load Balancer (2)

  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.

    Create a custom HTTP/HTTPS health probe for Azure Load Balancer - Azure Load Balancer (3)

  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:

    SettingValue
    ScopeSelect the load balancer to monitor.
    Metric NamespaceSelect Load balancer standard
    MetricSelect Health Probe status
    AggregationSelect Max
  8. Select checkmark to add the metric.

    Create a custom HTTP/HTTPS health probe for Azure Load Balancer - Azure Load Balancer (4)

Clean up resources

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

Next steps

Manage health probes for Azure Load Balancer using the Azure portal

Create a custom HTTP/HTTPS health probe for Azure Load Balancer - Azure Load Balancer (2024)
Top Articles
Gnocchi vs. Pasta: Is Gnocchi a Healthy Alternative to Pasta?
Key advantages and risks of investing in international funds - CNBC TV18
Nullreferenceexception 7 Days To Die
Umbc Baseball Camp
AllHere, praised for creating LAUSD’s $6M AI chatbot, files for bankruptcy
Exam With A Social Studies Section Crossword
Wausau Marketplace
Here's how eating according to your blood type could help you keep healthy
Needle Nose Peterbilt For Sale Craigslist
South Bend Tribune Online
Nj Scratch Off Remaining Prizes
Mephisto Summoners War
Huge Boobs Images
Fear And Hunger 2 Irrational Obelisk
Suffix With Pent Crossword Clue
Nutrislice Menus
Diamond Piers Menards
Marvon McCray Update: Did He Pass Away Or Is He Still Alive?
3S Bivy Cover 2D Gen
Jellyfin Ps5
Mission Impossible 7 Showtimes Near Marcus Parkwood Cinema
The Menu Showtimes Near Regal Edwards Ontario Mountain Village
Craigslist Pearl Ms
Purdue 247 Football
8005607994
Valic Eremit
Sandals Travel Agent Login
Amerisourcebergen Thoughtspot 2023
Saxies Lake Worth
Meijer Deli Trays Brochure
2021 Tesla Model 3 Standard Range Pl electric for sale - Portland, OR - craigslist
Craig Woolard Net Worth
Staar English 1 April 2022 Answer Key
Tirage Rapid Georgia
Wisconsin Women's Volleyball Team Leaked Pictures
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Joey Gentile Lpsg
Natasha Tosini Bikini
705 Us 74 Bus Rockingham Nc
Frequently Asked Questions
News & Events | Pi Recordings
Laura Houston Wbap
Research Tome Neltharus
The Hardest Quests in Old School RuneScape (Ranked) – FandomSpot
Craigslist Cars For Sale By Owner Memphis Tn
Sams La Habra Gas Price
What your eye doctor knows about your health
O'reilly's On Marbach
2121 Gateway Point
Equinox Great Neck Class Schedule
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6160

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.