Cloud Native 10 min read

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.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
How to Install HAProxy and Exporter on Kubernetes and Monitor It with Prometheus

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/conf

Copy the example configuration and adjust the listening port:

cp examples/option-http_proxy.cfg /usr/local/haproxy/conf/haproxy.cfg

Edit /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 start

Verify that HAProxy is listening on the chosen port:

ss -antulp | grep :5000

Install 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/bin

Run 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/metrics

Configure 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: 9100

Apply the manifest:

kubectl apply -f prometheus-servicemonitor-haproxy.yaml

Verify 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

monitoringKubernetesHAProxyExporterServiceMonitor
Full-Stack DevOps & Kubernetes
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.