[Reprint] Nginx + Keepalived site achieve high availability

Nginx + Keepalived site achieve high availability

https://linux.cn/article-5715-1.html

 

Author:  sean 

| 2015-07-01 10:34 Comments:  3  Favorites:  11    

Internal OA system to do online availability, no single point of failure, so plan on using 2 nginx virtual machines to achieve high availability (High Avaiability) by Keepalived tool to reach an entrance nginx server goes down, another backup the effect of automatically taking over the service. (Nginx do reverse proxy, load balancing back-end application servers) quickly erected skip directly to Section 2.

Introduction 1. Keepalived

Keepalived is a high availability service program is implemented based VRRP agreement, they may use their IP to avoid single points of failure, there is a similar tool heartbeat, corosync, pacemaker. But it usually does not occur alone, but with other load balancing technology (such as lvs, haproxy, nginx) work together to achieve high availability cluster.

1.1 VRRP protocol

VRRP stands for Virtual Router Redundancy Protocol, namely  virtual routing redundancy protocol . It can be considered highly available, fault-tolerant protocol router, sets about to N routers provide the same functionality of a router group (Router Group), the group which has a master and multiple backup, but it seems like a, like in the outside world constitute a virtual router has a virtual IP (vip, which is the default route to other machines within the router LAN where), possession of this IP master actually responsible for ARP appropriate and forwarding IP packets, set the other router role as a backup in standby. master will send a multicast message when backup vrrp not receive the package within the timeout period is considered master dawdle out, then you need a backup when the master VRRP election according to priority, to ensure high availability router.

In VRRP protocol, the virtual router uses 00-00-5E-00-01-XX as a virtual MAC address, XX is the only VRID (Virtual Router IDentifier), that address the same time there is only one physical router occupied. In the virtual router inside the physical router which group multicast IP address 224.0.0.18 through the advertisement message transmitted to the timing. Each Router has a priority between 1-255, the highest level (highest priority) will be the master (master) router. It allows the router is a backup by reducing the priority of the master preemption (pro-empt) primary router state, two identical backup priority highest IP address as master, taking over the virtual IP.

nginx-keepalived-vrrp.jpg

nginx-keepalived-vrrp.jpg

Compared with the heartbeat / corosync etc.

Direct excerpt from  http://www.linuxidc.com/Linux/2013-08/89227.htm  :

Heartbeat、Corosync、Keepalived这三个集群组件我们到底选哪个好,首先我想说明的是,Heartbeat、Corosync是属于同一类型,Keepalived与Heartbeat、Corosync,根本不是同一类型的。Keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol,简称VRRP);Heartbeat或Corosync是基于主机或网络服务的高可用方式;简单的说就是,Keepalived的目的是模拟路由器的高可用,Heartbeat或Corosync的目的是实现Service的高可用。

所以一般Keepalived是实现前端高可用,常用的前端高可用的组合有,就是我们常见的LVS+Keepalived、Nginx+Keepalived、HAproxy+Keepalived。而Heartbeat或Corosync是实现服务的高可用,常见的组合有Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd 实现Web服务器的高可用、Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL 实现MySQL服务器的高可用。总结一下,Keepalived中实现轻量级的高可用,一般用于前端高可用,且不需要共享存储,一般常用于两个节点的高可用。而Heartbeat(或Corosync)一般用于服务的高可用,且需要共享存储,一般用于多节点的高可用。这个问题我们说明白了。

又有博友会问了,那heartbaet与corosync我们又应该选择哪个好啊,我想说我们一般用corosync,因为corosync的运行机制更优于heartbeat,就连从heartbeat分离出来的pacemaker都说在以后的开发当中更倾向于corosync,所以现在corosync+pacemaker是最佳组合。

1.2 Keepalived + nginx

keepalived可以认为是VRRP协议在Linux上的实现,主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。本文基于如下的拓扑图:

  1. +-------------+
  2. | uplink |
  3. +-------------+
  4. |
  5. +
  6. MASTER keep|alived BACKUP
  7. 172.29.88.224 172.29.88.222 172.29.88.225
  8. +-------------+ +-------------+ +-------------+
  9. | nginx01 |----| virtualIP |----| nginx02 |
  10. +-------------+ +-------------+ +-------------+
  11. |
  12. +------------------+------------------+
  13. | | |
  14. +-------------+ +-------------+ +-------------+
  15. | web01 | | web02 | | web03 |
  16. +-------------+ +-------------+ +-------------+

