目录

  • 一、keepalived是什么
  • 二、Keepalived服务的重要功能
    • 1、实现对LVS集群节点健康检查功能(healthcheck)
    • 2、作为系统网络服务的高可用功能
    • 3、Keepalived高可用故障切换转移原理
  • 三、keepalived工作原理描述
  • 四、keepalived实现nginx负载均衡机高可用
    • 1.keepalived安装
    • 2.在主备上分别安装nginx
    • 3.配置keepalived
    • 4.查看VIP在哪台服务器上
    • 5.让keepalived监控nginx负载均衡机
    • 6.配置keepalived加入监控脚本的配置

一、keepalived是什么

Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。

Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。

所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。

keepalived官网

二、Keepalived服务的重要功能1、实现对LVS集群节点健康检查功能(healthcheck)

Keepalived可以通过在自身的keepalived.conf文件里配置LVS的节点IP和相关参数实现对LVS的直接管理;除此之外,当LVS集群中的某一个甚至是几个节点服务器同时发生故障无法提供服务时,Keepalived服务会自动将失效的节点服务器从LVS的正常转发队列中清除出去,并将请求调度到别的正常节点服务器上,从而保证最终用户的访问不受影响;当故障的节点服务器被修复以后,Keepalived服务又会自动地把它们加入到正常转发队列中,对客户提供服务。

2、作为系统网络服务的高可用功能

Keepalived高可用功能实现的简单原理为,两台主机同时安装好Keepalived软件并启动服务,开始正常工作时,由角色为Master的主机获得所有资源并对用户提供服务,角色为Backup的主机作为Master主机的热备;当角色为Master的主机失效或出现故障时,角色为Backup的主机将自动接管Master主机的所有工作,包括接管VIP资源及相应资源服务;而当角色为Master的主机故障修复后,又会自动接管回它原来处理的工作,角色为Backup的主机则同时释放Master主机失效时它接管的工作,此时,两台主机将恢复到最初启动时各自的原始角色及工作状态。

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实现nginx负载均衡机高可用

环境说明

系统信息主机名IP
centos8master192.168.111.141
centos8backup192.168.111.142

本次高可用虚拟IP(VIP)地址暂定为 192.168.111.250

1.keepalived安装

配置主服务器keepalived

//修改主机名便于区分[root@localhost ~]# hostnamectl set-hostname master[root@localhost ~]# bash//关闭防火墙和selinux[root@master ~]# systemctl disable --now firewalld[root@master ~]# setenforce 0[root@master ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config//配置yum源[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo[root@master ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo//下载依赖包[root@master ~]# yum -y install epel-release vim wget gcc gcc-c++//下载keepalived服务[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/systemd/system/keepalived.service      //此为服务控制文件/usr/libexec/keepalived/usr/sbin/keepalived.....此处省略N行

配置备服务器keepalived

//修改主机名便于区分[root@localhost ~]# hostnamectl set-hostname backup[root@localhost ~]# bash//关闭防火墙和selinux[root@backup ~]# systemctl disable --now firewalld[root@backup ~]# setenforce 0[root@backup ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config//配置yum源[root@backup ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo[root@backup ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo//下载依赖包[root@backup ~]# yum -y install epel-release vim wget gcc gcc-c++//下载keepalived服务[root@backup ~]# yum -y install keepalived

2.在主备上分别安装nginx

在master上安装nginx

[root@master ~]# yum -y install nginx[root@master ~]# cd /usr/share/nginx/html/[root@master html]# ls404.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]# ls404.html  50x.html  index.html  index.html.bak  nginx-logo.png  poweredby.png[root@master html]# systemctl start nginx[root@master html]# systemctl enable nginx[root@master html]# ss -anltState           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                            0.0.0.0:80                           0.0.0.0:*                              LISTEN          0               128                               [::]:22                              [::]:*                              LISTEN          0               128                               [::]:80                              [::]:*                              [root@master html]# curl 192.168.111.141master

在slave上安装nginx

[root@backup ~]# yum -y install nginx[root@backup ~]# cd /usr/share/nginx/html/[root@backup html]# ls404.html  50x.html  index.html  nginx-logo.png  poweredby.png[root@backup html]# mv index.html{,.bak}[root@backup html]# echo 'backup' > index.html[root@backup html]# ls404.html  50x.html  index.html  index.html.bak  nginx-logo.png  poweredby.png[root@backup html]# systemctl start nginx[root@backup html]# ss -anltState           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 html]# curl 192.168.111.142backup

3.配置keepalived

配置主服务器keepalived

//将原配置文件备份[root@master ~]# mv /etc/keepalived/keepalived.conf{,.bak}[root@master ~]# ls /etc/keepalived/keepalived.conf.bak//重新创建一个配置文件[root@master ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {   router_id lb01}vrrp_instance VI_1 {    state MASTER    interface ens33    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 123456    }    virtual_ipaddress {        192.168.111.250    }}virtual_server 192.168.111.250 80 {    delay_loop 6    lb_algo rr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 192.168.111.141 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }    real_server 192.168.111.142 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }}//设置开机自启动[root@master ~]# systemctl enable --now keepalived

配置备服务器keepalived

