HAProxy_in_Kubernetes_DevOps_Guide_1731261100
HAProxy_in_Kubernetes_DevOps_Guide_1731261100
1. Introduction to HAProxy
Key Benefits:
To deploy HAProxy in Kubernetes, you can use either a containerized HAProxy image
directly in your cluster or leverage HAProxy Ingress as an ingress controller.
Create a deployment for HAProxy to ensure multiple replicas are running and available.
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy-deployment
spec:
replicas: 2
selector:
matchLabels:
app: haproxy
template:
metadata:
labels:
app: haproxy
spec:
containers:
- name: haproxy
image: haproxy:latest
ports:
- containerPort: 80
- containerPort: 443
volumeMounts:
- mountPath: /usr/local/etc/haproxy/haproxy.cfg
name: haproxy-config
subPath: haproxy.cfg
volumes:
- name: haproxy-config
configMap:
name: haproxy-config
apiVersion: v1
kind: ConfigMap
metadata:
name: haproxy-config
data:
haproxy.cfg: |
global
log stdout format raw local0
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
server srv1 <SERVICE_NAME>:<PORT> check
Replace <SERVICE_NAME> and <PORT> with the actual Kubernetes service and port.
Expose HAProxy as a Kubernetes Service to make it accessible inside or outside the cluster.
apiVersion: v1
kind: Service
metadata:
name: haproxy-service
spec:
selector:
app: haproxy
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
HAProxy can also be set up as an ingress controller to manage HTTP routing across
multiple services in your Kubernetes cluster.
kubectl apply -f
https://haproxy-ingress.github.io/haproxy-ingress/deploy/haproxy-ingres
s.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
haproxy.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80
SSL Termination
HAProxy can handle SSL termination by binding port 443 in the configuration. Add an SSL
certificate and key to a Kubernetes secret and reference it in your HAProxy configuration.
apiVersion: v1
kind: Secret
metadata:
name: haproxy-ssl
data:
tls.crt: <base64-encoded-cert>
tls.key: <base64-encoded-key>
type: kubernetes.io/tls
Then, update the HAProxy configuration to include the SSL secret and bind port 443:
frontend https_front
bind *:443 ssl crt /usr/local/etc/haproxy/certs/haproxy-ssl.pem
default_backend http_back
Advanced Load Balancing Algorithms
HAProxy supports advanced load balancing strategies like leastconn (least connections),
source (sticky sessions), and random.
backend http_back
balance leastconn
server srv1 <SERVICE_NAME>:<PORT> check
● Replica Scaling: Set the number of replicas in the HAProxy deployment to increase
capacity.
● Auto-scaling: Use Horizontal Pod Autoscaler (HPA) to scale HAProxy based on
CPU or memory usage.
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: haproxy-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: haproxy-deployment
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 75
6. Monitoring HAProxy
Use HAProxy’s built-in metrics and monitoring tools to observe traffic and performance.
listen stats
bind *:8404
mode http
stats enable
stats uri /metrics
stats refresh 10s
readinessProbe:
httpGet:
path: /healthz
port: 8404
initialDelaySeconds: 5
periodSeconds: 10
5. SSL/TLS Management
Use Let’s Encrypt or similar tools for automated certificate management, especially
for public-facing services.
8. Troubleshooting Tips
Conclusion
HAProxy in Kubernetes provides robust load balancing, traffic routing, and high availability
for applications. By deploying HAProxy as a standalone service or an ingress controller,
DevOps teams can leverage its flexibility to manage traffic, scale dynamically, and enhance
security. This guide offers a step-by-step approach to deploying HAProxy, configuring
advanced features, and monitoring performance, helping DevOps engineers create resilient
and scalable environments.
For Kubernetes environments that demand high performance and reliability, HAProxy serves
as a powerful tool for routing and managing traffic effectively.