更新时间:2024-05-17 GMT+08:00

配置Keepalived

操作步骤

  1. 后台执行以下命令,修改/etc/keepalived/keepalived.conf文件,配置LVS主节点keepalived。

    vi /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
     
    global_defs {
        router_id master_node
    }
     
    local_address_group laddr_gl {
        192.168.0.157  # LVS主节点地址
    }
     
    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"
    }
     
    # 虚拟ip 端口
    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
            }
        }
    }
  2. 后台执行以下命令,修改/etc/keepalived/keepalived.conf文件,配置LVS备节点keepalived。

    vi /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
     
    global_defs {
        router_id master_node
    }
     
    local_address_group laddr_gl {
        192.168.0.234  #本机地址
    }
     
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id   51
        priority 99  # 需要比主节点低
        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
            }
        }
    }

    步骤3~6,主备LVS节点均需执行。

  3. 后台执行以下命令,修改/etc/keepalived/notify.sh文件。

    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. 执行以下命令,启动keepalived。

    systemctl start keepalived

  5. 执行以下命令,查看配置是否下发成功。

    ipvsadm -Ln

    如果ipvsadm返回的服务器数量和实际不符,请排查监听后端服务器端口是否放通。

  6. 执行以下命令,设置开机启动。

    systemctl enable keepalived.service