前言:Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,
Keepalived将检测到,并将有故障的服务器从系统剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工操作,人为工作只需要修复故障服务器。
一、 Keepalived案例解析
1.1、企业应用中,单台 服务器承担应用存在单点故障的危险
1.2、单点故障一旦发生,企业服务将发生中断,造成极大的危害
二、keepalived 软件工具
-
LVS可以实现负载均衡,但是不能够进行健康检查,比如一个rs出现故障,LVS 仍然会把请求转发给故障的rs服务器,这样就会导致请求的无效性。
-
keepalived 软件是专为LVS和HA设计的一款健康检查工具。
- 可以进行节点健康状态检查。
- 实现 LVS 的高可用性,解决 LVS 单点故障的问题。
- 支持故障自动切换。
在LVS基础之上实现心跳检测、监控服务器实现故障转移,如果服务器发生宕机的时候,可以尝试自动重试脚本。如果多次重试还是失败状态,会发送邮件给运维人员。实现高可用。
三、Keepalived实现原理剖析
3.1、Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能
3.2、VRRP(虚拟路由冗余协议)是针对路由器的一种备份解决方案
①由多台路由器组成一个热备组,通过共用的虚拟IP地址对外提供服务
②每个热备组内同时只有一台主路由器提供服务,其他路由器处于冗余状态
③若当前在线的路由器失效,则其他路由器会根据设置的优先级自动接替虚拟IP地址,继续提供服务
四、Keepalived案例讲解
4.1、双机热备的故障切换是由虚拟IP地址来实现,适用于各种应用服务器
4.2、实现基于Web服务的双机热备
①漂移地址:192.168.100.100
②主、备服务器:192.168.100.10、192.168.100.50
③提供的应用服务:Web
4.3、环境(基于LVS-DR进行搭建)
虚拟IP |
192.168.100.100/24 |
主调度器 |
192.168.100.10/24 |
备调度器 |
192.168.100.40/24 |
Web1服务器 |
192.168.100.20/24 |
Web2服务器 |
192.168.100.30/24 |
NFS共享服务器 |
192.168.100.50/24 |
客户机一台 |
用于测试验证 |
4.4、配置主调度器 (192.168.100.10)
加载ip_vs模块 [root@lvs-zhu ~]# modprobe ip_vs [root@lvs-zhu ~]# cat /proc/net/ip_vs IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn 安装管理软件ipvsadm [root@lvs-zhu ~]# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm 安装编译工具 [root@lvs-zhu ~]# yum -y install gcc gcc-c++ make popt-devel openssl-devel kernel-devel 解压缩,编译安装 [root@lvs-zhu ~]# tar zxf keepalived-2.0.13.tar.gz [root@lvs-zhu ~]# cd keepalived-2.0.13/ [root@lvs-zhu keepalived-2.0.13]# ./configure --prefix=/ [root@lvs-zhu keepalived-2.0.13]# make && make install 加入系统管理服务,设置开机自启 [root@lvs-zhu keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d/ [root@lvs-zhu keepalived-2.0.13]# systemctl enable keepalived.service 编辑配置文件 [root@lvs-zhu keepalived-2.0.13]# vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_01 #本服务器的名称 } vrrp_instance VI_1 { #定义VRRP热备实例 state MASTER #热备状态,MASTER表示主服务器,BACKUP表示从服务器 interface ens33 #承载VIP地址的物理接口 virtual_router_id 51 #虚拟路由器的ID号,每个热备组保持一致 priority 110 #优先级,数值越大优先级越高 advert_int 1 #通告间隔秒数(心跳频率) authentication { #热备认证信息,每个热备组保持一致 auth_type PASS #认证类型 auth_pass 6666 #密码字符串 } virtual_ipaddress { #指定飘逸地址(VIP),可以有多个 192.168.100.100 } } virtual_server 192.168.100.100 80 { #虚拟服务器地址(VIP)、端口 delay_loop 6 #健康检查的间隔时间(秒) lb_algo rr #轮询(rr)调度算法 lb_kind DR #直接路由(DR)群集工作模式 persistence_timeout 6 #连接保持时间(秒) protocol TCP #应用服务器采用的是TCP协议 real_server 192.168.100.20 80 { #第一个web服务器节点的地址、端口 weight 1 #节点的权重 TCP_CHECK { #健康检查方式 connect_port 80 #检查的目标端口 connect_timeout 3 #连接超时(秒) nb_get_retry 3 #重试次数 delay_before_retry 3 #重试间隔 } } real_server 192.168.100.30 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } 开启服务 [root@lvs-zhu keepalived-2.0.13]# systemctl start keepalived.service [root@lvs-zhu keepalived-2.0.13]# tail -f /var/log/messages [root@lvs-zhu keepalived-2.0.13]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.100.100:80 rr persistent 6 -> 192.168.100.20:80 Route 1 0 0 -> 192.168.100.30:80 Route 1 0 0 [root@lvs-zhu keepalived-2.0.13]# ip addr show dev ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9a:cd:27 brd ff:ff:ff:ff:ff:ff inet 192.168.100.10/24 brd 192.168.100.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.100.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::26b5:ebd3:a0d2:db12/64 scope link valid_lft forever preferred_lft forever
4.5、配置从调度器(192.168.100.40)
加载ip_vs模块 [root@lvs-bei ~]# modprobe ip_vs [root@lvs-bei ~]# cat /proc/net/ip_vs IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn 安装管理软件ipvsadm [root@lvs-bei ~]# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm 安装编译工具 [root@lvs-bei ~]# yum -y install gcc gcc-c++ make popt-devel openssl-devel kernel-devel 解压缩,编译安装 [root@lvs-bei ~]# tar zxf keepalived-2.0.13.tar.gz [root@lvs-bei ~]# cd keepalived-2.0.13/ [root@lvs-bei keepalived-2.0.13]# ./configure --prefix=/ [root@lvs-bei keepalived-2.0.13]# make && make install 加入系统管理服务,设置开机自启 [root@lvs-bei keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d/ [root@lvs-bei keepalived-2.0.13]# systemctl enable keepalived.service 编辑配置文件 [root@lvs-bei keepalived-2.0.13]# vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_02 #本服务器的名称 } vrrp_instance VI_1 { #定义VRRP热备实例 state BACKUP #热备状态,MASTER表示主服务器,BACKUP表示从服务器 interface ens33 #承载VIP地址的物理接口 virtual_router_id 51 #虚拟路由器的ID号,每个热备组保持一致 priority 105 #优先级,数值越大优先级越高 advert_int 1 #通告间隔秒数(心跳频率) authentication { #热备认证信息,每个热备组保持一致 auth_type PASS #认证类型 auth_pass 6666 #密码字符串 } virtual_ipaddress { #指定飘逸地址(VIP),可以有多个 192.168.100.100 } } virtual_server 192.168.100.100 80 { #虚拟服务器地址(VIP)、端口 delay_loop 6 #健康检查的间隔时间(秒) lb_algo rr #轮询(rr)调度算法 lb_kind DR #直接路由(DR)群集工作模式 persistence_timeout 6 #连接保持时间(秒) protocol TCP #应用服务器采用的是TCP协议 real_server 192.168.100.20 80 { #第一个web服务器节点的地址、端口 weight 1 #节点的权重 TCP_CHECK { #健康检查方式 connect_port 80 #检查的目标端口 connect_timeout 3 #连接超时(秒) nb_get_retry 3 #重试次数 delay_before_retry 3 #重试间隔 } } real_server 192.168.100.30 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } 开启服务 [root@lvs-bei keepalived-2.0.13]# systemctl start keepalived.service [root@lvs-bei keepalived-2.0.13]# tail -f /var/log/messages [root@lvs-bei keepalived-2.0.13]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.100.100:80 rr -> 192.168.100.20:80 Route 1 0 0 -> 192.168.100.30:80 Route 1 0 0 [root@lvs-bei keepalived-2.0.13]# ip addr show dev ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:dc:10:18 brd ff:ff:ff:ff:ff:ff inet 192.168.100.40/24 brd 192.168.100.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::c1f0:d588:3477:d684/64 scope link valid_lft forever preferred_lft forever ####由于设置了连接保持时间为60秒,一分钟后重新再访问该地址自动轮询交给另一台Web服务器
4.6、配置NFS服务器
安装NFS服务 [root@nfs-server ~]# yum -y install rpcbind nfs-utils 创建测试目录并新建网页 [root@nfs-server ~]# mkdir -p /opt/web1 [root@nfs-server ~]# mkdir -p /opt/web2 [root@nfs-server ~]# echo '<h1>web 1!</h1>' > /opt/web1/index.html [root@nfs-server ~]# echo '<h1>web 2!</h1>' > /opt/web2/index.html 设置访问权限 [root@nfs-server ~]# vi /etc/exports /opt/web1 192.168.100.20(ro) /opt/web2 192.168.100.30(ro) 开启服务并开机自启 [root@nfs-server ~]# systemctl start nfs [root@nfs-server ~]# systemctl start rpcbind [root@nfs-server ~]# systemctl enable nfs [root@nfs-server ~]# systemctl enable rpcbind 查看访问权限 [root@nfs-server ~]# showmount -e Export list for nfs-server: /opt/web2 192.168.100.30 /opt/web1 192.168.100.20
4.7、配置Web1服务器(192.168.100.20)
安装httpd服务 [root@web1 ~]# yum -y install httpd 挂载NFS服务 [root@web1 ~]# mount 192.168.100.50:/opt/web1 /var/www/html 查看权限 [root@web1 ~]# showmount -e 192.168.100.50 Export list for 192.168.100.50: /opt/web2 192.168.100.30 /opt/web1 192.168.100.20 开启httpd服务 [root@web1 ~]# systemctl start httpd 本机访问 [root@web1 ~]# curl http://localhost <h1>web1!</h1> 配置DR模式 [root@web1 ~]# vi web1.sh #!/bin/bash # web1 ifconfig lo:0 192.168.100.100 broadcast 192.168.100.100 netmask 255.255.255.255 up route add -host 192.168.100.100 dev lo:0 echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p &> /dev/null 执行脚本 [root@web1 ~]# sh web1.sh 查看端口信息 [root@web1 ~]# ifconfig ...... lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.100.100 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback) ......
4.8、配置web2服务器(192.168.100.30)
安装httpd服务 [root@web1 ~]# yum -y install httpd 挂载NFS服务 [root@web1 ~]# mount 192.168.100.50:/opt/web2 /var/www/html 查看权限 [root@web1 ~]# showmount -e 192.168.100.50 Export list for 192.168.100.50: /opt/web2 192.168.100.30 /opt/web1 192.168.100.20 开启httpd服务 [root@web1 ~]# systemctl start httpd 本机访问 [root@web1 ~]# curl http://localhost <h1>web2!</h1> 配置DR模式 [root@web1 ~]# vi web2.sh #!/bin/bash # web2 ifconfig lo:0 192.168.100.100 broadcast 192.168.100.100 netmask 255.255.255.255 up route add -host 192.168.100.100 dev lo:0 echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p &> /dev/null 执行脚本 [root@web1 ~]# sh web2.sh 查看端口信息 [root@web1 ~]# ifconfig ...... lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.100.100 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback) ......
4.9、测试web1和web2的IP访问
4.10、测试虚拟IP地址级查看状态
[root@lvs-zhu keepalived-2.0.13]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.100.100:80 rr -> 192.168.100.20:80 Route 1 1 1 -> 192.168.100.30:80 Route 1 1 0
4.10、模拟主调度器故障,验证结果
关闭主调度器keepalived [root@lvs-zhu keepalived-2.0.13]# systemctl stop keepalived.service 查看从调度器状态 [root@lvs-bei keepalived-2.0.13]# tail -f /var/log/messages [root@lvs-bei keepalived-2.0.13]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.100.100:80 rr -> 192.168.100.20:80 Route 1 0 0 -> 192.168.100.30:80 Route 1 0 0 [root@lvs-bei keepalived-2.0.13]# ip addr show dev ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:dc:10:18 brd ff:ff:ff:ff:ff:ff inet 192.168.100.40/24 brd 192.168.100.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.100.100/32 scope global ens33 valid_lft forever preferred_lft forever #虚拟地址漂移到备调度器上 inet6 fe80::c1f0:d588:3477:d684/64 scope link valid_lft forever preferred_lft forever
查看备调度器连接状态
[root@lvs-bei keepalived-2.0.13]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.100.100:80 rr -> 192.168.100.20:80 Route 1 0 1 -> 192.168.100.30:80 Route 1 2 0
4.11、开启主调度器并查看状态
[root@lvs-zhu keepalived-2.0.13]# systemctl start keepalived.service [root@lvs-zhu keepalived-2.0.13]# tail -f /var/log/messages [root@lvs-zhu keepalived-2.0.13]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.100.100:80 rr -> 192.168.100.20:80 Route 1 0 0 -> 192.168.100.30:80 Route 1 0 0 [root@lvs-zhu keepalived-2.0.13]# ip addr show dev ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9a:cd:27 brd ff:ff:ff:ff:ff:ff inet 192.168.100.10/24 brd 192.168.100.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.100.100/32 scope global ens33 valid_lft forever preferred_lft forever #虚拟地址又到主调度器上 inet6 fe80::26b5:ebd3:a0d2:db12/64 scope link valid_lft forever preferred_lft forever
4.12、模拟web服务器故障,查看状态
①关闭web1的httpd服务
Systemctl stop httpd
②测试网页,只能查看web2服务器的网页
③查看调度器节点状态
1 [root@lvs-zhu keepalived-2.0.13]# ipvsadm -Ln 2 IP Virtual Server version 1.2.1 (size=4096) 3 Prot LocalAddress:Port Scheduler Flags 4 -> RemoteAddress:Port Forward Weight ActiveConn InActConn 5 TCP 192.168.100.100:80 rr 6 -> 192.168.100.30:80 Route 1 3 1
④开启Web1服务又可以轮询了