Mastering Keepalived: Step‑by‑Step Server Load Balancing on Linux
This guide walks through planning the server and software environment, installing and configuring keepalived, setting up master‑backup VRRP instances, monitoring logs, handling failover, checking virtual IPs, troubleshooting common errors, and adding a Tomcat service script for high‑availability Linux deployments.
1. Planning
1.1 Server environment
Load balancer master and WEB server 1 real IP: 10.10.195.53
Load balancer backup and 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
This section describes only the installation and configuration of keepalived; Java and Tomcat setup are 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 service
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 Configure keepalived.conf
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vi /etc/keepalived/keepalived.conf
Master configuration (NodeA):
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 (NodeB):
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 /opt/tomcat.pid script
#!/bin/bash
#description: check tomcat service and decide whether stop keepalived
#edited by zzh: 2015-10-14
CATALINA_HOME=/users/shr/apache-tomcat-7.0.64
JAVA_HOME=/users/shr/util/JavaDir/jdk
export CATALINA_HOME
export JAVA_HOME
ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | grep -v "grep" | awk '{printf $1 " "}' | wc | awk '{print $2}' > tomcat_process_count.txt
read line < tomcat_process_count.txt
start_tomcat=$CATALINA_HOME/bin/startup.sh
if [ ${line} -lt 1 ]; then
echo -n "===Starting tomcat===:"
${start_tomcat}
echo "===tomcat start ok.==="
sleep 3
# check the tomcat status.
ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrapstart" | grep -v "grep" | awk '{printf $1 " "}' | wc | awk '{print $2}' > 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 inspection
3.1 Normal startup
Run tail -f /var/log/messages on the master (10.10.195.53) to see keepalived start logs, including VRRP instance transitions to MASTER and gratuitous ARP announcements.
Similar logs appear on the backup server (10.10.195.190) showing it entering BACKUP state.
3.2 Master‑backup failover
Stopping keepalived on the master ( service keepalived stop) triggers the backup to become MASTER, as shown by VRRP logs indicating state transition and VIP assignment.
Starting keepalived again on the original master causes it to reclaim MASTER role, with corresponding logs.
4. View virtual IP
Use ip addr show to verify that the virtual IP 10.10.195.212 is present on the active interface.
5. Common errors
5.1 Invalid IP number count
Log messages such as “receive an invalid ip number count associated with VRID!” can be resolved by changing virtual_router_id to a different value in /etc/keepalived/keepalived.conf.
5.2 Missing space before '{'
If the backup does not set VIPs after entering BACKUP state, ensure there is a space before the opening brace in the configuration file.
6. Adding a Tomcat service script
The following init script can be placed in /etc/init.d/tomcat to manage Tomcat as a service.
#!/bin/bash
#chkconfig: 2345 10 90
#description: Starts and Stops the tomcat daemon
#edited by ZZH: 2015-10-14
CATALINA_HOME=/users/shr/apache-tomcat-7.0.64
JAVA_HOME=/users/shr/util/JavaDir/jdk
export CATALINA_HOME
export JAVA_HOME
start_tomcat=$CATALINA_HOME/bin/startup.sh
stop_tomcat=$CATALINA_HOME/bin/shutdown.sh
start() {
echo -n "===Starting tomcat===:"
${start_tomcat}
echo "===tomcat start ok.==="
}
stop() {
echo -n "===Shutting down tomcat===:"
${stop_tomcat}
echo "===tomcat stop ok.==="
}
status() {
ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | grep -v "grep" | awk '{printf $1 " "}' | wc | awk '{print $2}' > tomcat_process_count.txt
read line < tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo "tomcat is running"
else
echo "tomcat is stopped"
fi
}
case "$1" in
start) start ;;
stop) stop ;;
restart) stop; sleep 1; start ;;
status) status ;;
*) echo "Usage: $0 {start|stop|restart|status}"; exit 1 ;;
esac
exit 0Make the script executable and add it to startup:
sudo chmod 755 tomcat sudo chkconfig --add tomcat
Then control Tomcat with service tomcat start|stop|restart|status.
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.
