HA-Proxy
HAProxy Setup Explained (From Beginner to Enterprise)
HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and reverse proxy. In Kubernetes, it is commonly used in on-premises clusters to provide a single endpoint for the Kubernetes API server and, in some architectures, to load balance application traffic.
Why Do We Need HAProxy?
Imagine you have a highly available Kubernetes control plane.
Master-1
192.168.1.11
Master-2
192.168.1.12
Master-3
192.168.1.13
If your developers configure kubectl to connect directly to:
192.168.1.11 - Master-1
and Master-1 fails:
kubectl
│
▼
Master-1 ❌
The Kubernetes API becomes unreachable from the client's perspective, even though Master-2 and Master-3 are healthy.
Solution: Put HAProxy in Front
kubectl
│
▼
HAProxy (192.168.1.10)
│
┌──────────┼──────────┐
▼ ▼ ▼
Master-1 Master-2 Master-3
6443 6443 6443
Now everyone connects to one IP address:
192.168.1.10
HAProxy forwards requests to a healthy API server.
Step 1 – Provision the HAProxy Server
You need a Linux VM.
| Component | IP |
|---|---|
| HAProxy | 192.168.1.10 |
| Master-1 | 192.168.1.11 |
| Master-2 | 192.168.1.12 |
| Master-3 | 192.168.1.13 |
Recommended OS:
- Ubuntu
- RHEL
- Rocky Linux
- AlmaLinux
Step 2 – Install HAProxy
Ubuntu
sudo apt update
sudo apt install haproxy -y
RHEL
sudo yum install haproxy -y
Verify:
haproxy -v
Step 3 – Configure HAProxy
Main configuration file:
/etc/haproxy/haproxy.cfg
Example:
global
daemon
maxconn 4000
defaults
mode tcp
timeout connect 10s
timeout client 1m
timeout server 1m
frontend kubernetes-api
bind *:6443
default_backend masters
backend masters
balance roundrobin
option tcp-check
server master1 192.168.1.11:6443 check
server master2 192.168.1.12:6443 check
server master3 192.168.1.13:6443 check
What Does This Configuration Mean?
Frontend
This tells HAProxy:
"Listen for incoming connections."
bind *:6443
Listen on port 6443, the Kubernetes API port.
Backend
backend masters
Defines the destination servers.
server master1 192.168.1.11:6443
Traffic can be sent to Master-1.
------------
server master2 192.168.1.12:6443
Traffic can be sent to Master-2.
------------
Health Checks
check
HAProxy periodically tests each API server.
Example:
Master-1 ✓
Master-2 ✓
Master-3 ✗
Master-3 is automatically removed from rotation until it becomes healthy again.
Step 4 – Start the Service
sudo systemctl enable haproxy
sudo systemctl start haproxy
Verify:
sudo systemctl status haproxy
Step 5 – Validate the Configuration
> sudo haproxy -c -f /etc/haproxy/haproxy.cfg
Expected:
Configuration file is valid
Step 6 – Configure Kubernetes
During kubeadm init:
Instead of:
192.168.1.11
Use:
192.168.1.10:6443
Example:
kubeadm init \
--control-plane-endpoint=192.168.1.10:6443
Now every node joins using the HAProxy endpoint.
Request Flow
kubectl get pods
│
▼
HAProxy
│
┌─────-─|──────┐
▼ ▼ ▼
Master1 Master2 Master3
Real time HAproxy file
#---------------------------------------------------------------------
# Global Settings
#---------------------------------------------------------------------
global
log /dev/log local0
log /dev/log local1 notice
daemon
user haproxy
group haproxy
maxconn 50000
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
tune.ssl.default-dh-param 2048
#---------------------------------------------------------------------
# Default Settings
#---------------------------------------------------------------------
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 10s
timeout client 60s
timeout server 60s
retries 3
#---------------------------------------------------------------------
# Kubernetes API Server Frontend
#---------------------------------------------------------------------
frontend kubernetes-api
bind *:6443
default_backend kubernetes-control-plane
#---------------------------------------------------------------------
# Kubernetes API Backend
#---------------------------------------------------------------------
backend kubernetes-control-plane
mode tcp
balance roundrobin
option tcp-check
default-server inter 10s fall 3 rise 2
server master01 10.10.1.11:6443 check
server master02 10.10.1.12:6443 check
server master03 10.10.1.13:6443 check
#---------------------------------------------------------------------
# Statistics Page
#---------------------------------------------------------------------
listen stats
bind *:8404
mode http
stats enable
stats uri /stats
stats refresh 10s
stats show-legends
stats auth admin:Admin@123
Comments
Post a Comment