To expose Prometheus metrics from your applications through HAProxy and scrape them using Prometheus, you can follow these steps:
-
Enable the Prometheus exporter in HAProxy:
You need to enable the Prometheus exporter in HAProxy to expose metrics. Add the following lines to your HAProxy configuration file (usually at /etc/haproxy/haproxy.cfg):
global
...
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
...
listen stats
bind *:9100
mode http
stats enable
stats hide-version
stats uri /admin?stats
stats realm Haproxy\ Statistics
stats auth admin:password
stats scope .
Replace admin and password with your preferred credentials. This configuration enables the Prometheus exporter on port 9100.
-
Configure Prometheus to scrape HAProxy metrics:
Add a new job to your Prometheus configuration file (usually at /etc/prometheus/prometheus.yml) to scrape HAProxy metrics:
scrape_configs:
- job_name: 'haproxy'
static_configs:
- targets: ['<HAProxy_IP_Address>:9100']
metrics_path: '/admin?stats'
params:
module: ['global', 'frontend', 'backend', 'server']
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: <HAProxy_IP_Address>:9100
Replace <HAProxy_IP_Address> with the IP address of your HAProxy server.
-
Restart HAProxy and Prometheus:
Restart the HAProxy and Prometheus services for the changes to take effect:
sudo systemctl restart haproxy
sudo systemctl restart prometheus
-
Verify Prometheus is scraping HAProxy metrics:
Check the Prometheus UI to ensure that it's scraping metrics from HAProxy. Open your browser and navigate to the Prometheus web interface (usually at http://<Prometheus_IP_Address>:9090). You should now see HAProxy metrics under the Graph tab.
For exposing metrics from your applications through HAProxy, you need to enable the /metrics endpoint in your application. If your application already has a Prometheus metrics endpoint, HAProxy can forward the requests to your application instances.
If you want to route Prometheus scraping requests to your application instances directly instead of going through HAProxy, you can do so by configuring another job in the Prometheus configuration file with the IP addresses and ports of your application instances.
However, if you still want to route Prometheus scraping requests through HAProxy, you can create a dedicated frontend and backend section in the HAProxy configuration for the metrics endpoint. Then, update the Prometheus configuration to target the HAProxy IP address and the metrics endpoint port.
For example, add the following to your HAProxy configuration:
frontend metrics_fe
bind *:9101
mode http
default_backend metrics_be
backend metrics_be
mode http
balance roundrobin
option forwardfor
http-request set-path /metrics
server app1 <App_IP_Address_1>:<App_Metrics_Port_1>
server app2 <App_IP_Address_2>:<App_Metrics_Port_2>
...
Then, update your Prometheus configuration to target the HAProxy IP address and the metrics endpoint port:
scrape_configs:
- job_name: 'my_app'
static_configs:
- targets: ['<HAProxy_IP_Address>:9101']
This way, Prometheus scrapes application metrics through HAProxy.