[root@backup ~]# mv /etc/keepalived/keepalived.conf{,.bak}[root@backup ~]# ls /etc/keepalived/keepalived.conf.bak[root@backup ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {   router_id lb01}vrrp_instance VI_1 {    state MASTER    interface ens33    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 123456    }    virtual_ipaddress {        192.168.111.250    }}virtual_server 192.168.111.250 80 {    delay_loop 6    lb_algo rr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 192.168.111.141 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }    real_server 192.168.111.142 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }}//设置开机自启动[root@backup ~]# systemctl enable --now keepalived

4.查看VIP在哪台服务器上

在master上查看

[root@master ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 925sec preferred_lft 925sec    inet 192.168.111.250/32 scope global ens33//可以看到虚拟IP(VIP)       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute        valid_lft forever preferred_lft forever

在backup上查看

[root@backup ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:07:42:65 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.142/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 1651sec preferred_lft 1651sec    inet6 fe80::20c:29ff:fe07:4265/64 scope link noprefixroute        valid_lft forever preferred_lft forever

5.让keepalived监控nginx负载均衡机

keepalived通过脚本来监控nginx负载均衡机的状态

在master上编写脚本

[root@master ~]# mkdir /scripts[root@master ~]# cd /scripts/[root@master scripts]# vim check_nginx.sh#!/bin/bashnginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)if [ $nginx_status -lt 1 ];then    systemctl stop keepalivedfi[root@master scripts]# vim notify.sh #!/bin/bashVIP=$2case "$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]# lltotal 8-rwxr-xr-x 1 root root 143 Oct  8 23:18 check_nginx.sh-rwxr-xr-x 1 root root 451 Oct  8 23:37 notify.sh

在backup上编写脚本

[root@backup ~]# mkdir /scripts[root@backup ~]# cd /scripts/[root@backup scripts]# scp root@192.168.111.141:/scripts/* .The authenticity of host '192.168.111.141 (192.168.111.141)' can't be established.ECDSA key fingerprint is SHA256:+KjoLZnhr7A3Jz2DNbF6JHSkb/6pBZPVizet4RohrS0.Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added '192.168.111.141' (ECDSA) to the list of known hosts.root@192.168.111.141's password: check_nginx.sh                                                                                           100%  143    99.3KB/s   00:00    notify.sh                                                                                                100%  451   403.0KB/s   00:00   [root@backup scripts]# lltotal 8-rwxr-xr-x 1 root root 143 Oct  8 23:38 check_nginx.sh-rwxr-xr-x 1 root root 451 Oct  8 23:38 notify.sh

6.配置keepalived加入监控脚本的配置

配置主服务器keeplived

[root@master ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_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 123456    }    virtual_ipaddress {        192.168.111.250    }    track_script {//添加这一部分        nginx_check    }    notify_master "/scripts/notify.sh master 192.168.111.250"    notify_backup "/scripts/notify.sh bakcup 192.168.111.250"}virtual_server 192.168.111.250 80 {    delay_loop 6    lb_algo rr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 192.168.111.141 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }    real_server 192.168.111.142 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }}[root@master ~]# systemctl restart keepalived

配置备服务器keepalived

[root@backup ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {   router_id lb01}vrrp_instance VI_1 {    state MASTER    interface ens33    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 123456    }    virtual_ipaddress {        192.168.111.250    }    notify_master "/scripts/notify.sh master 192.168.111.250"//添加这两句    notify_backup "/scripts/notify.sh backup 192.168.111.250"}virtual_server 192.168.111.250 80 {    delay_loop 6    lb_algo rr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 192.168.111.141 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }    real_server 192.168.111.142 80 {        weight 1        TCP_CHECK {            connect_port 80            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }}[root@backup ~]# systemctl restart keepalived

测试

//在master上关闭nginx服务模拟出错,进行测试[root@master ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 1251sec preferred_lft 1251sec    inet 192.168.111.250/32 scope global ens33       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute        valid_lft forever preferred_lft forever[root@master ~]# systemctl stop nginx[root@master ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 1238sec preferred_lft 1238sec    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute        valid_lft forever preferred_lft forever[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 Sun 2022-10-09 00:25:47 CST; 52s ago  Process: 277285 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 277288 (code=exited, status=0/SUCCESS)//在backup上查看[root@backup ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:07:42:65 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.142/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 1080sec preferred_lft 1080sec    inet 192.168.111.250/32 scope global ens33       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe07:4265/64 scope link noprefixroute        valid_lft forever preferred_lft forever

浏览器进行访问

//启动master上的nginx服务和keepalived服务,看VIP是否会回到主服务器上[root@master ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 1053sec preferred_lft 1053sec    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute        valid_lft forever preferred_lft forever[root@master ~]# systemctl start nginx[root@master ~]# systemctl restart keepalived[root@master ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 971sec preferred_lft 971sec    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute        valid_lft forever preferred_lft forever//需要重启备服务器上的keepalived服务[root@backup ~]# systemctl restart keepalived//VIP才能回到主服务上[root@master ~]# ip a1: lo:  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 forever2: ens33:  mtu 1500 qdisc fq_codel state UP group default qlen 1000    link/ether 00:0c:29:50:34:72 brd ff:ff:ff:ff:ff:ff    inet 192.168.111.141/24 brd 192.168.111.255 scope global dynamic noprefixroute ens33       valid_lft 926sec preferred_lft 926sec    inet 192.168.111.250/32 scope global ens33       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe50:3472/64 scope link noprefixroute        valid_lft forever preferred_lft forever//VIP在主服务器上时,想要访问需要关闭backup上的nginx服务[root@backup ~]# systemctl stop nginx