nginx+keepalived(双主)
一、环境
nginx1 192.168.40.211 nginx2 192.168.40.132 vip1 192.168.40.223 主为keep1,从为keep2 vip2 192.168.40.222 主为keep2,从为keep1
二、keepalived配置文件
nginx1节点
cat /etc/keepalived/keepalived.conf
global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 route_id LVS_DEVEL } vrrp_script_chk_nginx { script "/etc/keepalived/ck_ng.sh" interval 2 weight 2 } #VIP1 vrrp_instance VI_1 { state MASTER interface ens33 lvs_sync_daemon_interface ens33 virtual_router_id 151 priority 100 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.223 } track_script { chk_nginx } } #VIP2 vrrp_instance VI_2 { state BACKUP interface ens33 lvs_sync_daemon_interface ens33 virtual_router_id 152 priority 90 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.40.222 } track_script { chk_nginx } }
nginx2 节点
#vi /etc/keepalived/keepalived.conf global_defs { notification_email { root@192.168.40.132 } notification_email_from keepalived@192.168.40.132 smtp_server 127.0.0.1 smtp_connect_timeout 30 route_id LVS_DEVEL } vrrp_script_chk_nginx { script "/etc/keepalived/ck_ng.sh" interval 2 weight 2 } #VIP1 vrrp_instance VI_1 { state BACKUP interface ens33 lvs_sync_daemon_interface ens33 virtual_router_id 151 priority 90 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.223 } track_script { chk_nginx } } #VIP2 vrrp_instance VI_2 { state MASTER interface ens33 lvs_sync_daemon_interface ens33 virtual_router_id 152 priority 100 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.40.222 } track_script { chk_nginx } }
三、nginx检测脚本(两台机子一样)
cat /etc/keepalived/ck_ng.sh
#!/bin/bash #检查nginx进程是否存在 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then #尝试启动一次nginx,停止5秒后再次检测 service nginx start sleep 5 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then #如果启动没成功,就杀掉keepalive触发主备切换 service keepalived stop fi fi
chmod +x /etc/keepalived/ck_ng.sh
四、测试
ip a (nginx1)
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:17:c0:db brd ff:ff:ff:ff:ff:ff inet 192.168.40.211/24 brd 192.168.40.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.40.223/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe17:c0db/64 scope link valid_lft forever preferred_lft forever
ip a (nginx2)
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.40.222/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe5e:baa3/64 scope link valid_lft forever preferred_lft forever
停掉nginx1的keepalived,此时nginx1的vip转移到nginx2,如下
ip a
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.40.222/32 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.40.223/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe5e:baa3/64 scope link valid_lft forever preferred_lft forever
两个虚拟IP跑到一台机子上,实验成功