主机名 | ip |
---|---|
master | 192.168.220.10 |
backup | 192.168.220.30 |
# 修改master主机名
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
# 关闭防火墙
[root@master ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# setenforce 0
[root@master ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
#修改backup主机名
[root@localhost ~]# hostnamectl set-hostname backup
[root@localhost ~]# bash
# 关闭防火墙和selinux
[root@backup ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@backup ~]# setenforce 0
[root@backup ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
#master主机nginx配置
[root@master ~]# yum install nginx -y
[root@master ~]# cd /usr/share/nginx/html/
[root@master html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@master html]# mv index.html{,.bak}
[root@master html]# echo 'master' > index.html
[root@master html]# cat index.html
master
[root@master ~]# systemctl start nginx.service //开启nginx
#backup主机配置
[root@backup ~]# yum install nginx -y
[root@backup ~]# cd /usr/share/nginx/html/
[root@backup html]# mv index.html{,.bak}
[root@backup html]# echo 'backup' > index.html
[root@backup html]# cat index.html
backup
[root@backup html]# systemctl start nginx.service //启动nginx
[root@master ~]# yum install keepalived -y //安装keepalived
[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# mv keepalived.conf{,.bak} //备份配置原配置文件
[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER //设置为主
interface ens33 //必须和本机网卡名相同
virtual_router_id 51 //虚拟路由id要和备一致
priority 100 //定义优先级,按优先级来决定主备角色,优先级越大越优先
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.220.250 //要使用的ip
}
}
virtual_server 192.168.220.250 80 { //虚拟路由ip 监听端口号为80(需要对其他服务进行高可用就改为指定服务的端口号)
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.220.10 80 { //主ip地址
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.220.30 80 { //备IP地址
weight 1keepa
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#启动keepalived
[root@master ~]# systemctl start keepalived.service
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d2:6d:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.250/32 scope global ens33 //当keepalived启动后 会生成vip
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed2:6da2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@backup ~]# yum install keepalived -y
[root@backup ~]# cd /etc/keepalived/
[root@backup keepalived]# mv keepalived.conf{,.bak}
[root@backup keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP //修改为backup
interface ens33
virtual_router_id 51
priority 90 //修改优先级 因为是备 所以优先级更低
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.220.250
}
}
virtual_server 192.168.220.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.220.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.220.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#启动keepalived
[root@backup ~]# systemctl start keepalived.service
[root@backup ~]# ip a //因为优先级低 所以没有vip
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:8c:8e:d5 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.30/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8c:8ed5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
关闭master主机的keepalived服务
[root@master ~]# systemctl stop keepalived.service
[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:8c:8e:d5 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.30/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.250/32 scope global ens33 //backup主机拥有了vip
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8c:8ed5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
恢复环境
[root@master ~]# systemctl start keepalived.service //正常情况下主的keepalived和nginx都是开启的
[root@backup ~]# systemctl stop nginx.service //备的nginx是关闭的
#此脚本用于控制keepalived是否关闭,当主的nginx异常,而关闭时就自动关闭keepalived服务以便让备能获得vip
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# cat check_nginx.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
# 控制
[root@master scripts]# cat notify.sh
#此脚本用于检查主机是否为master和backup 来控制nginx是否关闭和开启
#!/bin/bash
VIP=$2
#sendmail (){ //可以注释掉这几行 此为异常时发送邮件 实验并不需要 所以删除也没事
# subject="${VIP}'s server keepalived state is translate"
# content="`date +'%F %T'`: `hostname`'s state change to master"
# echo $content | mail -s "$subject" [email protected]
#}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@master scripts]# chmod +x check_nginx.sh
[root@master scripts]# chmod +x notify.sh
[root@master scripts]# ll
总用量 8
-rwxr-xr-x. 1 root root 142 8月 31 11:34 check_nginx.sh
-rwxr-xr-x. 1 root root 667 8月 31 11:37 notify.sh
[root@backup ~]# mkdir /scripts
[root@backup ~]# cd /scripts/
[root@backup scripts]# cat notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" 1470044516@qq.com
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@backup scripts]# chmod +x notify.sh
[root@backup scripts]# ll
总用量 4
-rwxr-xr-x. 1 root root 662 8月 31 11:43 notify.sh
配置主keepalived
[root@master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script nginx_check { //这里添加
script "/scripts/check_nginx.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.220.250
}
track_script { //添加
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.220.250"
}
virtual_server 192.168.220.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.220.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.220.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]# systemctl restart keepalived.service //重启keepalived
配置备keepalived
[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.220.250
}
notify_master "/scripts/notify.sh master 192.168.220.250"
notify_backup "/scripts/notify.sh backup 192.168.220.250"
}
virtual_server 192.168.220.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.220.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.220.250
}
notify_master "/scripts/notify.sh master 192.168.220.250" //添加
notify_backup "/scripts/notify.sh backup 192.168.220.250"
}
virtual_server 192.168.220.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.220.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.220.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup ~]# systemctl restart keepalived.service //重启keepalived
master主机服务状态
[root@master ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset:>
Active: active (running) since Wed 2022-08-31 12:54:46 CST; 8min ago
Process: 4772 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 4769 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 4767 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/>
[root@master ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor pr>
Active: active (running) since Wed 2022-08-31 12:55:52 CST; 8min ago
backup主机服务状态
[root@backup ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset:>
Active: inactive (dead)
[root@backup ~]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor pr>
Active: active (running) since Wed 2022-08-31 12:54:26 CST; 12min ago
Process: 3652 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, s>
关闭主nginx服务
[root@master ~]# systemctl stop nginx.service
[root@master ~]# ip a //vip消失了
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d2:6d:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed2:6da2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#当主的nginx服务异常时 备抢占vip开启nginx服务
[root@backup ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2022-08-31 13:13:59 CST; 12s ago
Process: 48364 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 48362 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 48360 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:8c:8e:d5 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.30/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8c:8ed5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
注:正常情况下主的nginx和keepalived服务是开启的,备的nginx是关闭keeplived服务是开启的。当主的nginx服务异常时通过监控或者脚本用邮件发送告警信息给用户,当需要恢复主的服务时需要手动将主的nginx异常修复后并将nginx和keepalived服务重启,主就拥有了vip并可以正常访问到master
#开启nginx服务和keepalived服务
[root@master ~]# systemctl start nginx.service
[root@master ~]# systemctl restart keepalived.service
[root@master ~]# ip a //重新获取到了vip
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d2:6d:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed2:6da2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#此时备的nginx在vip被抢占后 通过脚本自动关闭nginx
[root@backup ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
主机 | ip |
---|---|
master | 192.168.220.10 |
zabbix | 192.168.220.20 |
backup | 192.168.220.30 |
监控出现异常的两种状态:
所以只需要监控备主机的网卡是否有vip就可以达到zabbix监控脑裂
在backup主机安装zabbix的客户端,在192.168.220.20主机安装zabbix服务端用于使用web网页管理监控
详细步参考zabbix客户端服务端安装
在备主机上编写脚本
[root@backup scripts]# cat check_keepalived.sh
#!/bin/bash
if [ `ip a show ens33 |grep 192.168.220.250|wc -l` -ne 0 ] //网卡与本机网卡名一致,过滤vip
then
echo "1" //当过滤到时为1
else
echo "0" //否则为0
fi
[root@backup scripts]# ./check_keepalived.sh //测试脚本
0
客户端测试脚本
[root@zabbix ~]# zabbix_get -s 192.168.220.30 -k check_keepalived
0
[root@backup etc]# pwd
/usr/local/etc
[root@backup etc]# cat zabbix_agentd.conf //在此文件最后添加
Server=192.168.220.20 //服务端ip
ServerActive=192.168.220.20 //服务端ip
Hostname=test //主机名和文本界面的一致
UserParameter=check_keepalived,/bin/bash /scripts/check_keepalived.sh //脚本参数
[root@backup ~]# pkill zabbix_agentd //重启服务让其生效
[root@backup ~]# zabbix_agentd
让主备产生脑裂
[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 50 //正常情况下此处id主备一致 改为不一致就可以产生脑裂
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.220.250
}
notify_master "/scripts/notify.sh master 192.168.220.250"
notify_backup "/scripts/notify.sh backup 192.168.220.250"
}
virtual_server 192.168.220.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.220.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.220.30 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup ~]# systemctl restart keepalived.service
查看vip
可以看到主备都产生的vip
主
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d2:6d:a2 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed2:6da2/64 scope link noprefixroute
valid_lft forever preferred_lft forever
备
[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:8c:8e:d5 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.30/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8c:8ed5/64 scope link noprefixroute
valid_lft forever preferred_lft forever