2. keepalived实现nginx高可用

2.1 安装

我的环境是CentOS 6.2 X86_64,直接通过yum方式安装最简单:

  1. # yum install -y keepalived
  2. # keepalived -v
  3. Keepalived v1.2.13 (03/19,2015)

2.2 nginx监控脚本

该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。

/etc/keepalived/check_nginx.sh :

  1. #!/bin/bash
  2. counter=$(ps -C nginx --no-heading|wc -l)
  3. if [ "${counter}" = "0" ]; then
  4. /usr/local/bin/nginx
  5. sleep 2
  6. counter=$(ps -C nginx --no-heading|wc -l)
  7. if [ "${counter}" = "0" ]; then
  8. /etc/init.d/keepalived stop
  9. fi
  10. fi

你也可以根据自己的业务需求,总结出在什么情形下关闭keepalived,如 curl 主页连续2个5s没有响应则切换:

  1. #!/bin/bash
  2. # curl -IL http://localhost/member/login.htm
  3. # curl --data "memberName=fengkan&password=22" http://localhost/member/login.htm
  4. count = 0
  5. for (( k=0; k<2; k++ ))
  6. do
  7. check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost/login.html -o /dev/null )
  8. if [ "$check_code" != "200" ]; then
  9. count = count +1
  10. continue
  11. else
  12. count = 0
  13. break
  14. fi
  15. done
  16. if [ "$count" != "0" ]; then
  17. # /etc/init.d/keepalived stop
  18. exit 1
  19. else
  20. exit 0
  21. fi

2.3 keepalived.conf

  1. ! Configuration File for keepalived
  2. global_defs {
  3. notification_email {
  4. zhouxiao@example.com
  5. itsection@example.com
  6. }
  7. notification_email_from itsection@example.com
  8. smtp_server mail.example.com
  9. smtp_connect_timeout 30
  10. router_id LVS_DEVEL
  11. }
  12. vrrp_script chk_nginx {
  13. # script "killall -0 nginx"
  14. script "/etc/keepalived/check_nginx.sh"
  15. interval 2
  16. weight -5
  17. fall 3
  18. rise 2
  19. }
  20. vrrp_instance VI_1 {
  21. state MASTER
  22. interface eth0
  23. mcast_src_ip 172.29.88.224
  24. virtual_router_id 51
  25. priority 101
  26. advert_int 2
  27. authentication {
  28. auth_type PASS
  29. auth_pass 1111
  30. }
  31. virtual_ipaddress {
  32. 172.29.88.222
  33. }
  34. track_script {
  35. chk_nginx
  36. }
  37. }

在其它备机BACKUP上,只需要改变 state MASTER -> state BACKUPpriority 101 -> priority 100mcast_src_ip 172.29.88.224 -> mcast_src_ip 172.29.88.225即可。

  1. service keepalived restart

2.4 配置选项说明

global_defs

  • notification_email : keepalived在发生诸如切换操作时需要发送email通知地址,后面的 smtp_server 相比也都知道是邮件服务器地址。也可以通过其它方式报警,毕竟邮件不是实时通知的。
  • router_id : 机器标识,通常可设为hostname。故障发生时,邮件通知会用到

vrrp_instance

  • state : 指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为MASTER
  • interface : 实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
  • mcast_src_ip : 发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址
  • virtual_router_id : 这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
  • priority : 设置本节点的优先级,优先级高的为master
  • advert_int : 检查间隔,默认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常
  • authentication : 定义认证方式和密码,主从必须一样
  • virtual_ipaddress : 这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
  • track_script : 引用VRRP脚本,即在 vrrp_script 部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。

vrrp_script

告诉 keepalived 在什么情况下切换,所以尤为重要。可以有多个 vrrp_script

  • script : 自己写的检测脚本。也可以是一行命令如killall -0 nginx
  • interval 2 : 每2s检测一次
  • weight -5 : 检测失败(脚本返回非0)则优先级 -5
  • fall 2 : 检测连续 2 次失败才算确定是真失败。会用weight减少优先级(1-255之间)
  • rise 1 : 检测 1 次成功就算成功。但不修改优先级

这里要提示一下script一般有2种写法:

  1. 通过脚本执行的返回结果,改变优先级,keepalived继续发送通告消息,backup比较优先级再决定
  2. 脚本里面检测到异常,直接关闭keepalived进程,backup机器接收不到advertisement会抢占IP

