How to Set Up Keepalived for High‑Availability Load Balancing on RHEL 5
This guide walks through planning server and software environments, installing and configuring keepalived on Red Hat Enterprise Linux 5, creating master‑backup VRRP instances, integrating a Tomcat health‑check script, monitoring logs, testing failover, and troubleshooting common errors.
1. Planning
1.1 Server environment
Load balancer master / WEB server 1 real IP: 10.10.195.53
Load balancer backup / WEB server 2 real IP: 10.10.195.190
Virtual IP for the load balancer: 10.10.195.212
1.2 Software environment
OS: Red Hat Enterprise Linux Server release 5.6 (Tikanga)
keepalived: keepalived‑1.2.19
Java: jdk‑1.7.0_79
Tomcat: apache‑tomcat‑7.0.64
2. Load‑balancer configuration
Only the keepalived installation and configuration are described here; Java and Tomcat setup is omitted.
2.1 Install keepalived
tar -zxvf keepalived-1.2.19.tar.gz cd keepalived-1.2.19 ./configure --prefix=/usr/local/keepalived --disable-fwmark make make install
2.2 Deploy keepalived binaries and init scripts
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ chkconfig --add keepalived chkconfig keepalived on
Control commands:
service keepalived restart service keepalived start service keepalived stop service keepalived status
2.3 Edit keepalived.conf
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vi /etc/keepalived/keepalived.conf
The master and backup configurations are almost identical; only the highlighted sections differ.
Master configuration
global_defs { router_id NodeA } vrrp_script chk_http_port { script "/opt/tomcat.pid" interval 5 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 52 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port } virtual_ipaddress { 10.10.195.212 } }
Backup configuration
global_defs { router_id NodeB } vrrp_script chk_http_port { script "/opt/tomcat.pid"; interval 5; weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS; auth_pass 1111 } track_script { chk_http_port } virtual_ipaddress { 10.10.195.212 } }
2.4 Tomcat health‑check script ( /opt/tomcat.pid )
#!/bin/bash #description: check tomcat service and decide whether to stop keepalived CATALINA_HOME=/users/shr/apache-tomcat-7.0.64 JAVA_HOME=/users/shr/util/JavaDir/jdk export CATALINA_HOME JAVA_HOME ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | grep -v grep | awk '{printf $1 " "}' | wc -l > tomcat_process_count.txt read line < tomcat_process_count.txt if [ ${line} -lt 1 ]; then echo "===Starting tomcat===:" ${CATALINA_HOME}/bin/startup.sh echo "===tomcat start ok.===" sleep 3 # check tomcat status again ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | grep -v grep | awk '{printf $1 " "}' | wc -l > tomcat_process_count.txt read line2 < tomcat_process_count.txt if [ ${line2} -lt 1 ]; then sudo service keepalived stop fi fi rm tomcat_process_count.txt # shell end
3. Log verification
Use tail -f /var/log/messages on the master (10.10.195.53) to see keepalived start‑up messages and VRRP state transitions. Similar logs appear on the backup (10.10.195.190).
4. Failover testing
Stop keepalived on the master ( service keepalived stop) – the backup becomes MASTER. Restart keepalived on the former master ( service keepalived start) – it re‑acquires MASTER role.
5. View virtual IP
ip addr show
Look for the 10.10.195.212/32 address on eth0.
6. Common errors and fixes
6.1 VRID conflict
Log shows “receive an invalid ip number count associated with VRID”. Change virtual_router_id in keepalived.conf to a different number.
6.2 Missing space in configuration
If the backup stays in BACKUP state without assigning VIPs, ensure there is a space after the opening brace { in the configuration file.
7. Tomcat service script (optional)
A sample /etc/init.d/tomcat script is provided to start, stop, restart and query status of Tomcat. It checks for the existence of catalina.sh, defines start, stop, and status functions, and registers the script with chkconfig.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
