公司内部 OA 系统要做线上高可用,避免单点故障,所以计划使用2台虚拟机通过 Keepalived 工具来实现 nginx 的高可用(High Avaiability),达到一台nginx入口服务器宕机,另一台备机自动接管服务的效果。(nginx做反向代理,实现后端应用服务器的负载均衡) 快速搭建请直接跳至 第2节。 1. Keepalived介绍Keepalived是一个基于VRRP协议来实现的服务高可用方案,可以利用其来避免IP单点故障,类似的工具还有heartbeat、 corosync、pacemaker。但是它一般不会单独出现,而是与其它负载均衡技术(如lvs、haproxy、nginx)一起工作来达到集群的 高可用。 1.1 VRRP协议VRRP全称 Virtual Router Redundancy Protocol,即 虚拟路由冗余协议。 可以认为它是实现路由器高可用的容错协议,即将N台提供相同功能的路由器组成一个路由器组(Router Group),这个组里面有一个master和多个backup,但在外界看来就像一台一样,构成虚拟路由器,拥有一个虚拟IP(vip,也就是路由器所 在局域网内其他机器的默认路由),占有这个IP的master实际负责ARP相应和转发IP数据包,组中的其它路由器作为备份的角色处于待命状态。 master会发组播消息,当backup在超时时间内收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个 backup当master,保证路由器的高可用。 在VRRP协议实现里,虚拟路由器使用 00-00-5E-00-01-XX 作为虚拟MAC地址,XX就是唯一的 VRID (Virtual Router IDentifier),这个地址同一时间只有一个物理路由器占用。在虚拟路由器里面的物理路由器组里面通过多播IP地址 224.0.0.18 来定时发送通告消息。每个Router都有一个 1-255 之间的优先级别,级别最高的(highest priority)将成为主控(master)路由器。通过降低master的优先权可以让处于backup状态的路由器抢占(pro-empt)主路由 器的状态,两个backup优先级相同的IP地址较大者为master,接管虚拟IP。
与heartbeat/corosync等比较
1.2 Keepalived + nginxkeepalived可以认为是VRRP协议在Linux上的实现,主要有三个模块,分别是core、check和vrrp。core模块为 keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来 实现VRRP协议的。本文基于如下的拓扑图: +-------------+ | uplink | +-------------+ | + MASTER keep|alived BACKUP 172.29.88.224 172.29.88.222 172.29.88.225 +-------------+ +-------------+ +-------------+ | nginx01 |----| virtualIP |----| nginx02 | +-------------+ +-------------+ +-------------+ | +------------------+------------------+ | | | +-------------+ +-------------+ +-------------+ | web01 | | web02 | | web03 | +-------------+ +-------------+ +-------------+
2. keepalived实现nginx高可用2.1安装我的环境是CentOS 6.2 X86_64,直接通过yum方式安装最简单: # yum install -y keepalived # keepalived -v Keepalived v1.2.13 (03/19,2015) 2.2 nginx监控脚本该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。
#!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/local/bin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/keepalived stop fi fi 你也可以根据自己的业务需求,总结出在什么情形下关闭keepalived,如 curl 主页连续2个5s没有响应则切换: #!/bin/bash # curl -IL http://localhost/member/login.htm # curl --data "memberName=fengkan&password=22" http://localhost/member/login.htm count = 0 for (( k=0; k<2; k++ )) do check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost/login.html -o /dev/null ) if [ "$check_code" != "200" ]; then count = count +1 continue else count = 0 break fi done if [ "$count" != "0" ]; then # /etc/init.d/keepalived stop exit 1 else exit 0 fi 2.3 keepalived.conf! Configuration File for keepalived global_defs { notification_email { zhouxiao@example.com itsection@example.com } notification_email_from itsection@example.com smtp_server mail.example.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { # script "killall -0 nginx" script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state MASTER interface eth0 mcast_src_ip 172.29.88.224 virtual_router_id 51 priority 101 advert_int 2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.29.88.222 } track_script { chk_nginx } }
在其它备机BACKUP上,只需要改变 service keepalived restart 2.4 配置选项说明global_defs
vrrp_instance
vrrp_script
这里要提示一下script一般有2种写法:
上文 vrrp_script 配置部分,
其他情况,原本配置的优先级不变,即配置文件中priority对应的值。 提示:
以上可以做到利用脚本检测业务进程的状态,并动态调整优先级从而实现主备切换。 配置结束
在默认的keepalive.conf里面还有 virtual_server,real_server 这样的配置,我们这用不到,它是为lvs准备的。 notify 2.5 nginx配置
当然nginx没有什么可配置的,因为它与keepalived并没有联系。但记住,2台nginx服务器上的配置应该是完全一样的(rsync同步),这样才能做到对用户透明,nginx.conf 里面的 3. 测试根据上面的配置,初始化状态:172.29.88.224 (itoatest1,MASTER,101),172.29.88.222(itoatest2,BACKUP,100),nginx和 keepalived都启动,虚拟IP 172.29.88.222 在 itoatest1上: # 使用ip命令配置的地址,ifconfig查看不了 [root@itoatest1 nginx-1.6]# ip a|grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 172.29.88.224/24 brd 172.29.88.255 scope global eth0 inet 172.29.88.222/32 scope global eth0
浏览器访问 172.29.88.222 或域名,OK。 [root@localhost keepalived]# ip a|grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 172.29.88.224/24 brd 172.29.88.255 scope global eth0 vip消失,漂移到 itoatest2:
## itoatest1 Jun 5 16:44:01 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222 Jun 5 16:44:06 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222 Jun 5 16:44:46 itoatest1 Keepalived_vrrp[44875]: VRRP_Script(chk_nginx) failed Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Received higher prio advert Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Entering BACKUP STATE Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) removing protocol VIPs. Jun 5 16:44:48 itoatest1 Keepalived_healthcheckers[44874]: Netlink reflector reports IP 172.29.88.222 removed ## itoatest2 Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Transition to MASTER STATE Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Received higher prio advert Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Entering BACKUP STATE Jun 5 16:44:48 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) forcing a new MASTER election Jun 5 16:44:48 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) forcing a new MASTER election Jun 5 16:44:49 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Transition to MASTER STATE Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Entering MASTER STATE Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) setting protocol VIPs. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222 Jun 5 16:44:50 itoatest2 Keepalived_healthcheckers[35554]: Netlink reflector reports IP 172.29.88.222 added Jun 5 16:44:55 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222 你也可以通过在两台服务器上抓包来查看 优先级priority 的变化 ## itoatest1 上 ## 直接输出,或后加 -w itoatest-kl.cap存入文件用wireshark查看 # tcpdump -vvv -n -i eth0 dst 224.0.0.18 and src 172.29.88.224 转载请保留固定链接: https://linuxeye.com/architecture/2668.html |