keepalived是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健康检查功能——判断LVS负载调度器,节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集。
■ 专门为LVS和HA设计的一款健康检查工具
在企业应用中,单台服务器承担应用存在单点故障的危险,在企业应用集群中,存在了至少两处单点故障危险,
单点故障一旦发生,企业服务将发生中断,造成极大的危害
Keepalived的设计目标是构建可用的LVS负载均衡群集,可以调用ipvsadm工具来创建虚拟服务器,管理服务器池,而不仅仅用来做双机热备。使用keepalived构建LVS群集更加简便易用,主要优势体现在:对LVS负载调度器实现热备切换,提高可用性,对服务器池中的节点进行健康检查,自动移除失效节点,恢复后再重新加入。
在基于LVS-Keepalived实现的LVS群集结构中,至少包括两台热备的负载调度器,两台以上的节点服务器,本例将以DR模式的LVS群集为基础,增加一台从负载调度器,使用Keepalived来实现主、从调度器的热备,从而构建建有负载均衡、高可用两种能力的LVS网站群集平台。
使用Keepalived构建LVS群集时,也需要用到ipvsadm管理工具,但大部分工作会由Keepalived自动完成,不需要手动执行ipvsadm。
■ Keepalived采用VRRP热备份协议,实现Linux服务器的多机热备功能。
■ VRRP(虚拟路由冗余协议)是针对路由器的一种备份解决方案。
■双机热备的故障切换是由虚拟IP地址的漂移实现,适用于各种应用服务器
■实现基于WEB服务的双机热备
【交 换 机】
●
● 漂移地址:192.168.100.100
●
●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
● ● ● ● ●
● ● ● ● ●
● ● ● ● ●
【调度器1】 【调度器2】 【WEB1】 【WEB2】 【存储】
192.168.100.21 192.168.100.22 192.168.100.23 192.168.100.24 192.168.100.25
■ keepalived配置目录位于/etc/keepalived/
■ keepalived.conf是主配置文件
■常用配置选项讲解
■keppalived 备份服务器的配置与master的配置有三个选项不同
■其他选项与master相同
keepalived是以VRRP协议为实现基础的
VRRP
Virtual Router Redundancy Protocol
虚拟路由冗余协议
可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip
VIP
Virtual IP Address
虚拟IP地址,该路由器所在局域网内其他机器的默认路由为该vip
master
会发组播,当backup收不到VRRP包时就认为master宕掉了
这时就需要根据VRRP的优先级,来选举一个backup当master
这样,就可以保证路由器的高可用了
名称 | 操作系统 | IP地址 |
---|---|---|
LVS-Master | Centos7.6-x86_64 | 192.168.100.21 |
LVS-Backup | Centos7.6-x86_64 | 192.168.100.22 |
WebserverA | Centos7.6-x86_64 | 192.168.100.23 |
WebserverB | Centos7.6-x86_64 | 192.168.100.24 |
NFS共享 | Centos7.6-x86_64 | 192.168.100.25 |
这边我们的漂移地址(VIP):192.168.100.10
首先我们在做实验时必须先关闭5台虚拟机的防火墙、核心防护,并配置好本地Yum源。
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p 让添加进去的配置文件生效
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# yum -y install keepalived ipvsadm 安装keepalived工具
[root@localhost keepalived]# vi /etc/keepalived.conf
global_defs {
router_id HA_TEST_R1 ####本路由器的服务器名称 HA_TEST_R1
}
vrrp_instance VI_1 {
####定义VRRP热备实列
state MASTER ####热备状态,master表示主服务器
interface ens33 ####表示承载VIP地址的物理接口
virtual_router_id 1 ####虚拟路由器的ID号,每个热备组保持一致
priority 100 ####优先级,优先级越大优先级越高
advert_int 1 ####通告间隔秒数(心跳频率)
authentication {
####认证信息,每个热备组保持一致
auth_type PASS ####认证类型
auth_pass 123456 ####认证密码
}
virtual_ipaddress {
####漂移地址(VIP),可以是多个
192.168.30.10
}
}
virtual_server 192.168.100.100 80 {
####虚拟服务器地址(VIP)、端口
delay_loop 15 ####健康检查的时间间隔(秒)
lb_algo rr ####轮询调度算法
lb_kind DR ####直接路由(DR)群集工作模式
persistence 60 ####连接保持时间(秒),若启用请去掉!号
protocol TCP ####应用服务采用的是TCP协议
real_server 192.168.100.23 80 {
####第一个WEB站点的地址,端口
weight 1 ####节点的权重
TCP_CHECK {
####健康检查方式
connect_port 80 ####检查端口目标
connect_timeout 3 ####连接超时(秒)
nb_get_retry 3 ####重试次数
delay_before_retry 4 ####重试间隔(秒)
}
}
real_server 192.168.100.24 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
[root@localhost keepalived]# systemctl start keepalived ####启动keepalived
[root@localhost keepalived]# systemctl enable keepalived ####开机启动keepalived
[root@localhost keepalived]# ip addr show dev ens33 ####查看主控制IP地址和漂移地址
2: ens33: ,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b5:da:33 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.21/24 brd 192.168.100.255 scope global noprefixroute ens33 ## IP地址
valid_lft forever preferred_lft forever
inet 192.168.100.100/32 scope global ens33 ## 漂移地址
valid_lft forever preferred_lft forever
inet6 fe80::c574:d230:3778:e9dd/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost network-scripts]# vi /etc/sysctl.conf //关闭本地路由功能
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost network-scripts]# sysctl -p ###生效
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# yum -y install ipvsadm //是在负载调速器是使用的LVS 群集管理工具
[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak //在修改任何配置文件一定要先备份
[root@localhost keepalived]# vi keepalived.conf
global_defs {
router_id HA_TEST_R2 ####本路由器的服务器名称 HA_TEST_R2
}
vrrp_instance VI_1 {
####定义VRRP热备实列
state BACKUP ####热备状态,backup表示辅服务器
interface ens33 ####表示承载VIP地址的物理接口
virtual_router_id 1 ####虚拟路由器的ID号,每个热备组保持一致
priority 99 ####优先级,优先级越大优先级越高,这里的优先级要比主机小!
advert_int 1 ####通告间隔秒数(心跳频率)
authentication {
####认证信息,每个热备组保持一致
auth_type PASS ####认证类型
auth_pass 123456 ####认证密码
}
virtual_ipaddress {
####漂移地址(VIP),可以是多个
192.168.100.100
}
}
virtual_server 192.168.100.100 80 {
delay_loop 15
lb_algo rr
lb_kind DR
persistence 60
protocol TCP
real_server 192.168.100.23 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.100.24 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
[root@localhost keepalived]# systemctl start keepalived ####启动keepalived
[root@localhost keepalived]# systemctl enable keepalived ####开机启动keepalived
[root@localhost keepalived]# ip addr show dev ens33 ####查看主控制IP地址和漂移地址
[root@localhost keepalived]# yum -y install ipvsadm
[root@localhost keepalived]# ipvsadm -ln
提示:安装了keepalived,会自动添加节点信息,不需要手动的添加,但是他也是根据监听80端口也没有启来,所有做这个步骤的前提就是web的俩台服务器已经安装了httpd服务,并且已经启动了否则节点添加不成功
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# sudo yum install net-tools ## 最小化安装需要装这个,才可以用 ifconfig
[root@localhost network-scripts]# ifconfig
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.10 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.10 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.100.10 dev lo:0
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# yum -y install nfs-utils
[root@localhost ~]# showmount -e 192.168.100.25 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.25:
/opt/Jack 192.168.100.0/24
/opt/Tom 192.168.100.0/24
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.25:/opt/Tom /var/www/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.25:/opt/Tom /var/www/html nfs defaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# init 6 ## 重启测试一下
[root@localhost ~]# curl 192.168.100.23
this is Tom
也可以在浏览器输入 192.168.100.23 测试
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# sudo yum install net-tools ## 最小化安装需要装这个,才可以用 ifconfig
[root@localhost network-scripts]# ifconfig
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.10 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.10 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.100.10 dev lo:0
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# yum -y install nfs-utils
[root@localhost ~]# showmount -e 192.168.100.25 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.25:
/opt/Jack 192.168.100.0/24
/opt/Tom 192.168.100.0/24
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.25:/opt/Jack /var/www/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.25:/opt/Jack /var/www/html nfs defaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# init 6 ## 重启测试一下
[root@localhost ~]# curl 192.168.100.24
this is Jack
1、主调度器ens33网卡MAC地址:00:0c:29:b5:da:33
2、在真机打开 cmd 测试 ping 192.168.100.100
3、arp -a 发现缓存表里面MAC是00:0c:29:b5:da:33
证明数据包往主调度器的MAC地址丢
进行抓包查看一下
4、现在在主调度器关闭 keepalived
[root@localhost ~]# systemctl status keepalived
5、然后再从调度器输入 ip addr show,漂移地址已经到了从调度器了,这时候,从调度器抢占主调度器,进行工作。
6、从调度器ens33网卡MAC地址:00:0c:29:53:1f:1a
7、在真机打开 cmd 测试 ping 192.168.100.100
8、arp -a 发现缓存表里面MAC是 0:0c:29:53:1f:1a,这说明当主调度器宕掉的时候,从调度器已经代替主调度器进行工作了,证明数据包往从调度器的MAC地址丢。
进行抓包查看一下
9、再重新开启keepalived.service
### 开启keepaliced
[root@localhost keepalived]# systemctl start keepalived.service