Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。(LVS,不能做健康检查,以前用来管理lvs,现在用来做高可用)
Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。
Keepalived软件主要是通过VRRP协议实现高可用功能的。
VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写
VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。(当一个节点挂掉,走另外一个节点)
Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。(系统网络服务的高可用:所有关键性业务,nginx,lvs和haproxy的调度器)
系统信息 | 主机名 | IP |
---|---|---|
red 8.5 | master | 192.168.232.134 |
red 8.5 | backup | 192.168.232.128 |
[root@master ~]# dnf module -y install nginx:1.20
[root@master ~]# cd /usr/share/testpage/
[root@master testpage]# ls
index.html
[root@master testpage]# echo '123456 master pkg' > index.html
[root@master testpage]# cat index.html
123456 master pkg
[root@master testpage]# cd
[root@master ~]#
启动nginx
[root@master ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service 鈫� /usr/lib/systemd/system/nginx.service.
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]#
关闭防火墙和selinux
[root@master ~]# vim /etc/selinux/config
[root@master ~]# setenforce 0
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]#
[root@backup ~]# dnf module -y install nginx:1.20
[root@backup ~]# echo '66666 backup' > /usr/share/testpage/index.html
[root@backup ~]# cat /usr/share/testpage/index.html
66666 backup
[root@backup ~]#
启动nginx
[root@backup ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@backup ~]#
关闭防火墙和selinux
[root@backup ~]# systemctl disable --now firewalld
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 ~]# vim /etc/selinux/config
安装keepalived
[root@master ~]# dnf list all|grep keepalived
Failed to set locale, defaulting to C.UTF-8
keepalived.x86_64 2.1.5-6.el8 AppStream
[root@master ~]# yum -y install keepalived
查看安装生成的文件
[root@master ~]# rpm -ql keepalived
/etc/keepalived //配置目录
/etc/keepalived/keepalived.conf //此为主配置文件
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/.build-id
/usr/lib/.build-id/0a
/usr/lib/.build-id/0a/410997e11c666114ca6d785e58ff0cc248744e
/usr/lib/.build-id/6f
/usr/lib/.build-id/6f/ba0d6bad6cb5ff7b074e703849ed93bebf4a0f
/usr/lib/systemd/system/keepalived.service //此为服务控制文件
/usr/libexec/keepalived
/usr/sbin/keepalived
/usr/share/doc/keepalived
/usr/share/doc/keepalived/AUTHOR
/usr/share/doc/keepalived/CONTRIBUTORS
/usr/share/doc/keepalived/COPYING
/usr/share/doc/keepalived/ChangeLog
/usr/share/doc/keepalived/README
/usr/share/doc/keepalived/TODO
/usr/share/doc/keepalived/keepalived.conf.HTTP_GET.port
/usr/share/doc/keepalived/keepalived.conf.IPv6
/usr/share/doc/keepalived/keepalived.conf.PING_CHECK
/usr/share/doc/keepalived/keepalived.conf.SMTP_CHECK
/usr/share/doc/keepalived/keepalived.conf.SSL_GET
/usr/share/doc/keepalived/keepalived.conf.SYNOPSIS
/usr/share/doc/keepalived/keepalived.conf.UDP_CHECK
/usr/share/doc/keepalived/keepalived.conf.conditional_conf
/usr/share/doc/keepalived/keepalived.conf.fwmark
/usr/share/doc/keepalived/keepalived.conf.inhibit
/usr/share/doc/keepalived/keepalived.conf.misc_check
/usr/share/doc/keepalived/keepalived.conf.misc_check_arg
/usr/share/doc/keepalived/keepalived.conf.quorum
/usr/share/doc/keepalived/keepalived.conf.sample
/usr/share/doc/keepalived/keepalived.conf.status_code
/usr/share/doc/keepalived/keepalived.conf.track_interface
/usr/share/doc/keepalived/keepalived.conf.virtual_server_group
/usr/share/doc/keepalived/keepalived.conf.virtualhost
/usr/share/doc/keepalived/keepalived.conf.vrrp
/usr/share/doc/keepalived/keepalived.conf.vrrp.localcheck
/usr/share/doc/keepalived/keepalived.conf.vrrp.lvs_syncd
/usr/share/doc/keepalived/keepalived.conf.vrrp.routes
/usr/share/doc/keepalived/keepalived.conf.vrrp.rules
/usr/share/doc/keepalived/keepalived.conf.vrrp.scripts
/usr/share/doc/keepalived/keepalived.conf.vrrp.static_ipaddress
/usr/share/doc/keepalived/keepalived.conf.vrrp.sync
/usr/share/man/man1/genhash.1.gz
/usr/share/man/man5/keepalived.conf.5.gz
/usr/share/man/man8/keepalived.8.gz
/usr/share/snmp/mibs/KEEPALIVED-MIB.txt
/usr/share/snmp/mibs/VRRP-MIB.txt
/usr/share/snmp/mibs/VRRPv3-MIB.txt
[root@master ~]#
安装keepalived
[root@backup ~]# yum -y install keepalived
[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# mv keepalived.conf{,.bak}
[root@master keepalived]# ls
keepalived.conf.bak
[root@master keepalived]# vim keepalived.conf
[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]#
[root@master ~]# systemctl enable --now keepalived
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]#
[root@backup ~]# cd /etc/keepalived/
[root@backup keepalived]# ls
keepalived.conf
[root@backup keepalived]# mv keepalived.conf{,.bak}
[root@backup keepalived]# ls
keepalived.conf keepalived.conf.bak
[root@backup keepalived]# vim keepalived.conf
[root@backup keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup keepalived]#
[root@backup ~]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@backup ~]#
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master ~]#
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup ~]#
C:\Users\Administrator>curl 192.168.232.250
curl: (28) Failed to connect to 192.168.232.250 port 80 after 21033 ms: Timed out
C:\Users\Administrator>curl 192.168.232.250
123456 master pkg
[root@master ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
[root@master ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@master ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
[root@backup ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
[root@backup ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@backup ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check_nginx.sh
[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]#
[root@master scripts]# chmod +x check_nginx.sh
解释:
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
nginx_status:查看nginx的状态
grep -Ev "grep|$0":grep -v 取反,grep本身没有了,$0脚本本身
grep '\bnginx\b'|wc -l:过滤nginx,显示行数
-lt:小于
[root@master scripts]# vim notify.sh
[root@master scripts]# chmod +x notify.sh
[root@master scripts]# ls
check_nginx.sh notify.sh
[root@master scripts]# cat notify.sh
#!/bin/bash
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
;;
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]#
VIP=$2 给脚本传参数,第二个为虚拟IP
如果$1是master:启动nginx
如果$1是backup:停掉nginx
否则告诉怎么使用
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 ~]# mkdir /scripts
[root@backup ~]# cd /scripts/
[root@backup scripts]# ls
[root@backup scripts]#
[root@master scripts]# scp notify.sh 192.168.232.128:/scripts/
The authenticity of host '192.168.232.128 (192.168.232.128)' can't be established.
ECDSA key fingerprint is SHA256:Blx3FlbkuzuEWE0fW0IcvAT1vM9oBVG/LrPyW+M99Es.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.232.128' (ECDSA) to the list of known hosts.
root@192.168.232.128's password:
notify.sh 100% 435 336.9KB/s 00:00
[root@master scripts]#
[root@backup scripts]# ls
notify.sh
[root@backup scripts]# ls
notify.sh
[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# vi keepalived.conf
[root@master keepalived]# ls
keepalived.conf keepalived.conf.bak
[root@master keepalived]# cat 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 ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.232.250"
notify_backup "/scripts/notify.sh backup 192.168.232.250"
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]#
[root@master ~]# systemctl restart keepalived
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master ~]#
vrrp_script nginx_check {
script "/scripts/check_nginx.sh"//真实脚本名字
interval 1//间隔1秒
weight -20//权重,如果脚本执行失败了,将权重-20,变成备
}
track_script { //跟踪脚本
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.232.250"
notify_backup "/scripts/notify.sh backup 192.168.232.250"
notify_master "/scripts/notify.sh master 192.168.232.250":变成master,给脚本传参数,传一个master,给一个虚拟ip
[root@backup ~]# cd /etc/keepalived/
[root@backup keepalived]# ls
keepalived.conf keepalived.conf.bak
[root@backup keepalived]# vi keepalived.conf
[root@backup keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
notify_master "/scripts/notify.sh master 192.168.232.250"
notify_backup "/scripts/notify.sh backup 192.168.232.250"
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup keepalived]#
[root@backup ~]# systemctl restart keepalived
[root@backup ~]# systemctl restart keepalived
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@backup ~]#
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup ~]#
检查脚本配置上去,主坏了,备抢占,主好了,抢不过来
将master主配置文件检查脚本注释
环境:keepalived在运行,有VIP,nginx在运行
[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# vim keepalived.conf
[root@master keepalived]# cat 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 ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
# track_script {
# nginx_check
# }
notify_master "/scripts/notify.sh master 192.168.232.250"
notify_backup "/scripts/notify.sh backup 192.168.232.250"
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]#
[root@master keepalived]# systemctl start keepalived
[root@master keepalived]# systemctl status keepalived
鈼� keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service;>
Active: active (running) since Wed 2022-08-31 10:47:48 CST;>
Process: 578045 ExecStart=/usr/sbin/keepalived $KEEPALIVED_O>
Main PID: 578046 (keepalived)
Tasks: 3 (limit: 23502)
Memory: 2.2M
CGroup: /system.slice/keepalived.service
鈹溾攢578046 /usr/sbin/keepalived -D
鈹溾攢578047 /usr/sbin/keepalived -D
鈹斺攢578048 /usr/sbin/keepalived -D
Aug 31 10:47:48 master Keepalived_vrrp[578048]: SECURITY VIOLA>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: Assigned addre>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: Assigned addre>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: Warning - scri>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: Registering gr>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: (VI_1) removin>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: (VI_1) Enterin>
Aug 31 10:47:48 master Keepalived_vrrp[578048]: VRRP sockpool:>
Aug 31 10:47:49 master Keepalived_vrrp[578048]: (VI_1) receive>
Aug 31 10:47:50 master Keepalived_vrrp[578048]: (VI_1) receive>
[root@master keepalived]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master keepalived]#
[root@backup ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service;>
Active: active (running) since Wed 2022-08-31 10:22:28 CST;>
Process: 525648 ExecStart=/usr/sbin/keepalived $KEEPALIVED_O>
Main PID: 525652 (keepalived)
Tasks: 3 (limit: 23502)
Memory: 2.2M
CGroup: /system.slice/keepalived.service
├─525652 /usr/sbin/keepalived -D
├─525654 /usr/sbin/keepalived -D
└─525655 /usr/sbin/keepalived -D
[root@backup ~]#
[root@backup ~]# systemctl stop nginx
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@backup ~]#
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup ~]#
[root@master keepalived]# vim keepalived.conf
[root@master keepalived]# cat 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 ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.232.250"
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]#
[root@master ~]# systemctl restart nginx
[root@master ~]# systemctl restart keepalived
[root@master keepalived]# systemctl enable nginx
[root@master keepalived]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@master keepalived]#
[root@master keepalived]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master keepalived]#
[root@master keepalived]# systemctl status keepalived
鈼� keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service;>
Active: active (running) since Wed 2022-08-31 10:47:48 CST;>
Main PID: 578046 (keepalived)
Tasks: 3 (limit: 23502)
Memory: 2.2M
CGroup: /system.slice/keepalived.service
鈹溾攢578046 /usr/sbin/keepalived -D
鈹溾攢578047 /usr/sbin/keepalived -D
鈹斺攢578048 /usr/sbin/keepalived -D
[root@backup ~]# systemctl disable nginx
Removed /etc/systemd/system/multi-user.target.wants/nginx.service.
[root@backup ~]#
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service;>
Active: active (running) since Wed 2022-08-31 10:22:28 CST;>
Process: 525648 ExecStart=/usr/sbin/keepalived $KEEPALIVED_O>
Main PID: 525652 (keepalived)
Tasks: 3 (limit: 23502)
Memory: 2.2M
CGroup: /system.slice/keepalived.service
├─525652 /usr/sbin/keepalived -D
├─525654 /usr/sbin/keepalived -D
└─525655 /usr/sbin/keepalived -D
[root@master ~]# systemctl stop nginx
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master ~]#
[root@master ~]# systemctl status keepalived
鈼� keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2022-08-31 11:42:31 CST; 28s ago
Process: 702395 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 702396 (code=exited, status=0/SUCCESS)
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup ~]#
[root@master ~]# systemctl restart nginx keepalived
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master ~]#
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup ~]#
[root@master ~]# vim /etc/keepalived/keepalived.conf
[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 BACKUP
interface ens160
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.232.250"
notify_backup "/scripts/notify.sh backup 192.168.232.250"
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]#
[root@master ~]# systemctl restart nginx keepalived
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]# systemctl status keepalived
鈼� keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service;>
Active: inactive (dead) since Wed 2022-08-31 11:55:22 CST; >
Process: 733253 ExecStart=/usr/sbin/keepalived $KEEPALIVED_O>
Main PID: 733255 (code=exited, status=0/SUCCESS)
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@backup ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service;>
Active: active (running) since Wed 2022-08-31 11:25:49 CST;>
Process: 656165 ExecStart=/usr/sbin/keepalived $KEEPALIVED_O>
Main PID: 656166 (keepalived)
Tasks: 3 (limit: 23502)
Memory: 2.2M
CGroup: /system.slice/keepalived.service
├─656166 /usr/sbin/keepalived -D
├─656167 /usr/sbin/keepalived -D
└─656168 /usr/sbin/keepalived -D
[root@backup ~]# systemctl stop nginx
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@backup ~]#
[root@master ~]# systemctl start nginx
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]#
master主机好了之后,没有抢占
[root@master ~]# systemctl start keepalived
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]#
[root@master ~]# vim /etc/keepalived/keepalived.conf
# nopreempt
# notify_backup "/scripts/notify.sh backup 192.168.232.250"
[root@backup ~]# systemctl stop nginx
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
[root@backup ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
重启服务
[root@master ~]# systemctl restart nginx keepalived
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master ~]#
/etc/keepalived/keepalived.conf
。[root@master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { //全局配置
notification_email { //定义报警收件人邮件地址
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected] //定义报警发件人邮箱
smtp_server 192.168.232.1 //邮箱服务器地址
smtp_connect_timeout 30 //定义邮箱超时时间
router_id LVS_DEVEL //路由器ID定义路由标识信息,同局域网内唯一
vrrp_skip_check_adv_addr//vrp的跳过检查功能
vrrp_strict//严格功能
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 { //定义实例
state MASTER //指定keepalived节点的初始状态,可选值为MASTER|BACKUP
interface ens33 //VRRP实例绑定的网卡接口,用户发送VRRP包
virtual_router_id 51 //虚拟路由的ID,同一集群要一致
priority 100 //定义优先级,按优先级来决定主备角色,优先级越大越优先
nopreempt //设置不抢占,必须跟state的backup使用
advert_int 1 //主备通讯时间间隔
authentication { //配置认证
auth_type PASS //认证方式,此处为密码
auth_pass 1111 //同一集群中的keepalived配置里的此处必须一致,推荐使用8位随机数
}
virtual_ipaddress { //配置要使用的VIP地址
192.168.232.250
}
}
virtual_server 192.168.232.250 80 { //配置虚拟服务器
delay_loop 6 //健康检查的时间间隔
lb_algo rr //lvs调度算法
lb_kind NAT //lvs模式
persistence_timeout 50 //持久化超时时间,单位是秒
protocol TCP //4层协议,tcp协议
sorry_server 192.168.232.150 80 //定义备用服务器,当所有RS都故障时用sorry_server来响应客户端,公益网站
real_server 192.168.223.134 80 { //定义真实处理请求的服务器,表示两台调度器
weight 1 //给服务器指定权重,默认为1
HTTP_GET {
url {
path /testurl/test.jsp //指定要检查的URL路径
digest 640205b7b0fc66c1ea91c463fac6334d //摘要信息
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3 //连接超时时间
nb_get_retry 3 //get尝试次数
delay_before_retry 3 //在尝试之前延迟多长时间
}
}
real_server 192.168.232.128 80 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]#
nopreempt //设置为不抢占。默认是抢占的,当高优先级的机器恢复后,会抢占低优先 \
级的机器成为MASTER,而不抢占,则允许低优先级的机器继续成为MASTER,即使高优先级 \
的机器已经上线。如果要使用这个功能,则初始化状态必须为BACKUP。
preempt_delay //设置抢占延迟。单位是秒,范围是0---1000,默认是0.发现低优先 \
级的MASTER后多少秒开始抢占。
//作用:添加一个周期性执行的脚本。脚本的退出状态码会被调用它的所有的VRRP Instance记录。
//注意:至少有一个VRRP实例调用它并且优先级不能为0.优先级范围是1-254.
vrrp_script <SCRIPT_NAME> {
...
}
SCRIPT_NAME:自己定义的,不等于你所编写的脚本
//选项说明:
script "/path/to/somewhere" //指定要执行的脚本的路径,真实脚本的名字。
interval <INTEGER> //指定脚本执行的间隔。单位是秒。默认为1s。
timeout <INTEGER> //指定在多少秒后,脚本被认为执行失败。
weight <-254 --- 254> //调整优先级。默认为2.假设weight为-20,主为100,备为90,100-20为80,备变成主节点
rise <INTEGER> //执行成功多少次才认为是成功。INTEGER:数字
fall <INTEGER> //执行失败多少次才认为失败。
user <USERNAME> [GROUPNAME] //运行脚本的用户和组。
init_fail //假设脚本初始状态是失败状态。
//weight说明:
1. 如果脚本执行成功(退出状态码为0),weight大于0,则priority增加。
2. 如果脚本执行失败(退出状态码为非0),weight小于0,则priority减少。
3. 其他情况下,priority不变。
weight <INT> //给服务器指定权重。默认是1
inhibit_on_failure //当服务器健康检查失败时,将其weight设置为0, \
//而不是从Virtual Server中移除
notify_up <STRING> //当服务器健康检查成功时,执行的脚本
notify_down <STRING> //当服务器健康检查失败时,执行的脚本
uthreshold <INT> //到这台服务器的最大连接数
lthreshold <INT> //到这台服务器的最小连接数
connect_ip <IP ADDRESS> //连接的IP地址。默认是real server的ip地址
connect_port <PORT> //连接的端口。默认是real server的端口
bindto <IP ADDRESS> //发起连接的接口的地址。
bind_port <PORT> //发起连接的源端口。
connect_timeout <INT> //连接超时时间。默认是5s。
fwmark <INTEGER> //使用fwmark对所有出去的检查数据包进行标记。
warmup <INT> //指定一个随机延迟,最大为N秒。可防止网络阻塞。如果为0,则关闭该功能。
retry <INIT> //重试次数。默认是1次。
delay_before_retry <INT> //默认是1秒。在重试之前延迟多少秒。
global_defs {
router_id LVS_Server
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 150
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.232.250 dev ens33
}
}
virtual_server 192.168.232.250 80 {
delay_loop 3//循环
lvs_sched rr//lvs轮询
lvs_method DR//算法
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
在高可用(HA)系统中,一主,一备,通过心跳去维持,当联系2个节点的“心跳线”断开时,本来为一整体、动作协调的HA系统,就分裂成为2个独立的个体。由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人”一样,争抢“共享资源”、争起“应用服务”,就会发生严重后果——或者共享资源被瓜分、两边“服务”都起不来了;或者两边“服务”都起来了,但同时读写“共享存储(nfs和rsync)”,导致数据损坏(常见如数据库轮询着的联机日志出错)。
心跳线断开时,主以为备挂了,备以为主挂了,相互争抢资源,两边都以为对方出现故障。两边都认为自己是好的,备认为自己有VIP,是主,将服务启动,访问失败。
对付HA系统“裂脑”的对策,目前达成共识的的大概有以下几条:
Keepalived配置里同一 VRRP实例如果 virtual_router_id两端参数配置不一致也会导致裂脑问题发生。
对脑裂的监控应在备用服务器上进行,通过添加zabbix自定义监控进行。
监控备上有无VIP地址
备机上出现VIP有两种情况:
监控只是监控发生脑裂的可能性,不能保证一定是发生了脑裂,因为正常的主备切换VIP也是会到备上的。
监控脚本如下:
[root@slave ~]# mkdir -p /scripts && cd /scripts
[root@slave scripts]# vim check_keepalived.sh
#!/bin/bash
if [ `ip a show ens160 |grep 192.168.232.250|wc -l` -ne 0 ]
then
echo "keepalived is error!"
else
echo "keepalived is OK !"
fi
主机 | 安装的服务 | ip |
---|---|---|
master | keepalived,nginx | 192.168.232.134 |
backup | keepalived,nginx,zabbix客户端 | 192.168.232.128 |
zabbix | zabbix服务端 | 192.168.232.132 |
在backup主机安装zabbix的客户端,在192.168.232.132主机安装zabbix服务端用于使用web网页管理监控
监控出现异常的两种状态:
[root@backup ~]# cd /scripts/
[root@backup scripts]# pwd
/scripts
[root@backup scripts]# cat check_keepalived.sh
#!/bin/bash
if [ `ip a show ens160 |grep 192.168.232.250|wc -l` -ne 0 ]
then
echo "1"
else
echo "0"
fi
[root@backup scripts]#
[root@backup scripts]# chmod +x check_keepalived.sh
[root@backup scripts]# ./check_keepalived.sh //测试脚本
0
[root@backup scripts]# chown -R zabbix.zabbix /scripts/
[root@backup etc]# vim zabbix_agentd.conf
[root@backup etc]# pwd
/usr/local/etc
[root@backup etc]#
修改这两行:
UserParameter=check_keepalived,/bin/bash /scripts/check_keepalived.sh
UnsafeUserParameters=1
然后重启
[root@backup ~]# pkill zabbix_agentd
[root@backup ~]# zabbix_agentd
[root@zabbix ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@zabbix ~]# zabbix_get -s 192.168.232.128 -k check_keepalived
0
[root@zabbix ~]#
[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# ls
keepalived.conf keepalived.conf.bak
[root@master keepalived]# vim keepalived.conf
[root@master keepalived]# cat 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 BACKUP
interface ens160
virtual_router_id 50
# nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mushuang
}
virtual_ipaddress {
192.168.232.250
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.232.250"
notify_backup "/scripts/notify.sh backup 192.168.232.250"
}
virtual_server 192.168.232.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.232.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.232.128 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]#
[root@master keepalived]# systemctl restart keepalived
[root@master keepalived]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:93:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.232.134/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:9321/64 scope link
valid_lft forever preferred_lft forever
[root@master keepalived]#
[root@backup scripts]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:03:67:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.232.128/24 brd 192.168.232.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.232.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe03:678a/64 scope link
valid_lft forever preferred_lft forever
[root@backup scripts]#
[root@zabbix ~]# zabbix_get -s 192.168.232.128 -k check_keepalived
0
[root@zabbix ~]# zabbix_get -s 192.168.232.128 -k check_keepalived
1
[root@zabbix ~]#