Operations 13 min read

Mastering Keepalived: Step-by-Step HA Setup, Scripts, and Troubleshooting

This guide walks through planning server IPs, installing and configuring keepalived for high availability, crafting master and backup keepalived.conf files, creating a Tomcat health‑check script, monitoring logs, handling failover, and resolving common errors on Linux systems.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Keepalived: Step-by-Step HA Setup, Scripts, and Troubleshooting

1. Planning

Define server IPs: master/load balancer 10.10.195.53, backup 10.10.195.190, virtual IP 10.10.195.212.

2. Software Environment

OS: Red Hat Enterprise Linux 5.6; keepalived 1.2.19; JDK 1.7.0_79; Tomcat 7.0.64.

3. Install and Configure keepalived

Installation commands:

tar -zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived --disable-fwmark
make
make install

Copy binaries and config files, enable 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
service keepalived restart

3.1 keepalived.conf for Master

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
    }
}

3.2 keepalived.conf for Backup

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
    }
}

4. /opt/tomcat.pid script

#!/bin/bash
# description: check tomcat service and decide whether stop keepalived
CATALINA_HOME=/users/shr/apache-tomcat-7.0.64
JAVA_HOME=/users/shr/util/JavaDir/jdk
export CATALINA_HOME JAVA_HOME
# check tomcat process count
ps ax | grep "org.apache.catalina.startup.Bootstrap start" | grep -v grep | 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 again, stop keepalived if tomcat not running
    ps ax | grep "org.apache.catalina.startup.Bootstrap start" | grep -v grep | wc -l > tomcat_process_count.txt
    read line2 < tomcat_process_count.txt
    if [ $line2 -lt 1 ]; then
        service keepalived stop
    fi
fi
rm tomcat_process_count.txt

5. Log Checking and Failover

Use tail -f /var/log/messages to view keepalived logs. The logs show VRRP state transitions, VIP assignment, and gratuitous ARP messages. Stopping the master service triggers a failover to the backup, which then assumes MASTER state.

6. Common Errors

Error: “receive an invalid ip number count associated with VRID”. Fix by changing virtual_router_id to a unique value.

Error: Backup does not set VIP after entering BACKUP state. Ensure there is a space before the opening brace in the configuration file.

7. Tomcat Init Script (Optional)

A sample SysV init script for managing Tomcat is provided, including start, stop, restart, and status functions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

high availabilityload balancingLinuxTomcatVRRPkeepalived
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.