How to Install HAProxy and Exporter on Kubernetes and Monitor It with Prometheus
This guide walks through installing HAProxy on a Kubernetes master node, compiling and configuring it, adding the HAProxy exporter, creating a ServiceMonitor YAML for the Prometheus Operator, and verifying that metrics are correctly scraped and displayed in the Prometheus UI.
Install HAProxy on the Kubernetes Master
On the master node (IP 192.168.0.6) download the HAProxy 1.8.6 source package from the provided Baidu Cloud link, extract it, and compile:
tar zxvf haproxy-1.8.6.tar.gz cd haproxy-1.8.6 make TARGET=linux31 make install PREFIX=/usr/local/haproxy mkdir /usr/local/haproxy/confCopy the example configuration and adjust the listening port:
cp examples/option-http_proxy.cfg /usr/local/haproxy/conf/haproxy.cfgEdit /usr/local/haproxy/conf/haproxy.cfg and change the bind line to *:5000 (or any port suitable for your environment).
Create an init script at /etc/init.d/haproxy (the full script is provided in the source). Make it executable and start the service:
chmod +x /etc/init.d/haproxy service haproxy startVerify that HAProxy is listening on the chosen port:
ss -antulp | grep :5000Install HAProxy Exporter
Download the haproxy_exporter-0.9.0.linux-386.tar.gz package from the same Baidu Cloud link, extract it, and copy the binary to /usr/bin:
tar zxvf haproxy_exporter-0.9.0.linux-386.tar.gz cd haproxy_exporter-0.9.0.linux-386 cp haproxy_exporter /usr/binRun the exporter, pointing it to the HAProxy stats endpoint (adjust IP/port if needed):
haproxy_exporter --haproxy.scrape-uri="http://192.168.0.6:5000/baz?stats;csv" --web.listen-address="192.168.0.6:9100" &Check that the exporter is listening: ss -antulp | grep :9100 Query the metrics endpoint to see the exported data:
curl 192.168.0.6:9100/metricsConfigure Prometheus Operator ServiceMonitor for HAProxy
Create a Service and Endpoints object for HAProxy, then a ServiceMonitor that tells the Prometheus Operator to scrape the exporter:
kind: Service
apiVersion: v1
metadata:
name: haproxy
labels:
app: haproxy
spec:
type: ClusterIP
ports:
- name: haproxy
port: 9100
targetPort: 9100
---
kind: Endpoints
apiVersion: v1
metadata:
name: haproxy
labels:
app: haproxy
subsets:
- addresses:
- ip: 192.168.0.6
ports:
- name: haproxy
port: 9100
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: haproxy
namespace: monitoring
labels:
team: frontend
spec:
namespaceSelector:
any: true
matchNames:
- default
selector:
matchLabels:
app: haproxy
endpoints:
- port: haproxy
targetPort: 9100Apply the manifest:
kubectl apply -f prometheus-servicemonitor-haproxy.yamlVerify Monitoring in Prometheus UI
Open the Prometheus web UI and confirm that HAProxy metrics appear. Screenshots of the UI are included below.
With HAProxy, the exporter, and the ServiceMonitor correctly configured, Prometheus continuously scrapes HAProxy performance data, enabling alerting and dashboard visualisation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
