keepalived简介
Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。
Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。
所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。
Keepalived官网:Keepalived for Linux
Keepalived的重要功能
keepalived 有三个重要的功能,分别是:
- 管理LVS负载均衡软件
- 实现LVS集群节点的健康检查
- 作为系统网络服务的高可用性
keepalived好可用故障转移原理
Keepalived 高可用服务之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的。
在 Keepalived 服务正常工作时,主 Master 节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备 Backup 节点自己还活看,当主 Master 节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master 节点的心跳了,于是调用自身的接管程序,接管主 Master 节点的 IP 资源及服务。而当主 Master 节点恢复时,备 Backup 节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
原理图
Keepalived高可用对之间是通过VRRP通信的,因此,我们从 VRRP开始了解起:
(1) VRRP,全称 Virtual Router Redundancy Protocol,中文名为虚拟路由冗余协议,VRRP的出现是为了解决静态路由的单点故障。
(2)VRRP是通过一种竟选协议机制来将路由任务交给某台 VRRP路由器的。
(3)VRRP用 IP多播的方式(默认多播地址(224.0.0.18))实现高可用对之间通信。
(4)工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选,但一般 Keepalived系统运维工作中都是一对。
(5)VRRP使用了加密协议加密数据,但Keepalived官方目前还是推荐用明文的方式配置认证类型和密码。
再介绍一下 Keepalived服务的工作原理:
Keepalived高可用是通过 VRRP 进行通信的, VRRP是通过竞选机制来确定主备的,主的优先级高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。
在 Keepalived 服务之间,只有作为主的服务器会一直发送 VRRP 组播包,告诉备它还活着,此时备不会枪占主,当主不可用时,即备监听不到主发送的组播包时,就会启动相关服务接管资源,保证业务的连续性.接管速度最快可以小于1秒。
keepalived部署
四台主机
192.168.100.10 master
192.168.100.10 slave
192.168.100.30 rs1
192.168.100.50 rs2
192.168.100.60 测试访问
rs1和rs2添加好网页[root@rs2 ~]# yum -y install nginx
[root@rs2 ~]# echo rs1 > /usr/share/nginx/html/index.html[root@rs1 ~]# systemctl restart nginx;systemctl enable nginx
[root@rs1 ~]# rs2
[root@rs2 ~]# yum -y install nginx
[root@rs2 ~]# echo rs2 > /usr/share/nginx/html/index.html[root@rs2 ~]# systemctl restart nginx;systemctl enable nginx
[root@rs2 ~]#
测试访问
master,slave主机做负载均衡
master
vim /etc/nginx/nginx.confupstream cc {server 192.168.100.30:80;server 192.168.100.50:80;}server {listen 80;server_name localhost;location / {proxy_pass http://cc;}
slave
vim /etc/nginx/nginx.confupstream cc {server 192.168.100.30:80;server 192.168.100.50:80;}server {listen 80;server_name localhost;location / {proxy_pass http://cc;}
测试访问
master,slave主机都安装keepalived和相关软件包
yum -y install gcc gcc-c++
yum -y install keepalived
查看keepalived配置文件
vim /etc/keepalived/keepalived.conf global_defs { //全局配置notification_email { //定义报警收件人邮件地址acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.loc //定义报警发件人邮箱smtp_server 192.168.200.1 //邮箱服务器地址smtp_connect_timeout 30 //定义邮箱超时时间router_id LVS_DEVEL //定义路由标识信息,同局域网内唯一vrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_instance VI_1 { //定义实例state MASTER //指定keepalived节点的初始状态,可选值为MASTER/BACKUPinterface eth0 //VRRP实例绑定的网卡接口,用户发送VRRP包virtual_router_id 51 //虚拟路由的ID,同一集群要一致priority 100 //定义优先级,按优先级来决定主备角色,优先级越大越优先advert_int 1 //主备通讯时间间隔authentication { //配置认证auth_type PASS //认证方式,此处为密码auth_pass 1111 //同一集群中的keepalived配置里此处必须一致,推荐使用8为随机数}virtual_ipaddress { //配置要使用的VIP192.168.200.16192.168.200.17192.168.200.18}
}virtual_server 192.168.200.100 443 { //配置虚拟服务器delay_loop 6 //健康检查的时间间隔lb_algo rr //lvs调度算法lb_kind NAT //lvs模式persistence_timeout 50 //持久化超时时间,单位为秒protocol TCP //4层协议sorry_server 192.168.200.200 1358 //定义备用服务器,当所有RS都故障时,用sorry_server来响应客户端real_server 192.168.201.100 443 { //定义真实处理请求的服务器weight 1 //给服务器指定权重,默认为1SSL_GET {url {path / //指定要检查的URL路径digest ff20ad2481f97b1754ef3e12ecd3a9cc //摘要信息}url {path /mrtg/digest 9b3a0c85a887a256d6939da88aabd8cd}connect_timeout 3 //连接超时时间retry 3 //get尝试次数delay_before_retry 3 //在尝试之前延迟多长时间}}
}virtual_server 10.10.10.2 1358 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCPsorry_server 192.168.200.200 1358real_server 192.168.200.2 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl3/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}connect_timeout 3retry 3delay_before_retry 3}}real_server 192.168.200.3 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334c}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334c}connect_timeout 3retry 3delay_before_retry 3}}
}virtual_server 10.10.10.3 1358 {delay_loop 3lb_algo rrlb_kind NATpersistence_timeout 50protocol TCPreal_server 192.168.200.4 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl3/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}connect_timeout 3retry 3delay_before_retry 3}}real_server 192.168.200.5 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl3/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}connect_timeout 3retry 3delay_before_retry 3}}
}
修改主keepalived配置文件
vim /etc/keepalived/keepalived.confglobal_defs {router_id cz01
}vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.150}
}virtual_server 192.168.100.150 80 {delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 50protocol TCPreal_server 192.168.100.10 80 {weight 1TCP_CHECK {connect_port 80 connect_timeout 3 nb_get_retry 3delay_before_retry 3}}real_server 192.168.100.20 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
配置从keepalived配置文件
vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {router_id cz02
}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.150}
}virtual_server 192.168.100.150 80 {delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 50protocol TCPreal_server 192.168.100.10 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.100.20 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
修改内核参数,开启侦听VIP功能
此步可做可不做,该功能可用于仅监听VIP的时候
Master和slave:vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1sysctl -p
重启服务并设置下次启动生效
[root@master ~]# systemctl restart keepalived.service
[root@master ~]# systemctl enable keepalived.service
此时可以查看ip地址
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:b8:d7:51 brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.100.10/24 brd 192.168.100.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.100.150/32 scope global ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:feb8:d751/64 scope link noprefixroute valid_lft forever preferred_lft forever此时就不需要去配置虚拟地址了,192.168.100.150就是虚拟地址了
slave启动服务
[root@slave ~]# systemctl restart keepalived.service
[root@slave ~]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.[root@slave ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:82:d1:8e brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.100.20/24 brd 192.168.100.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe82:d18e/64 scope link noprefixroute valid_lft forever preferred_lft forever
[root@slave ~]#
测试,使用虚拟地址来访问
让keepalived监控nginx负载均衡
//在master上编写脚本
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check.sh
#!/bin/bash
nginx_status=`ps -ef | grep -v "grep" | grep "nginx" | wc -l`
if [ $nginx_status -lt 1 ];thensystemctl stop keepalived
fi
[root@master scripts]# chmod +x check.sh///
[root@master scripts]# vim 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 | s-nail -s "$subject" cczz1999@163.com
}
case "$1" inmaster)nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)if [ $nginx_status -lt 1 ];thensystemctl start nginxfisendmail;;backup)nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)if [ $nginx_status -gt 0 ];thensystemctl stop nginxfi;;*)echo "Usage:$0 master|backup VIP";;
esac[root@master scripts]# chmod +x notify.sh
在slave上编写脚本
[root@slave keepalived]# mkdir /scripts
[root@slave keepalived]# cd /scripts/
[root@slave scripts]# scp root@192.168.100.10:/scripts/check.sh .
root@192.168.100.100's password:
check.sh 100% 137 296.3KB/s 00:00
[root@slave scripts]# scp root@192.168.100.10:/scripts/notify.sh .
root@192.168.100.100's password:
notify.sh 100% 594 1.0MB/s 00:00
[root@slave scripts]# chmod +x check.sh
[root@slave scripts]# chmod +x notify.sh
配置master的keepalived
! Configuration File for keepalivedglobal_defs {router_id cz01
}vrrp_script nginx_check {script "/scripts/check.sh"interval 10weight -20
}vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.150}track_script {nginx_check}notify_master "/scripts/notify.sh master 192.168.100.150"notify_backup "/scripts/notify.sh backup 192.168.100.150"
}virtual_server 192.168.100.150 80 {delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 50protocol TCPreal_server 192.168.100.10 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.100.20 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
配置slave的keepalived
! Configuration File for keepalivedglobal_defs {router_id cz02
}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.150}notify_master "/scripts/notify.sh master 192.168.100.150"notify_backup "/scripts/notify.sh backup 192.168.100.150"
}virtual_server 192.168.100.150 80 {delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 50protocol TCPreal_server 192.168.100.10 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.100.20 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
测试
查看负载均衡是否可用
模拟故障
启用keepalived,开启nginx服务
[root@master scripts]# systemctl restart keepalived.service
[root@master scripts]# systemctl enable keepalived
[root@master scripts]# systemctl restart nginx
[root@master scripts]# systemctl enable nginx关闭master的nginx服务
[root@master scripts]# systemctl stop nginx此时虚拟ip就不在master节点上了
[root@master scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:b8:d7:51 brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.100.10/24 brd 192.168.100.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:feb8:d751/64 scope link noprefixroute valid_lft forever preferred_lft forever查看slave节点
[root@slave scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:82:d1:8e brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.100.20/24 brd 192.168.100.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.100.150/32 scope global ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe82:d18e/64 scope link noprefixroute valid_lft forever preferred_lft forever此时虚拟IP在slave节点运行
测试访问