目录
一、双主模式原理
1. nginx+keepalived主备模式缺点
2. 主备模式和双主模式的区别
二、配置文件
1. nginx01的keepalived.conf
2. nginx02的keepalived.conf
3. 检测nginx存活脚本文件nginx_check.sh
三、测试准备
1. 启动nginx01、nginx02
2. 启动keepalived
3. 查看网卡信息
四、测试
1. 测试VIP访问
2. 测试nginx检测脚本
3. 模拟nginx01宕机
4. 模拟nginx01修复宕机
主机名 | 真实IP | VIP |
nginx01 | 11.0.1.31 | 11.0.1.29 |
nginx02 | 11.0.1.32 | 11.0.1.30 |
双机主备存在一个问题,不管keepalived存活在哪台设备上,总有一台设备是空闲的,资源利用率并不高,双主模式可以很好解决该问题。
双主模式可以实现以下效果:
11.0.1.29 VIP1 默认访问11.0.1.31
11.0.1.30 VIP2 默认访问11.0.1.32
当其中一个nginx宕机,两个虚拟IP仍然可以正常使用,此时存活的nginx同是绑定2个虚拟IP
双主与主备的核心区别就是,主备模式只有一个vrrp实例(默认抢占模式:1个master1个backup),双主机需要两个虚拟IP,两个vrrp实例,两者互为主备,vrrp1实例中(nginx01为master,nginx02为backup)、vrrp2实例中(nginx01为backup,nginx02为master)
[root@nginx01 sbin]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id real-server1
script_user root
enable_script_security
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 151
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
11.0.1.29
}
track_script {
chk_nginx
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 152
priority 50
advert_int 5
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
11.0.1.30
}
track_script {
chk_nginx
}
}
[root@nginx02 sbin]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id real-server2
script_user root
enable_script_security
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 151
priority 50
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
11.0.1.29
}
track_script {
chk_nginx
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 152
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
11.0.1.30
}
track_script {
chk_nginx
}
}
[root@nginx01 sbin]# cat /etc/keepalived/nginx_check.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi
cd /usr/local/ngins/sbin
./nginx
请确保真实IP可以访问到nginx01、nginx02
systemctl start keepalived
nginx01的网卡信息
[root@nginx01 sbin]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:60:05:0e brd ff:ff:ff:ff:ff:ff
inet 11.0.1.31/24 brd 11.0.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 11.0.1.29/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::a344:1c87:5caf:42cd/64 scope link noprefixroute
valid_lft forever preferred_lft forever
nginx02的网卡信息
[root@nginx02 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b9:97:26 brd ff:ff:ff:ff:ff:ff
inet 11.0.1.32/24 brd 11.0.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 11.0.1.30/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::4ae3:fc87:6344:7062/64 scope link noprefixroute
valid_lft forever preferred_lft forever
可以看出VIP成功绑定到各自的真实服务器IP
关闭nginx01上的nginx服务,查看脚本是否可以正常重启nginx
[root@nginx01 sbin]# ./nginx -s stop
[root@nginx01 sbin]# ps -aux | grep nginx
root 3598 0.0 0.0 20540 612 ? Ss 22:48 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3600 0.0 0.0 20984 1304 ? S 22:48 0:00 nginx: worker process
root 3650 0.0 0.0 112824 988 pts/0 R+ 22:48 0:00 grep --color=auto nginx
重启成功,脚本生效。
停止nginx01上的keepalived进程或者直接将nginx01关机(效果一样),模拟nginx01宕机
[root@nginx01 sbin]# systemctl stop keepalived
nginx02网卡信息
nginx01网卡信息
访问nginx01的VIP11.0.1.29,但实际是11.0.1.32响应,因为VIP1漂移到nginx02上,此时nginx02同时绑定VIP1、VIP2。即使nginx01宕机,ngin01的VIP11.0.1.29仍然可以正常使用。
重启nginx01的keepalived(或开机)
访问恢复正常
模拟nginx02宕机效果也是一样,不再重复实验。
参考文档:
玩转企业集群运维管理系列(十二):Keepalived 双主、非抢占模式及脑裂问题详解-腾讯云开发者社区-腾讯云