Keepalived:
keepalived是基于vrrp协议实现的一个高可用集群解决方案,可以利用keepalived来解决单点故障问题,使用keepalived实现的高可用集群方案中,一般有两台服务器,一个是MASTER(主服务器),另一个是BACKUP(备用服务器),这个集群中对外提供一个虚拟IP,MASTER服务器会定时发送特定信息给BACKUP服务器,当BACKUP服务器接收不到MASTER发送的消息时,BACKUP服务器会接管虚拟IP,继续提供服务。
当keepalived基于主备模式实现高可用时,提供服务的只有一台主服务器,备服务器只有在主服务器出现故障时才能提供作用,这就造成了资源的浪费,所以keepalived可以通过实现双主模式来实现资源的利用。两台服务器都对外提供服务,并且当一台服务器出现故障时,这台IP地址就会转移到另外一台服务器上,继续提供服务。
keepalived双主模式图:
实验准备:
1、虚拟机 172.18.250.75 反向代理服务器
2、虚拟机 172.18.250.76 反向代理服务器
3、虚拟机 172.18.250.77 Web服务器
4、虚拟机 172.18.250.79 Web服务器
注意:iptables和selinux不会影响keepalived,两台服务时间要同步
一、安装keepalived服务
~]# yum -y install keepalived
编辑配置文件/etc/keepalived/keepalived.conf
~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.74.18 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 151 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.250.99 dev eth0 label eth0:0 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 152 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 172.18.250.100 dev eth0 label eth0:1 } }
另一台上面也配置相同的文件(250.76)
~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.74.18 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 151 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.250.99 dev eth0 label eth0:0 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 152 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 172.18.250.100 dev eth0 label eth0:1 } }
配置完后启动keepalived服务:
]# service keepalived start Starting keepalived: [ OK ] ]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:C5:A4:6B inet addr:172.18.250.99 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 ]# ifconfig eth0:1 Link encap:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.100 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
这样就实现了keepalived的双主模式,测试下如果一台keepalived服务器down,地址会不会转移到另一台服务。
]# service keepalived stop Stopping keepalived: [ OK ] ]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.99 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.100 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
地址能转移。也可以通过vrrp_script脚本来实现keepalived的IP地址转移。
编辑配置文件:
~]# vim /etc/keepalived/keepalived.conf vrrp_script ck_down { script "[[ -e /etc/keepalived/down ]] && exit 1 || exit 0" //如果down文件存在,则返回错误码 weight -5 //返回错误码后权重就减5 interval 2 //2秒钟扫描一次文件 } 在两个虚拟节点中调用此脚本: track_script { ch_down } 重启keepalived服务: ]# service keepalived restart Stopping keepalived: [ OK ] Starting keepalived: [ OK ] 测试: ]# touch down ]# ls down keepalived.conf keepalived.conf.bak ]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.99 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.100 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
已经转移到另一台服务器上来了。
]# rm down ]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:C5:A4:6B inet addr:172.18.250.99 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
双主模型就是这样实现。
二、通过脚本实现反向代理服务nginx的高可用
~]# yum -y install nginx
编辑配置文件:
~]# vim /etc/keepalived/keepalived.conf vrrp_script chk_nginx_down { script "killall -0 nginx" //查看nginx进程是否存在 weight -5 interval 2 } 在两个虚拟节点中添加调用此脚本: track_script { ch_down chk_nginx_down }
重启服务:
]# service keepalived restart Stopping keepalived: [ OK ] Starting keepalived: [ OK ]
测试如果一台nginx服务挂了,地址会不会转移
]# nginx -s stop ]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:C5:A4:6B inet addr:172.18.250.75 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fec5:a46b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:122298 errors:0 dropped:0 overruns:0 frame:0 TX packets:8029 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:39436766 (37.6 MiB) TX bytes:918430 (896.9 KiB)
IP地址立马转移到另一台服务器上,所以实现了nginx的高可用。
keepalived也可以实现Web服务器的高可用,可以参考我的前一篇博客keepalived实现LVS集群的高可用。