双机双主


正常情况下nginx是一台提供服务,另外一条备份,

互为主备要引入两个VIP,如mysql双主,nginx双主,这样要引入两个VIP,也就是还需要引入

virtual_ipaddress {
192.168.179.199
192.1681.79.188
}

只是代表这个实例有两个VIP

 

192.168.179.102


[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
router_id real-server1
script_user root
enable_script_security
}

vrrp_script chk_nginx {
script "/data/shell/check_nginx_status.sh"
interval 2
}


vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 151
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111

}
virtual_ipaddress {
192.168.179.199
}

track_script {
chk_nginx
}
}


vrrp_instance VI_2 {
state BACKUP
interface ens32
virtual_router_id 152
priority 50
advert_int 5
authentication {
auth_type PASS
auth_pass 2222

}
virtual_ipaddress {
192.168.179.198
}

track_script {
chk_nginx
}
}


[root@localhost ~]# cat /data/shell/check_nginx_status.sh
#!/bin/bash
nginx_status=`ps -ef | grep nginx | grep -v grep | grep -v check | wc -l`

if [ $nginx_status -eq 0 ];then
systemctl restart keepalived.service
fi


先启动nginx然后keepalived
[root@localhost ~]# ip a
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:0e:1a:bf brd ff:ff:ff:ff:ff:ff
inet 192.168.179.102/24 brd 192.168.179.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.179.199/32 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::2d42:a0b1:1cdc:74c0/64 scope link
valid_lft forever preferred_lft forever

 

192.168.179.103


[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
router_id real-server2
script_user root
enable_script_security
}

vrrp_script chk_nginx {
script "/data/shell/check_nginx_status.sh"
interval 2
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 151
priority 50
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.179.199
}

track_script {
chk_nginx
}
}


vrrp_instance VI_2 {
state MASTER
interface ens32
virtual_router_id 152
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 2222

}
virtual_ipaddress {
192.168.179.198
}

track_script {
chk_nginx
}
}


[root@localhost ~]# ip a
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:61:90:c1 brd ff:ff:ff:ff:ff:ff
inet 192.168.179.103/24 brd 192.168.179.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.179.198/32 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::f54d:5639:6237:2d0e/64 scope link
valid_lft forever preferred_lft forever

 

102上干掉Nginx


 102操作以及日志

[root@localhost ~]# pkill nginx
[root@localhost ~]#


Nov 21 19:12:49 localhost Keepalived_vrrp[52348]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Nov 21 19:12:49 localhost Keepalived[52346]: Stopping
Nov 21 19:12:49 localhost systemd: Stopping LVS and VRRP High Availability Monitor...
Nov 21 19:12:49 localhost Keepalived_healthcheckers[52347]: Stopped
Nov 21 19:12:50 localhost Keepalived_vrrp[52348]: Stopped
Nov 21 19:12:50 localhost Keepalived[52346]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Nov 21 19:12:50 localhost systemd: start request repeated too quickly for keepalived.service
Nov 21 19:12:50 localhost systemd: Failed to start LVS and VRRP High Availability Monitor.
Nov 21 19:12:50 localhost systemd: Unit keepalived.service entered failed state.
Nov 21 19:12:50 localhost systemd: keepalived.service failed.

103上查看日志,可以看到199跑到103上了 ,下面是103日志和ip

Nov 21 19:12:45 localhost Keepalived_vrrp[7582]: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: VRRP_Instance(VI_1) setting protocol VIPs.
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: Sending gratuitous ARP on ens32 for 192.168.179.199
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.179.199
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: Sending gratuitous ARP on ens32 for 192.168.179.199
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: Sending gratuitous ARP on ens32 for 192.168.179.199
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: Sending gratuitous ARP on ens32 for 192.168.179.199
Nov 21 19:12:50 localhost Keepalived_vrrp[7582]: Sending gratuitous ARP on ens32 for 192.168.179.199
Nov 21 19:12:55 localhost Keepalived_vrrp[7582]: Sending gratuitous ARP on ens32 for 192.168.179.199
Nov 21 19:12:55 localhost Keepalived_vrrp[7582]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.179.199
[root@localhost ~]# ip a
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:61:90:c1 brd ff:ff:ff:ff:ff:ff
inet 192.168.179.103/24 brd 192.168.179.255 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.179.198/32 scope global ens32
valid_lft forever preferred_lft forever
inet 192.168.179.199/32 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::f54d:5639:6237:2d0e/64 scope link
valid_lft forever preferred_lft forever

Keepalived 双主模式_nginx

Keepalived 双主模式_ipad_02