
Load Balancer: ตั้งค่า HAProxy สำหรับ High Availability
Load Balancer กระจาย traffic จาก clients ไปยัง backend servers หลายตัว เพื่อเพิ่ม capacity (รองรับ users มากขึ้น), availability (ถ้า server ตัวหนึ่งล่ม traffic ไปตัวอื่นอัตโนมัติ) และ performance (กระจาย load ไม่ให้ server ตัวไหน overload) Load Balancer เป็นองค์ประกอบสำคัญของ production infrastructure ที่ต้องรองรับ traffic สูง
HAProxy (High Availability Proxy) เป็น open-source load balancer ที่นิยมที่สุดในโลก ใช้โดย GitHub, Stack Overflow, Reddit, Airbnb และองค์กรขนาดใหญ่อีกมากมาย รองรับ L4 (TCP) และ L7 (HTTP) load balancing performance สูงมาก (handle millions of connections) บทความนี้จะสอนวิธีติดตั้งและ configure HAProxy
Load Balancing Algorithms
| Algorithm | วิธีทำงาน | เหมาะกับ |
|---|---|---|
| Round Robin | วนส่ง traffic ไปทีละ server ตามลำดับ | Servers spec เท่ากัน stateless apps |
| Weighted Round Robin | วนส่ง แต่ server ที่ weight สูงได้ traffic มากกว่า | Servers spec ไม่เท่ากัน |
| Least Connections | ส่งไป server ที่มี active connections น้อยที่สุด | Requests ใช้เวลาต่างกัน (long-lived connections) |
| Source IP Hash | Hash source IP เพื่อส่งไป server เดิมทุกครั้ง | Session persistence (sticky sessions) |
| URI Hash | Hash URI เพื่อ cache efficiency | Caching servers |
L4 vs L7 Load Balancing
| คุณสมบัติ | L4 (TCP/UDP) | L7 (HTTP/HTTPS) |
|---|---|---|
| ทำงานที่ Layer | Transport (TCP/UDP) | Application (HTTP) |
| ดูข้อมูล | IP + Port เท่านั้น | URL, Headers, Cookies, Content |
| Routing | ตาม IP/Port | ตาม URL path, hostname, headers |
| SSL Termination | ไม่ได้ (pass-through) | ได้ (decrypt แล้ว forward) |
| Performance | เร็วกว่า (ไม่ inspect content) | ช้ากว่าเล็กน้อย (inspect HTTP) |
| Use Case | Database, mail, non-HTTP | Web applications, APIs |
ติดตั้ง HAProxy
Installation
ติดตั้ง HAProxy บน Ubuntu ด้วย apt install haproxy config file หลักอยู่ที่ /etc/haproxy/haproxy.cfg ตรวจสอบ version ด้วย haproxy -v แนะนำ version 2.6+ สำหรับ features ใหม่ (HTTP/3, advanced health checks)
HAProxy Configuration
Global & Defaults
Section global กำหนด process-level settings: maxconn (max connections), logging, SSL settings Section defaults กำหนด default values สำหรับทุก frontend/backend: mode (http/tcp), timeout connect, timeout client, timeout server, logging options
Frontend
Frontend กำหนดว่า HAProxy listen ที่ port ไหน รับ traffic จาก clients bind *:80 สำหรับ HTTP bind *:443 ssl crt /path/to/cert.pem สำหรับ HTTPS (SSL termination) กำหนด ACL rules สำหรับ routing (เช่น ถ้า URL เริ่มด้วย /api ส่งไป backend API servers) use_backend เลือก backend ตาม ACL
Backend
Backend กำหนด group ของ servers ที่รับ traffic balance algorithm (roundrobin, leastconn, etc.) server entries: server web1 10.0.1.10:80 check server web2 10.0.1.11:80 check “check” เปิด health check (HAProxy จะ check ว่า server ยังทำงานอยู่)
Health Checks
HTTP Health Check
HAProxy ตรวจสอบ backend servers เป็นระยะ ถ้า server ไม่ตอบ หรือตอบ error จะถูก mark เป็น DOWN ไม่ส่ง traffic ไปอีก จนกว่าจะกลับมา UP HTTP health check: option httpchk GET /health ตรวจสอบว่า /health endpoint ตอบ 200 OK กำหนด inter (interval), fall (จำนวนครั้งที่ fail ก่อน mark DOWN), rise (จำนวนครั้งที่ pass ก่อน mark UP)
SSL Termination
HTTPS บน HAProxy
SSL Termination HAProxy decrypt HTTPS traffic แล้ว forward เป็น HTTP ไป backend servers ข้อดี: backend servers ไม่ต้องจัดการ SSL certificates จัดการ certificates ที่จุดเดียว (HAProxy) HAProxy ทำ SSL offloading ได้เร็ว ใช้ Let’s Encrypt + certbot สำหรับ free SSL certificates
Session Persistence (Sticky Sessions)
Cookie-based
บาง applications ต้องการให้ user กลับมาที่ server เดิม ทุกครั้ง (session ถูกเก็บบน server) HAProxy ใช้ cookie insert: cookie SERVERID insert indirect nocache ฝัง cookie ใน response บอก client ว่าอยู่ server ไหน requests ถัดไป HAProxy อ่าน cookie แล้วส่งไป server เดิม
HAProxy Stats Dashboard
Monitoring
HAProxy มี built-in stats page แสดง: จำนวน connections ต่อ server, server status (UP/DOWN), response times, error rates เปิด stats ด้วย: listen stats bind *:8404 stats enable stats uri /stats stats auth admin:password เข้า http://haproxy_ip:8404/stats
High Availability สำหรับ HAProxy เอง
Keepalived + HAProxy
ถ้า HAProxy ตัวเดียวล่ม ทุกอย่างล่มตาม (single point of failure) ใช้ Keepalived + Virtual IP (VIP) สร้าง HA pair: HAProxy 1 (active) + HAProxy 2 (standby) share VIP ถ้า active ล่ม standby จะ take over VIP ภายในวินาที clients เห็น VIP เดิม ไม่รู้ว่า failover เกิดขึ้น
HAProxy vs Alternatives
| Load Balancer | ราคา | จุดเด่น | เหมาะกับ |
|---|---|---|---|
| HAProxy | ฟรี (open-source) | Performance สูงสุด feature ครบ | High-traffic production |
| Nginx | ฟรี / Plus (paid) | Web server + LB + reverse proxy | Web applications |
| Traefik | ฟรี (open-source) | Auto-discovery containers/K8s | Docker/Kubernetes environments |
| AWS ALB/NLB | Pay-per-use | AWS native fully managed | AWS workloads |
| F5 BIG-IP | แพงมาก | Enterprise features iRules | Large enterprise |
ทิ้งท้าย: Load Balancer คือ Must-Have สำหรับ Production
HAProxy เป็น load balancer ที่ดีที่สุดสำหรับองค์กรที่ต้องการ performance สูง ฟรี feature ครบ ติดตั้งและ configure ไม่ยาก เริ่มจาก basic round robin + health checks แล้วค่อยเพิ่ม SSL termination, sticky sessions, stats monitoring ใช้ Keepalived สำหรับ HA ของ HAProxy เอง
อ่านเพิ่มเติมเกี่ยวกับ Disaster Recovery และ Container Networking ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com