当使用Nginx作为应用服务器前端软负载的时候,可以通过KeepAlived来实现虚拟IP(Virtual IP, VIP)在主、备节点之前的漂移,其中VIP需要在申请服务器的时候进行创建。
1)当主节点Nginx服务无法启动,或者主节点服务器宕机,VIP将漂移到备用节点;
2)当主节点服务恢复(服务器启动、KeepAlived和Nginx服务正常运行),备用节点将会进行备用状态,并移除VIP,VIP将漂移回主节点。
在这个切换过程中,正常情况下,前端用户是无感知的。
备用节点
# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-01-07 08:43:32 CST; 1min 21s ago
Process: 28260 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 28262 (keepalived)
Tasks: 2
CGroup: /system.slice/keepalived.service
├─28262 /usr/local/keepalived/sbin/keepalived -D
└─28264 /usr/local/keepalived/sbin/keepalived -D
Jan 07 08:43:36 ngServer2 Keepalived_vrrp[28264]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:43:41 ngServer2 Keepalived_vrrp[28264]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.xxx.xxx.17
Jan 07 08:43:41 ngServer2 Keepalived_vrrp[28264]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:43:41 ngServer2 Keepalived_vrrp[28264]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:43:41 ngServer2 Keepalived_vrrp[28264]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:43:41 ngServer2 Keepalived_vrrp[28264]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:43:41 ngServer2 Keepalived_vrrp[28264]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:26 ngServer2 Keepalived_vrrp[28264]: (VI_1) Master received advert from 10.xxx.xxx.15 with higher priority 122, ours 102
Jan 07 08:44:26 ngServer2 Keepalived_vrrp[28264]: (VI_1) Entering BACKUP STATE
Jan 07 08:44:26 ngServer2 Keepalived_vrrp[28264]: (VI_1) removing VIPs.
主节点
# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-01-07 08:44:24 CST; 16s ago
Process: 29790 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 29792 (keepalived)
Tasks: 2
CGroup: /system.slice/keepalived.service
├─29792 /usr/local/keepalived/sbin/keepalived -D
└─29793 /usr/local/keepalived/sbin/keepalived -D
Jan 07 08:44:28 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:28 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:28 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:28 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:33 ngServer1 Keepalived_vrrp[29793]: (VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.xxx.xxx.17
Jan 07 08:44:33 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:33 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:33 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:33 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17
Jan 07 08:44:33 ngServer1 Keepalived_vrrp[29793]: Sending gratuitous ARP on eth0 for 10.xxx.xxx.17