上文 vrrp_script 配置部分,killall -0 nginx属于第1种情况,/etc/keepalived/check_nginx.sh属于第2种情况(脚本中关闭keepalived)。个人更倾向于通过shell脚本判断,但有异常时exit 1,正常退出exit 0,然后keepalived根据动态调整的 vrrp_instance 优先级选举决定是否抢占VIP:

  • 如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加
  • 如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少

其他情况,原本配置的优先级不变,即配置文件中priority对应的值。

提示:

  1. 优先级不会不断的提高或者降低
  2. 可以编写多个检测脚本并为每个检测脚本设置不同的weight(在配置中列出就行)
  3. 不管提高优先级还是降低优先级,最终优先级的范围是在[1,254],不会出现优先级小于等于0或者优先级大于等于255的情况
  4. 在MASTER节点的 vrrp_instance 中 配置 nopreempt ,当它异常恢复后,即使它 prio 更高也不会抢占,这样可以避免正常情况下做无谓的切换

以上可以做到利用脚本检测业务进程的状态,并动态调整优先级从而实现主备切换。

配置结束

在默认的keepalive.conf里面还有 virtual_server,real_server 这样的配置,我们这用不到,它是为lvs准备的。 notify 可以定义在切换成MASTER或BACKUP时执行的脚本,如有需求请自行google。

2.5 nginx配置

当然nginx没有什么可配置的,因为它与keepalived并没有联系。但记住,2台nginx服务器上的配置应该是完全一样的(rsync同步),这样才能做到对用户透明,nginx.conf 里面的 server_name 尽量使用域名来代替,然后dns解析这个域名到虚拟IP 172.29.88.222。

For more content on nginx configuration, see  here  .

3. Test

According to the above configuration, initialization state: 172.29.88.224 (itoatest1, MASTER, 101), 172.29.88.222 (itoatest2, BACKUP, 100), nginx keepalived and are activated in the virtual itoatest1 IP 172.29.88.222:

  1. # 使用ip命令配置的地址,ifconfig查看不了
  2. [root@itoatest1 nginx-1.6]# ip a|grep eth0
  3. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  4. inet 172.29.88.224/24 brd 172.29.88.255 scope global eth0
  5. inet 172.29.88.222/32 scope global eth0

Browser access 172.29.88.222 or domain name, OK.

Directly off nginx on itoatest1: /usr/local/nginx-1.6/sbin/nginx -s stop:

  1. [root@localhost keepalived]# ip a|grep eth0
  2. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  3. inet 172.29.88.224/24 brd 172.29.88.255 scope global eth0

vip disappear, drift to itoatest2:

nginx-keepalived-vip.png

nginx-keepalived-vip.png

At the same time it can be seen on both servers  /var/log/messages:

  1. ## itoatest1
  2. Jun 5 16:44:01 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222
  3. Jun 5 16:44:06 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222
  4. Jun 5 16:44:46 itoatest1 Keepalived_vrrp[44875]: VRRP_Script(chk_nginx) failed
  5. Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Received higher prio advert
  6. Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Entering BACKUP STATE
  7. Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) removing protocol VIPs.
  8. Jun 5 16:44:48 itoatest1 Keepalived_healthcheckers[44874]: Netlink reflector reports IP 172.29.88.222 removed
  9. ## itoatest2
  10. Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Transition to MASTER STATE
  11. Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Received higher prio advert
  12. Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Entering BACKUP STATE
  13. Jun 5 16:44:48 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) forcing a new MASTER election
  14. Jun 5 16:44:48 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) forcing a new MASTER election
  15. Jun 5 16:44:49 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Transition to MASTER STATE
  16. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Entering MASTER STATE
  17. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) setting protocol VIPs.
  18. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222
  19. Jun 5 16:44:50 itoatest2 Keepalived_healthcheckers[35554]: Netlink reflector reports IP 172.29.88.222 added
  20. Jun 5 16:44:55 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222

You can also view the change priority by priority packet capture on both servers:

  1. ## itoatest1
  2. ## 直接输出,或后加 -w itoatest-kl.cap存入文件用wireshark查看
  3. # tcpdump -vvv -n -i eth0 dst 224.0.0.18 and src 172.29.88.224

nginx-keepalived-prio.png

nginx-keepalived-prio.png

reference

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12085266.html