Updated on 2024-05-17 GMT+08:00

Configuring Keepalived

Procedure

  1. Run the following command in the background to modify the /etc/keepalived/keepalived.conf file and configure Keepalived for the primary LVS node:

    vi /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
     
    global_defs {
        router_id master_node
    }
     
    local_address_group laddr_gl {
    192.168.0.157 # IP address of the primary LVS node
    }
     
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id   51
        priority 100
        advert_int   1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            192.168.0.27
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    }
     
    Vitual IP port
    virtual_server 192.168.0.27 80 { 
        delay_loop 6
        lb_algo rr
        lb_kind FNAT
        protocol TCP
        laddr_group_name laddr_gl
    Backend server node
        real_server 192.168.0.3 80 {
            weight 1
            TCP_CHECK {
                connect_timeout 15
                connect_port 80
                nb_get_retry 5
                delay_before_retry 3
            }
        }
        real_server 192.168.0.46 80 {
            weight 1
            TCP_CHECK {
                connect_timeout 15
                connect_port 80
                nb_get_retry 5
                delay_before_retry 3
            }
        }
    }
  2. Run the following command in the background to modify the /etc/keepalived/keepalived.conf file and configure Keepalived for the standby LVS node:

    vi /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
     
    global_defs {
        router_id master_node
    }
     
    local_address_group laddr_gl {
    192.168.0.234 #Local IP address
    }
     
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id   51
    priority 99 #The value must be lower than that of the primary node.
        advert_int   1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            192.168.0.27
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    }
     
    virtual_server 192.168.0.27 80 {
        delay_loop 6
        lb_algo rr
        lb_kind FNAT
        protocol TCP
        laddr_group_name laddr_gl
        real_server 192.168.0.3 80 {
            weight 1
            TCP_CHECK {
                connect_timeout 15
                connect_port 80
                nb_get_retry 5
                delay_before_retry 3
            }
        }
        real_server 192.168.0.46 80 {
            weight 1
            TCP_CHECK {
                connect_timeout 15
                connect_port 80
                nb_get_retry 5
                delay_before_retry 3
            }
        }
    }

    Perform steps 3 to 6 on both the primary and standby LVS nodes.

  3. Run the following command in the background to modify the /etc/keepalived/notify.sh file:

    vi /etc/keepalived/notify.sh

    #!/bin/bash
     
    if [ $# -eq 0 ]
    then
        echo "input master/backup"
        exit 1
    fi
     
    case $1 in
    master)
            sed -i "s/#laddr_group_name/laddr_group_name/" /etc/keepalived/keepalived.conf 2>/dev/null
            systemctl reload keepalived.service
    ;;
    backup)
            if [ `grep "#laddr_group_name" /etc/keepalived/keepalived.conf|wc -l` -eq 0 ]
            then
                sed -i "s/laddr_group_name/#laddr_group_name/" /etc/keepalived/keepalived.conf 2>/dev/null
                systemctl reload keepalived.service
            fi
    ;;
    esac

    chmod +x /etc/keepalived/notify.sh

  4. Run the following command to enable keepalived:

    systemctl start keepalived

  5. Check whether the configuration is modified:

    ipvsadm -Ln

    If the number of servers returned by ipvsadm is different from the actual number, check whether the listener port allows traffic to the associated backend server.

  6. Run the following command to configure automatic startup:

    systemctl enable keepalived.service