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相同
名称 | 操作系统 | 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 |
首先我们在做实验时必须先关闭5台虚拟机的防火墙、核心防护,并配置好本地Yum源。
IP地址规划:
漂移地址(VIP):192.168.100.100
主调度器:192.168.100.21
辅调度器:192.168.100.22
WEB服务器1:192.168.100.23
WEB服务器2:192.168.100.24
存储服务器:192.168.100.25
#####配置主调度器 192.168.100.21 ####### 关闭防火墙
【1】调整/proc响应参数
[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
【2】调整keepalived参数
[root@localhost ~]# yum -y install keepalived ipvsadm
[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_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.100.100
}
}
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fifo_fast state UP group default qlen 1000
link/ether 00:0c:29:bb:29:cc brd ff:ff:ff:ff:ff:ff
inet 192.168.100.21/24 brd 192.168.100.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.100.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:febb:29cc/64 scope link
valid_lft forever preferred_lft forever
#####配置辅调度器 192.168.100.22 ####### 关闭防火墙
【1】调整/proc响应参数
[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
【2】调整keepalived参数
[root@localhost ~]# yum -y install keepalived ipvsadm
[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地址和漂移地址
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:44:0b:2a brd ff:ff:ff:ff:ff:ff
inet 192.168.100.22/24 brd 192.168.100.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe44:b2a/64 scope link
valid_lft forever preferred_lft forever
#####配置存储服务器:192.168.100.25#####
rpm -q nfs-utils ###如果没装,yum -y install nfs-utils
rpm -q rpcbind ###如果没装,yum -y install rpcbind
[root@localhost ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo "this is 51xit" >/opt/51xit/index.html
[root@localhost ~]# echo "this is 52xit" >/opt/52xit/index.html
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl enable nfs
#####配置节点服务器:192.168.100.23####
[root@localhost ~]# yum -y install httpd ###安装Apache
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# yum -y install nfs-utils ###安装nfs
[root@localhost ~]# mount 192.168.100.25:/opt/51xit /var/www/html/ ###临时挂载
###测试:浏览器输入192.168.100.23就能看见51网页###
[root@localhost ~]# vi /etc/fstab
192.168.100.25:/opt/51xit /var/www/html nfs defaults,_netdev 0 0 ###永久挂载
【1】配置虚拟IP地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ ###进入网卡目录
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0 ###复制一个网卡,命名为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]# ifconfig ###查看一下
【2】调整/proc响应参数
[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
[root@localhost network-scripts]# route add -host 192.168.100.10 dev lo:0 ###手工配置一条路由
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.100 dev lo:0
#####配置节点服务器:192.168.100.24####
[root@localhost ~]# yum -y install httpd ###安装Apache
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# yum -y install nfs-utils ###安装nfs
[root@localhost ~]# mount 192.168.100.25:/opt/52xit /var/www/html/ ###临时挂载
###测试:浏览器输入192.168.100.24就能看见52网页###
[root@localhost ~]# vi /etc/fstab
192.168.100.25:/opt/52xit /var/www/html nfs defaults,_netdev 0 0 ###永久挂载
【1】配置虚拟IP地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ ###进入网卡目录
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0 ###复制一个网卡,命名为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]# ifconfig ###查看一下
【2】调整/proc响应参数
[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
[root@localhost network-scripts]# route add -host 192.168.100.10 dev lo:0 ###手工配置一条路由
测试:在浏览器输入192.168.100.100 就会出现其中一个网站,在等一会刷新就会显示另一个网站,LVS+Keepalived负载均衡就做完了