主节点ip:192.168.3.122,备节点ip:192.168.3.123

主节点配置如下:

! Configuration file for keepalived
global_defs {
   notification_email {
    [email protected]
        }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
#监控huawei-grpc
vrrp_script chk_nginx {
    script "/etc/keepalived/script/monitor_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER  #主从设置
    interface em1  #网卡名
    virtual_router_id 1
    mcast_src_ip 192.168.3.122 #本机ip
    priority 100  #从机小于主机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        192.168.3.120  #VIP 的IP
    }
    #track_script {
    #chk_nginx
    #}

}

virtual_server 192.168.3.120 50051 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.3.122 50051 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            retry 3
            delay_before_retry 3
            connect_port 50051
        }
    }
 real_server 192.168.3.123 50051 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 50051
        }
    }
}

备节点配置如下:

! Configuration file for keepalived
global_defs {
   notification_email {
    [email protected]
        }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
#监控huawei-grpc
vrrp_script chk_nginx {
    script "/etc/keepalived/script/monitor_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP  #主从设置
    interface em1  #网卡名
    virtual_router_id 1
    mcast_src_ip 192.168.3.123 #本机ip
    priority 50  #从机小于主机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        192.168.3.120  #VIP 的IP
    }
    #track_script {
    #chk_nginx
    #}

}

virtual_server 192.168.3.120 50051 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.3.122 50051 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            retry 3
            delay_before_retry 3
            connect_port 50051
        }
    }
 real_server 192.168.3.123 50051 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 50051
        }
    }
}