(01)Install HAProxy
Linux
1# Ubuntu / Debian2sudo apt-get install -y haproxy3 4# RHEL / Fedora5sudo dnf install -y haproxy6 7sudo systemctl enable haproxyHigh availability load balancer and reverse proxy for TCP/HTTP traffic
What is this?
HAProxy spreads incoming web traffic across multiple servers so no single server gets overloaded.
Where to create or edit the main configuration — paths below match the setup steps.
/etc/haproxy/haproxy.cfgLocation: HAProxy server
Frontends, backends, ACLs, and SSL
Step 01
1# Ubuntu / Debian2sudo apt-get install -y haproxy3 4# RHEL / Fedora5sudo dnf install -y haproxy6 7sudo systemctl enable haproxyStep 02
1sudo nano /etc/haproxy/haproxy.cfg2 3# global / defaults sections first, then:4 5frontend http_front6 bind *:807 default_backend http_back8 9backend http_back10 balance roundrobin11 server web1 192.168.1.21:80 check12 server web2 192.168.1.22:80 check13 14# Stats page (optional):15listen stats16 bind *:840417 stats enable18 stats uri /stats19 stats refresh 10s20 21sudo haproxy -c -f /etc/haproxy/haproxy.cfg22sudo systemctl restart haproxy1frontend https_front2 bind *:443 ssl crt /etc/haproxy/certs/example.com.pem3 default_backend http_back4 5# Combine cert + key:6# cat cert.pem key.pem > example.com.pem1frontend mysql_front2 bind *:33063 mode tcp4 default_backend mysql_back5 6backend mysql_back7 mode tcp8 balance leastconn9 server db1 192.168.1.31:3306 check10 server db2 192.168.1.32:3306 checkStep 03
1sudo systemctl status haproxy2curl http://localhost/stats3sudo ss -tlnp | grep haproxyStep 04
1sudo haproxy -c -f /etc/haproxy/haproxy.cfg2sudo systemctl reload haproxy3echo "show stat" | sudo socat stdio /run/haproxy/admin.sock1 YAML template for HAProxy. Copy and deploy after setup.
1 ready-to-copy template. Expand one, copy the YAML, then run the deploy commands.
deployment.yml
1apiVersion: apps/v12kind: Deployment3metadata:4 name: web5spec:6 replicas: 37 selector:8 matchLabels:9 app: web10 template:11 metadata:12 labels:13 app: web14 spec:15 containers:16 - name: nginx17 image: nginx:alpine18 ports:19 - containerPort: 80service-loadbalancer.yml
LoadBalancer assigns external IP (cloud) or MetalLB IP
1apiVersion: v12kind: Service3metadata:4 name: web-lb5spec:6 type: LoadBalancer7 selector:8 app: web9 ports:10 - port: 8011 targetPort: 8012 protocol: TCPStep 01
1kubectl apply -f .2kubectl get all3kubectl get pods -w1kubectl delete -f .