Build a Dual‑Master HAProxy + Keepalived Load Balancer for Zero Downtime
This guide walks through setting up a high‑availability HAProxy and Keepalived dual‑master configuration, detailing the experiment environment, installation steps, keepalived scripts, and testing procedures that ensure continuous service even if one node fails.
Experiment Environment
Two HAProxy instances (haproxy01 192.168.137.254 and haproxy02 192.168.137.253) are deployed alongside two web servers (web1 192.168.137.201, web2 192.168.137.202) for www.zhirs.com and two image servers (img1 192.168.158.101, img2 192.168.158.102) for img.zhirs.com. Virtual IPs VIP1 192.168.137.200 and VIP2 192.168.137.100 are used for the two domains.
Architecture Diagram
Installation and Configuration Process
Download and compile HAProxy 1.4.24 on both servers: http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz Create identical HAProxy configuration files and init scripts on the two nodes.
Start the HAProxy service and verify its status.
Keepalived Installation and Configuration
Install required packages (e.g., PCRE) and Nginx as shown below:
#cd /root/software
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
#tar -zxvf pcre-8.33.tar.gz
#cd pcre-8.33
#./configure
#make && make install
#wget http://nginx.org/download/nginx-1.2.9.tar.gz
#tar -zxvf nginx-1.2.9.tar.gz
#cd nginx-1.2.9
#./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module
#make && make installConfigure keepalived on the first node as master for VIP1 (192.168.137.200) and backup for VIP2 (192.168.137.100); the second node has the opposite roles. The configuration files are illustrated in the following screenshots.
Health‑Check Script
A custom script monitors the HAProxy process; if HAProxy stops, keepalived will not trigger an automatic failover, ensuring stability. The script content is shown in the image below.
Testing and Verification
Start both keepalived and HAProxy services on the two servers, then verify the virtual IPs with ifconfig (examples shown in the image). Bringing down either keepalived instance leaves both www.zhirs.com and img.zhirs.com reachable, demonstrating zero‑downtime failover.
Further testing with an Nginx + Keepalived dual‑master setup is planned.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
