nginx可以在应用层面做到负载均衡,但如果nginx主机挂掉,则整个服务就会down了,因此有必要通过服务器层面的负载保障服务正常运行,这里我们应用keepalived 来实现这个功能。
#yum -y install keepalived
配置文件目录 /etc/keepalived/keepalived.conf
2、配置文件
192.168.3.105
[root@centos7-2 keepalived]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.3.249
}
}
192.168.3.202配置文件
[root@cent7 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.3.249
}
}
3、分别启动keepalived服务
#service keepalived start
ping一下 192.168.3.249 是否正常
访问192.168.3.249,现在是在MASTER 192.168.3.202 的nginx页面
我们来测试下MASTER(192.168.3.202)挂掉的情况下,是否会切换到BACKUP(192.168.3.105)的nginx上去
192.168.3.202上停止keepalived服务
#service keepalived stop
192.168.3.105查看keepalived状态,已切换到MASTER状态
#[root@centos7-2 keepalived]# service keepalived status
Redirecting to /bin/systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-12-09 10:09:04 CST; 5min ago
Process: 53480 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 53481 (keepalived)
Tasks: 3
CGroup: /system.slice/keepalived.service
├─53481 /usr/sbin/keepalived -D
├─53482 /usr/sbin/keepalived -D
└─53483 /usr/sbin/keepalived -D
Dec 09 10:09:13 centos7-2 Keepalived_vrrp[53483]: VRRP_Instance(VI_1) removing protocol VIPs.
Dec 09 10:14:21 centos7-2 Keepalived_vrrp[53483]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.3.249
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:14:22 centos7-2 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.3.249
访问页面如下,OK ! 说明已经切换成功!!
MASTER(192.168.3.202) 再次启动keepalived服务,又返回MASTER状态
[root@cent7 ~]# service keepalived restart
Redirecting to /bin/systemctl restart keepalived.service
[root@cent7 ~]# service keepalived status
Redirecting to /bin/systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-12-09 10:18:35 CST; 5s ago
Process: 53078 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 53080 (keepalived)
Tasks: 3
CGroup: /system.slice/keepalived.service
├─53080 /usr/sbin/keepalived -D
├─53081 /usr/sbin/keepalived -D
└─53082 /usr/sbin/keepalived -D
Dec 09 10:18:35 cent7 Keepalived_vrrp[53082]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Dec 09 10:18:36 cent7 Keepalived_vrrp[53082]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.3.249
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: Sending gratuitous ARP on ens33 for 192.168.3.249
Dec 09 10:18:37 cent7 Keepalived_vrrp[53082]: Sending gratuitous ARP on ens33 for 192.168.3.249
1、修改配置文件
vi /etc/sysconfig/keepalived
配置信息
KEEPALIVED_OPTIONS=”-D -d -S 0”
#vi /etc/rsyslog.conf
末尾增加
local0.* /var/log/keepalived.log
3、重启rsyslog服务
#service rsyslog restart
重启keepalived
#service keepalived restart
4、查看日志信息
#tail -20 /var/log/keepalived.log
2019.12.10 SHENZHEN.NanShan CY Building…