keepalived简介
Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。
Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。
所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。
Keepalived官网
Keepalived的重要功能
keepalived 有三个重要的功能,分别是:
(1)管理LVS负载均衡软件
(2)实现LVS集群节点的健康检查
(3)作为系统网络服务的高可用性
3、keepalived高可用故障转移的原理
Keepalived 高可用服务之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的。
在 Keepalived 服务正常工作时,主 Master 节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备 Backup 节点自己还活看,当主 Master 节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master 节点的心跳了,于是调用自身的接管程序,接管主 Master 节点的 IP 资源及服务。而当主 Master 节点恢复时,备 Backup 节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
keepalived原理
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官方目前还是推荐用明文的方式配置认证类型和密码。
介绍完 VRRP,接下来我再介绍一下 Keepalived服务的工作原理:
Keepalived高可用是通过 VRRP 进行通信的, VRRP是通过竞选机制来确定主备的,主的优先级高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。
在 Keepalived 服务之间,只有作为主的服务器会一直发送 VRRP 广播包,告诉备它还活着,此时备不会枪占主,当主不可用时,即备监听不到主发送的广播包时,就会启动相关服务接管资源,保证业务的连续性.接管速度最快可以小于1秒。
keepalived部署
主机名 | IP | 系统 |
---|---|---|
Master | IP:192.168.40.99 --VIP: 192.168.40.111 | rhel8/centos8 |
Slave | IP:192.168.40.100 | rhel8/centos8 |
Master主节点和slave备份节点
关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
cat /etc/sysconfig/selinux
SELINUX=disabled
配置yum源--阿里源centos8和epel源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
安装一下常用命令
dnf -y install gcc gcc-c++
安装keepalived
dnf -y install keepalived
在master节点安装nginx
[root@Master ~]# dnf -y install nginx
[root@Master ~]# systemctl restart nginx.service
[root@Master ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
在nginx中添加测试网页
[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]# echo 'hi Master' > index.html
在slave节点安装nginx
[root@Slave ~]# systemctl restart nginx.service
[root@Slave ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@Slave ~]# dnf -y install nginx
在nginx中添加测试网页
[root@Slave ~]# cd /usr/share/nginx/html/
[root@Slave html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@Slave html]# echo "hi Slave" > index.html
配置主keepalived
例子:
at keepalived.conf //查看配置文件
! Configuration File for keepalived
global_defs { //全局配置
notification_email { //定义报警收件人邮件地址
acassen@firewall.loc
failover@firewall.loc
sysadmin@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_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 { //定义实例
state MASTER //指定keepalived节点的初始状态,可选值为MASTER/BACKUP
interface eth0 //VRRP实例绑定的网卡接口,用户发送VRRP包
virtual_router_id 51 //虚拟路由的ID,同一集群要一致
priority 100 //定义优先级,按优先级来决定主备角色,优先级越大越优先
advert_int 1 //主备通讯时间间隔
authentication { //配置认证
auth_type PASS //认证方式,此处为密码
auth_pass 1111 //同一集群中的keepalived配置里此处必须一致,推荐使用8为随机数
}
virtual_ipaddress { //配置要使用的VIP
192.168.200.16
192.168.200.17
192.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 //给服务器指定权重,默认为1
SSL_GET {
url {
path / //指定要检查的URL路径
digest ff20ad2481f97b1754ef3e12ecd3a9cc //摘要信息
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3 //连接超时时间
retry 3 //get尝试次数
delay_before_retry 3 //在尝试之前延迟多长时间
}
}
}
real_server 192.168.100.100 80 {
weight 1
TCP_CHECK {
connect_port 80 //连接端口
connect_timeout 3 //连接超时时间,默认为5秒
nb_get_retry 3
delay_before_retry 3 //在重试之前延迟多少秒
[root@Master ~]# cd /etc/keepalived/
[root@Master keepalived]# cp keepalived.conf keepalived.conf.bak
[root@Master keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id haha
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.111
}
}
virtual_server 192.168.40.111 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.40.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.40.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
重启服务并设置下次启动生效
[root@Master keepalived]# systemctl restart keepalived.service
[root@Master keepalived]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
在slave节点配置备份keepalived
[root@Slave ~]# cd /etc/keepalived/
[root@Slave keepalived]# cp keepalived.conf keepalived.conf.bak
[root@Slave keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id hehe
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.111
}
}
virtual_server 192.168.40.111 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.40.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.40.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
重启服务并设置下次启动生效
[root@Slave keepalived]# systemctl restart keepalived.service
[root@Slave keepalived]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
在master中配置VIP
[root@Master ~]# ip addr add 192.168.40.111/32 dev ens33
root@Master ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:a3:51:0a brd ff:ff:ff:ff:ff:ff
inet 192.168.40.99/24 brd 192.168.40.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.40.111/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fea3:510a/64 scope link noprefixroute
valid_lft forever preferred_lft forever
修改内核参数,开启侦听VIP功能
此步可做可不做,该功能可用于仅监听VIP的时候
Master和slave
vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
sysctl -p
net.ipv4.ip_nonlocal_bind = 1
让keepalived监控nginx负载均衡
在master上编写脚本
[root@Master ~]# mkdir /scripts
[root@Master ~]# cd /scripts/
[root@Master scripts]# vim check.sh
[root@Master scripts]# cat check.sh
#!/bin/bash
nginx_status=`ps -ef | grep -v "grep" | grep "nginx" | wc -l`
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[root@Master scripts]# chmod +x check.sh
[root@Master scripts]# vim notify.sh
[root@Master 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" haha@example.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@Master scripts]# chmod +x notify.sh
在slave上编写脚本
[root@Slave ~]# mkdir /scripts
[root@Slave ~]# cd /scripts/
[root@Slave scripts]# scp -r root@192.168.40.99:/scripts/* .
root@192.168.40.99's password:
check.sh 100% 144 198.3KB/s 00:00
notify.sh 100% 595 914.7KB/s 00:00
[root@Slave scripts]# ll
总用量 8
-rwxr-xr-x. 1 root root 144 10月 8 01:18 check.sh
-rwxr-xr-x. 1 root root 595 10月 8 01:18 notify.sh
配置master的keepalived
[root@Master keepalived]# cat keepalived.conf
[root@Master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id haha
}
vrrp_script nginx_check {
script "/scripts/check.sh"
interval 10
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 1111
}
virtual_ipaddress {
192.168.40.111
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.40.111"
notify_backup "/scripts/notify.sh backup 192.168.40.111"
}
virtual_server 192.168.40.111 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.40.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.40.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@Master keepalived]# systemctl restart keepalived.service
配置slave的keepalived
[root@Slave keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id hehe
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.111
}
notify_master "/scripts/notify.sh master 192.168.40.111"
notify_backup "/scripts/notify.sh backup 192.168.40.111"
}
virtual_server 192.168.40.111 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.40.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.40.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@Slave keepalived]# systemctl restart keepalived.service
模拟故障
启用keepalived,开启nginx服务
[root@Master ~]# systemctl restart keepalived.service
[root@Master ~]# systemctl enable keepalived.service
[root@Master ~]# systemctl restart nginx.service
[root@Master ~]# systemctl enable nginx.service
在slave中查看端口80是否开启
[root@Slave keepalived]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=912,fd=4),("systemd",pid=1,fd=40))
LISTEN 0 32 192.168.122.1:53 0.0.0.0:* users:(("dnsmasq",pid=1816,fd=6))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1000,fd=4))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=1163,fd=10))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=912,fd=6),("systemd",pid=1,fd=42))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1000,fd=6))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=1163,fd=9))
关闭master的nginx服务
[root@Master scripts]# systemctl stop nginx
在slave中查看端口80是否开启
[root@Slave keepalived]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=912,fd=4),("systemd",pid=1,fd=40))
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=2892,fd=8),("nginx",pid=2891,fd=8),("nginx",pid=2890,fd=8))
LISTEN 0 32 192.168.122.1:53 0.0.0.0:* users:(("dnsmasq",pid=1816,fd=6))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1000,fd=4))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=1163,fd=10))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=912,fd=6),("systemd",pid=1,fd=42))
LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=2892,fd=9),("nginx",pid=2891,fd=9),("nginx",pid=2890,fd=9))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1000,fd=6))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=1163,fd=9))