跳转至

Nginx HA(Keepalived)


2014-06-02 by dongnan

环境

Nginx-VIP: 172.27.233.94
Nginx-Master: 172.27.233.95/10.0.0.95
Nginx-BACKUP: 172.27.233.96/10.0.0.96
Web1: 10.0.0.87
Web2: 10.0.0.88

目标

使用 KeepaliveNginx 负载均衡器配置HA高可用,确保负载均衡器在架构中不是单点。

配置文件

Keepalived 配置文件

Master

! Configuration File for keepalived

#Global
global_defs {
   router_id nginx-cp
}

vrrp_script chk_nginx {
    script "killall -0 nginx"
    interval 3
    fall 3
    rise 1
    weight -30
}

#VRRP
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    track_interface {
        eth0
        eth1
    }

    virtual_router_id 35
    priority 120
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zongming.net
    }

    track_script {
        chk_nginx
    }

    virtual_ipaddress {
        172.27.233.94
    }
}

Backup

! Configuration File for keepalived

#Global
global_defs {
   router_id nginx-cp
}

vrrp_script chk_nginx {
    script "killall -0 nginx"
    interval 3
    fall 3
    rise 1
    weight -30
}

#VRRP
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    track_interface {
        eth0
        eth1
    }

    virtual_router_id 35
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zongming.net
    }

    track_script {
        chk_nginx
    }

    virtual_ipaddress {
        172.27.233.94
    }
}

测试

网络故障

各种原因导致 keepalive 无法接收 vrrp 网络数据包导致VIP切换。

模拟 master 故障,backup 获得VIP

backup 日志:

May 27 10:53:22 cp-lb2 Keepalived_vrrp[2892]: VRRP_Script(chk_nginx) succeeded
May 27 10:54:38 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 27 10:54:39 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) Entering MASTER STATE
May 27 10:54:39 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) setting protocol VIPs.
May 27 10:54:39 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94
May 27 10:54:39 cp-lb2 Keepalived_healthcheckers[2891]: Netlink reflector reports IP 172.27.233.94 added
May 27 10:54:44 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94

模拟 master 恢复,重新获取 VIP

backup 日志:

May 27 10:56:40 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) Received higher prio advert
May 27 10:56:40 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 27 10:56:40 cp-lb2 Keepalived_vrrp[2892]: VRRP_Instance(VI_1) removing protocol VIPs.
May 27 10:56:40 cp-lb2 Keepalived_healthcheckers[2891]: Netlink reflector reports IP 172.27.233.94 removed

master 日志:

May 27 10:56:40 cp-lb1 Keepalived_vrrp[31865]: VRRP_Script(chk_nginx) succeeded
May 27 10:56:41 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 27 10:56:42 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Entering MASTER STATE
May 27 10:56:42 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) setting protocol VIPs.
May 27 10:56:42 cp-lb1 Keepalived_healthcheckers[31864]: Netlink reflector reports IP 172.27.233.94 added
May 27 10:56:42 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94
May 27 10:56:47 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94

Nginx Crash

各种原因导致 Nginx 进程崩溃导致VIP切换。

模拟 master 故障,backup 获得VIP

master 日志:

May 27 11:01:52 cp-lb1 Keepalived_vrrp[31865]: VRRP_Script(chk_nginx) failed
May 27 11:01:54 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Received higher prio advert
May 27 11:01:54 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 27 11:01:54 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) removing protocol VIPs.
May 27 11:01:54 cp-lb1 Keepalived_healthcheckers[31864]: Netlink reflector reports IP 172.27.233.94 removed

backup 日志:

May 27 11:01:53 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) forcing a new MASTER election
May 27 11:01:54 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 27 11:01:55 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) Entering MASTER STATE
May 27 11:01:55 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) setting protocol VIPs.
May 27 11:01:55 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94
May 27 11:01:55 cp-lb2 Keepalived_healthcheckers[3303]: Netlink reflector reports IP 172.27.233.94 added
May 27 11:02:00 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94

模拟 master 进程恢复,重新获取VIP

master 日志:

May 27 11:04:17 cp-lb1 Keepalived_vrrp[31865]: VRRP_Script(chk_nginx) succeeded
May 27 11:04:17 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) forcing a new MASTER election
May 27 11:04:18 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 27 11:04:19 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Entering MASTER STATE
May 27 11:04:19 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) setting protocol VIPs.
May 27 11:04:19 cp-lb1 Keepalived_vrrp[31865]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.27.233.94
May 27 11:04:19 cp-lb1 Keepalived_healthcheckers[31864]: Netlink reflector reports IP 172.27.233.94 added

backup 日志:

May 27 11:04:17 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) Received higher prio advert
May 27 11:04:17 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 27 11:04:17 cp-lb2 Keepalived_vrrp[3304]: VRRP_Instance(VI_1) removing protocol VIPs.
May 27 11:04:17 cp-lb2 Keepalived_healthcheckers[3303]: Netlink reflector reports IP 172.27.233.94 removed



回到页面顶部