keepalived是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健康检查功能——判断LVS负载调度器,节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集。
Keepalived的热备方式
Keepalived采用VRRP(虚拟路由冗余协议)热备份协议,以软件的方式实现Linux服务器的多机热备功能。VRRP是针对路由器的一种备份解决方案——有多台路由组成一个热备组,通过共用的虚拟IP地址对外提供服务;每个热备组内同一时刻只有一台主路由器提供服务 ,其他路由器处于冗余状态,若当前在线路由器失败,则其他路由器会自动接替(优先级决定接替顺序)虚拟IP地址,以继续提供服务。如下图所示:
热备组内的每台路由器都有可能成为主路由器,虚拟路由器的IP地址可以在热备组内的路由器之间进行转移,所以也称为漂移IP地址。使用keepalived时,漂移地址的实现不需要手动建立虚接口配置文件,而是由keepalived根据配置文件自动管理。
LVS+Keepalived高可用群集
Keepalived的设计目标是构建可用的LVS负载均衡群集,可以调用ipvsadm工具来创建虚拟服务器,管理服务器池,而不仅仅用来做双机热备。使用keepalived构建LVS群集更加简便易用,主要优势体现在:对LVS负载调度器实现热备切换,提高可用性,对服务器池中的节点进行健康检查,自动移除失效节点,恢复后再重新加入。
在基于LVS-Keepalived实现的LVS群集结构中,至少包括两台热备的负载调度器,两台以上的节点服务器,本例将以DR模式的LVS群集为基础,增加一台从负载调度器,使用Keepalived来实现主、从调度器的热备,从而构建建有负载均衡、高可用两种能力的LVS网站群集平台。如下面案例图所示:
使用Keepalived构建LVS群集时,也需要用到ipvsadm管理工具,但大部分工作会由Keepalived自动完成,不需要手动执行ipvsadm。
实验案例:
公司的Web平台已采用LVS-DR负载均衡结构,但只有一个负载均衡器。为了提高Web服务的持续可用性,要求增加一台负载调度器,与原有的调度器实现双击热备,并要求能自动管理服务器池,而不是由管理员手动添加、移除节点,如下图所示:
需求描述:
1. 将现有的LVS群集改造为LVS+Keepalived高可用负载均衡群集
2. 主调度器负则分配客户机请求,若出现故障则由从调度器自动接替服务
3. 启用邮件通知功能,以便及时了解各Web节点服务器的健康状况
实验步骤如下:
此实验是在上篇博客的LVS-DR基础上新增一个从调度器,参考文档https://blog.51cto.com/yangshufan/2065686
1. 配置主调度器
[root@localhost ~]# service ipvsadm stop //清除原有的ipvsadm策略 ipvsadm: Clearing the current IPVS table: [确定] ipvsadm: Unloading modules: [确定] [root@localhost ~]# ipvsadm -ln //确认清空 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@localhost ~]# rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:0 //删除网卡eth0:0 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:1C:B4:FB inet addr:172.16.16.173 Bcast:172.16.16.255 Mask:255.255.255.0 eth1 Link encap:Ethernet HWaddr 00:0C:29:1C:B4:05 inet addr:192.168.7.173 Bcast:192.168.7.255 Mask:255.255.255.0 [root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel //安装支持软件 [root@localhost ~]# tar zxf keepalived-1.2.13.tar.gz -C /usr/src/ [root@localhost ~]# cd /usr/src/keepalived-1.2.13/ [root@localhost keepalived-1.2.13]# ./configure --prefix=/ //安装路径指定为根目录,这样就无需额外创建链接文件 --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64 //只有在使用LVS时,才需要指定此参数 [root@localhost keepalived-1.2.13]# make && make install [root@localhost keepalived-1.2.13]# chkconfig --add keepalived //添加为系统服务 [root@localhost keepalived-1.2.13]# chkconfig keepalived on //设置为开机自启动 [root@localhost keepalived-1.2.13]# cd /etc/keepalived/ [root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak //备份配置文件 [root@localhost keepalived]# vim keepalived.conf //修改配置文件 global_defs { //此区域指定全局参数 notification_email { //邮件服务 [email protected] //收件人地址 } notification_email_from ysf localhost.localdomain //发件人姓名、地址 smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS-1 //本服务器的名称 } vrrp_instance VI_1 { //此区域指定VRRP热备参数 state MASTER //热备状态,MASTER表示为主服务器 interface eth0 //承载VIP地址的物理接口 virtual_router_id 51 //虚拟路由器的ID号,每个热备组必须保持一致 priority 100 //优先级,数值越大,优先级越高 advert_int 1 //通告间隔秒数(心跳频率) authentication { //认证信息,每个热备组必须保持一致 auth_type PASS //认证类型 auth_pass 1111 //密码字串 } virtual_ipaddress { 172.16.16.172 //指定漂移地址(VIP),可以有多个 } }
#下面区域用来配置虚拟服务器 virtual_server 172.16.16.172 80 { //虚拟服务器地址(VIP)、端口 delay_loop 6 //健康检查的间隔秒数(秒) lb_algo rr //轮询(rr)调度算法 lb_kind DR //直接路由(DR)集群工作模式 nat_mask 255.255.255.0 persistence_timeout 50 //连接保持秒数 protocol TCP //应用服务采用TCP协议 real_server 172.16.16.177 80 { //第一个Web节点的地址、端口 weight 1 //节点的权重 TCP_CHECK { //健康检查方式 connect_port 80 //检查的目标端口 connect_timeout 3 //连接超时(秒) nb_get_retry 3 //重试次数 delay_before_retry 3 //重试间隔(秒) } } real_server 172.16.16.178 80 { //第二个Web节点的地址、端口 …… //和第一个的配置相同,省略 } real_server 172.16.16.179 80 { //第三个Web节点的地址、端口 …… } real_server 172.16.16.180 80 { //第四个Web节点的地址、端口 …… } } [root@localhost keepalived]# /etc/init.d/keepalived start 正在启动 keepalived: [确定] [root@localhost keepalived]# ip addr show dev eth0 //查看自动添加的VIP地址 2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:1c:b4:fb brd ff:ff:ff:ff:ff:ff inet 172.16.16.173/24 brd 172.16.16.255 scope global eth0 inet 172.16.16.172/32 scope global eth0 //自动设置的VIP地址 inet6 fe80::20c:29ff:fe1c:b4fb/64 scope link valid_lft forever preferred_lft forever [root@localhost keepalived]# ipvsadm -ln //自动创建虚拟服务器,添加服务器节点 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.16.16.172:80 rr -> 172.16.16.178:80 Route 1 0 0 -> 172.16.16.179:80 Route 1 0 0 -> 172.16.16.180:80 Route 1 0 0
2. 配置从调度器
[root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:1C:B4:4D inet addr:172.16.16.174 Bcast:172.16.16.255 Mask:255.255.255.0 eth1 Link encap:Ethernet HWaddr 00:0C:29:1C:B4:05 inet addr:192.168.7.174 Bcast:192.168.7.255 Mask:255.255.255.0 [root@localhost ~]# modprobe ip_vs [root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel [root@localhost ~]# yum -y install ipvsadm [root@localhost ~]# tar zxf keepalived-1.2.13.tar.gz -C /usr/src/ [root@localhost ~]# cd /usr/src/keepalived-1.2.13/ [root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64 [root@localhost keepalived-1.2.13]# make && make install [root@localhost keepalived-1.2.13]# chkconfig --add keepalived [root@localhost keepalived-1.2.13]# chkconfig keepalived on [root@localhost ~]# cd ~ //从服务器的配置文件可从主服务器中复制 [root@localhost ~]# scp [email protected]:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf [email protected]'s password: keepalived.conf 100% 1378 1.4KB/s 00:00 [root@localhost ~]# vim /etc/keepalived/keepalived.conf //修改其中的三项 router_id LVS-2 //从调度器的名称 state BACKUP //从调度器的热备状态 priority 50 //从调度器的优先级 [root@localhost keepalived]# /etc/init.d/keepalived start 正在启动 keepalived: [确定]
3. 配置Web节点服务器
本实验是基于上篇实验配置的,Web节点服务器的配置不用修改,参考文档https://blog.51cto.com/yangshufan/2065686
4. LVS+Keepalived集成测试
(1)禁用主调度器的eth0网卡,3分钟后重新启用,确认主从热备正常
(2)禁用Web服务器池的一部分节点,确认对Web群集的访问仍然正常
(3)查阅通知邮件内容
[root@localhost ~]# less /var/spool/mail/root From [email protected] Wed Jan 31 05:51:02 2018 Return-Path:X-Original-To: [email protected] Delivered-To: [email protected] Received: from localhost (localhost [127.0.0.1]) by localhost.localdomain (Postfix) with SMTP id 05C61422BA for ; Wed, 31 Jan 2018 05:51:01 +0800 (CST) Date: Tue, 30 Jan 2018 21:51:02 +0000 From: [email protected] Subject: [LVS-1] Realserver [172.16.16.177]:80 - DOWN X-Mailer: Keepalived To: [email protected] Message-Id: <[email protected]> => TCP CHECK failed on service <= ...skipping... From [email protected] Wed Jan 31 05:51:02 2018 Return-Path: X-Original-To: [email protected] Delivered-To: [email protected] Received: from localhost (localhost [127.0.0.1]) by localhost.localdomain (Postfix) with SMTP id 05C61422BA for ; Wed, 31 Jan 2018 05:51:01 +0800 (CST) Date: Tue, 30 Jan 2018 21:51:02 +0000 From: [email protected] Subject: [LVS-1] Realserver [172.16.16.177]:80 - DOWN X-Mailer: Keepalived To: [email protected] Message-Id: <[email protected]> => TCP CHECK failed on service <=