主机名 | ip |
---|
master | 192.168.220.10 | backup | 192.168.220.30 |
准备工作
[root@localhost ~]
[root@localhost ~]
[root@master ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]
[root@master ~]
[root@localhost ~]
[root@localhost ~]
[root@backup ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@backup ~]
[root@backup ~]
安装并配置nginx
[root@master ~]
[root@master ~]
[root@master html]
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@master html]
[root@master html]
[root@master html]
master
[root@master ~]
浏览器访问
[root@backup ~]
[root@backup ~]
[root@backup html]
[root@backup html]
[root@backup html]
backup
[root@backup html]
浏览器访问
安装并配置keepalived
为主安装配置keepalived
[root@master ~]
[root@master ~]
[root@master keepalived]
[root@master keepalived]
! 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
}
}
}
[root@master ~]
[root@master ~]
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
为备安装配置keepalived
[root@backup ~]
[root@backup ~]
[root@backup keepalived]
[root@backup keepalived]
! 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
}
}
}
[root@backup ~]
[root@backup ~]
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
浏览器上使用vip进行访问 可以访问到master
使备主机获得vip并访问到对应网站
关闭master主机的keepalived服务
[root@master ~]
[root@backup ~]
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
浏览器访问vip 可以看到backup主机的对应网页
配置脚本
恢复环境
[root@master ~]
[root@backup ~]
为主编写脚本
[root@master ~]
[root@master ~]
[root@master scripts]
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]
VIP=$2
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]
[root@master scripts]
[root@master scripts]
总用量 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 ~]
[root@backup ~]
[root@backup scripts]
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]
[root@backup scripts]
总用量 4
-rwxr-xr-x. 1 root root 662 8月 31 11:43 notify.sh
配置keepalived加入监控脚本的配置
配置主keepalived
[root@master ~]
! 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 ~]
配置备keepalived
[root@backup ~]
! 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 ~]
! 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 ~]
实验
master主机服务状态
[root@master ~]
● 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 ~]
● 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 ~]
● 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 ~]
● 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 ~]
[root@master ~]
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
[root@backup ~]
● 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 ~]
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
[root@master ~]
[root@master ~]
[root@master ~]
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
通过浏览器访问vip
[root@backup ~]
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
zabbix监控脑裂
主机 | ip |
---|
master | 192.168.220.10 | zabbix | 192.168.220.20 | backup | 192.168.220.30 |
监控出现异常的两种状态:
- 正常情况下master主机nginx和keepalived为开启,backup主机keepalived为开启nginx为关闭,当master主机发生异常时backup主机通过脚本抢夺vip
- 当出现脑裂时主备的两台主机都会有vip
所以只需要监控备主机的网卡是否有vip就可以达到zabbix监控脑裂
在backup主机安装zabbix的客户端,在192.168.220.20主机安装zabbix服务端用于使用web网页管理监控 详细步参考zabbix客户端服务端安装
编写脚本
在备主机上编写脚本
[root@backup scripts]
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]
0
客户端测试脚本
[root@zabbix ~]
0
添加需要监控的主机
添加监控项
添加触发器
勾选所有级别警告
配置backup主机zabbix文件
[root@backup etc]
/usr/local/etc
[root@backup etc]
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 ~]
[root@backup ~]
仪表盘查看
测试
让主备产生脑裂
[root@backup ~]
! 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 ~]
查看vip
可以看到主备都产生的vip 主
[root@master ~]
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 ~]
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
查看仪表盘 发出了告警
|