Skip to content

Commit 6fe8535

Browse files
Merge pull request #244015 from mbender-ms/lb-newdoc-health-probes
load balancer - New Doc - Create Custom HTTP/HTTPS Health Probe
2 parents 64412d5 + 6b84117 commit 6fe8535

File tree

7 files changed

+184
-2
lines changed

7 files changed

+184
-2
lines changed

articles/load-balancer/TOC.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,16 @@
144144
href: /security/benchmark/azure/baselines/azure-load-balancer-security-baseline?toc=/azure/load-balancer/TOC.json
145145
- name: How-to
146146
items:
147-
- name: Manage health probes
148-
href: manage-probes-how-to.md
149147
- name: Backend Pool management
150148
href: backend-pool-management.md
151149
- name: Retrieve metadata using the Azure IMDS
152150
href: howto-load-balancer-imds.md
151+
- name: Health probes
152+
items:
153+
- name: Manage health probes
154+
href: manage-probes-how-to.md
155+
- name: Create a custom HTTP/HTTPS health probe
156+
href: create-custom-http-health-probe-howto.md
153157
- name: Load Balancer metrics
154158
items:
155159
- name: Get Load Balancer metrics with Azure Monitor CLI
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: Create a custom HTTP/HTTPS health probe for Azure Load Balancer
3+
titleSuffix: Azure Load Balancer
4+
description: Learn to create a custom HTTP/HTTPS health probe for Azure Load Balancer using python and FLASK restful server library.
5+
services: load-balancer
6+
author: mbender-ms
7+
ms.service: load-balancer
8+
ms.topic: troubleshooting
9+
ms.date: 05/22/2023
10+
ms.author: mbender
11+
---
12+
13+
# Create a custom HTTP/HTTPS health probe for Azure Load Balancer
14+
15+
In this article, you learn to create a custom API for HTTP [health probes](load-balancer-custom-probe-overview.md) 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.
16+
17+
## Prerequisites
18+
19+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) and access to the Azure portal.
20+
- 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](quickstart-load-balancer-standard-public-portal.md).
21+
- An Azure Virtual Machine running linux in the backend pool of the Azure Load Balancer, see [Create a virtual machine using the Azure portal](../virtual-machines/linux/quick-create-portal.md).
22+
- Linux virtual machine has *python3*, *pip* and the following packages installed:
23+
- *flask*
24+
- *flask_restful*
25+
- *psutil*
26+
- Remote access to the virtual machine via SSH or Azure Bastion.
27+
28+
> [!IMPORTANT]
29+
> [!INCLUDE [Pricing](../../includes/bastion-pricing.md)]
30+
>
31+
32+
## Configure API on virtual machine
33+
34+
In this section, you configure the virtual machine to run the custom API for the HTTP health probe.
35+
36+
1. Connect to the VM using SSH or Azure Bastion. This article uses SSH to connect to the VM.
37+
1. Create a new folder to store the code for the health API, and enter the new folder:
38+
39+
```bash
40+
mkdir health_API && cd health_API
41+
```
42+
43+
1. Create a new python file and paste the following code into the file:
44+
45+
```bash
46+
touch health.py && vim health.py
47+
```
48+
49+
```python
50+
# Import libraries
51+
from flask import Flask
52+
from flask_restful import Resource, Api
53+
import psutil
54+
55+
# Define app and API
56+
app = Flask(__name__)
57+
api = Api(app)
58+
59+
# Define API GET method
60+
class check_CPU_VM(Resource):
61+
def get(self):
62+
# If VM CPU utilization is over 75%, throw an error
63+
if psutil.cpu_percent() >= 75.0:
64+
return '',408
65+
        # Else keep the VM as healthy
66+
        else:
67+
            return '',200
68+
69+
# Add the GET method to the API at the path 'health_check'
70+
api.add_resource(check_CPU_VM, '/health_check/')
71+
72+
# Expose the API on all available IP address on the VM
73+
if __name__ == "__main__":
74+
app.run(debug=True, host ="0.0.0.0")
75+
```
76+
1. 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:
77+
78+
```bash
79+
#Install Python
80+
sudo apt-get update
81+
sudo apt-get install python3
82+
sudo apt-get install python3-pip
83+
pip3 install flask flask_restful psutil
84+
```
85+
86+
1. Run the API using the following command:
87+
88+
```bash
89+
python3 health.py
90+
```
91+
92+
1. Once the API starts running, you see two IP addresses that are exposed to the API on port **5000**.
93+
- The first IP address is the local IP address that is only available to the VM.
94+
- The second IP address is the private IP address of the VM, the Load Balancer’s health probe tests this IP address.
95+
96+
:::image type="content" source="media/create-custom-health-probe-howto/running-api-output-thumb.png" alt-text="Screenshot of output from running API for health probe." lightbox="media/create-custom-health-probe-howto/running-api-output.png":::
97+
98+
> [!NOTE]
99+
> 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.
100+
101+
## Create health probe
102+
103+
In this section, you create the health probe used to check the health of the backend instances running the custom API.
104+
105+
1. Navigate to the Azure portal and select the load balancer that you would like to add the health probe to.
106+
1. Select **Health probes** under **Settings**.
107+
1. Select **+ Add**.
108+
1. In the **Add Health Probe** page, enter or select the following information:
109+
110+
| **Setting** | **Value** |
111+
| --- | --- |
112+
| **Name** | Enter **HTTP_Health** |
113+
| **Protocol** | Select **HTTP** |
114+
| **Port** | Enter **5000** |
115+
| **Path** | Enter **/health_check/** |
116+
| **Interval (seconds)** | Enter **5** |
117+
118+
1. Select **OK** to create the health probe.
119+
120+
## Create the load balancer rule
121+
122+
In this section, you create the load balancer rule that uses the HTTP health probe.
123+
124+
1. From the load balancer overview page, select **Load balancing rules** under **Settings**.
125+
1. Select **+ Add**.
126+
1. On the **Add load balancing rule** page, enter the following information:
127+
128+
| **Setting** | **Value** |
129+
| --- | --- |
130+
| **Name** | Enter **custom_HTTP_rule** |
131+
| **Frontend IP address** | Select the frontend IP address of your load balancer. |
132+
| **Backend pool** | Select the backend pool that you want to use. |
133+
| **Protocol** | Select **TCP** |
134+
| **Port** | Enter **5000** |
135+
| **Backend port** | Enter **5000** |
136+
| **Health probe** | Select **HTTP_Health (HTTP:5000/health_checkk/)** |
137+
| **Session persistence** | Select **None** |
138+
| **Idle timeout (minutes)** | Enter **5** |
139+
140+
:::image type="content" source="media/create-custom-health-probe-howto/add-load-balancing-rule.png" alt-text="Screenshot of Add load balancing rule with settings for custom health probe.":::
141+
142+
1. Select **Save** to create the load balancing rule.
143+
144+
## Verify health probe
145+
146+
In this section, you verify that the health probe is working as expected by checking the running API and the load balancer metrics.
147+
148+
1. Navigate back to the SSH session to the VM running the API.
149+
1. 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.
150+
151+
:::image type="content" source="media/create-custom-health-probe-howto/api-output-healthy-virtual-machine.png" alt-text="Screenshot of API output with a healthy GET response of 200.":::
152+
153+
1. Enter **ctrl+c** to stop the API.
154+
1. Close the SSH session to the VM.
155+
1. Navigate back to the load balancer overview page.
156+
1. Select **Metrics** under **Monitoring**.
157+
1. Select **+ Add metric** and enter/select the following information:
158+
159+
| **Setting** | **Value** |
160+
| --- | --- |
161+
| **Scope** | Select the load balancer to monitor. |
162+
| **Metric Namespace** | Select **Load balancer standard** |
163+
| **Metric** | Select **Health Probe status** |
164+
| **Aggregation** | Select **Max** |
165+
166+
1. Select **checkmark** to add the metric.
167+
168+
:::image type="content" source="media/create-custom-health-probe-howto/load-balancer-metrics.png" alt-text="Screenshot of metrics chart with health probe status for load balancer.":::
169+
170+
## Clean up resources
171+
172+
When no longer needed, delete the resource group, load balancer, and all related resources.
173+
174+
## Next steps
175+
176+
> [!div class="nextstepaction"]
177+
> [Manage health probes for Azure Load Balancer using the Azure portal](manage-probes-how-to.md)
178+
67.7 KB
Loading
44.9 KB
Loading
30.5 KB
Loading
47.5 KB
Loading
30.3 KB
Loading

0 commit comments

Comments
 (0)