一.Keepalived工具介绍

专为LVS和HA设计的一款健康检查工具

​• 支持故障自动切换(Failover)

• 支持节点健康状态检查(Health Checking)

• 官方网站:http://www.keepalived.org/

二.Keepalived工作原理

• Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题

• 在一个LVS服务集群中通常有主服务器(MASTER)和备份服务器(BACKUP)两种角色的服务器,但是对外表现为一个虚拟IP,主服务器会发送VRRP通告信息给备份服务器,当备份服务器收不到VRRP消息的时候,即主服务器异常的时候,备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性

三.部署LVS+Keepalived 高可用群集

环境准备

主DR 服务器: 192.168.116.100
备DR 服务器: 192.168.116.70
Web 服务器1: 192.168.116.80
Web 服务器2: 192.168.116.90
nfs 服务器: 192.168.116.60
客户端:192.168.116.123
vip:192.168.116.200

1.配置负载调度器(主、备相同;192.168.116.100;192.168.116.70)

systemctl stop firewalld.service
setenforce 0

yum -y install ipvsadm keepalived
modprobe ip_vs
cat /proc/net/ip_vs

(1)配置keeplived(主、备DR 服务器上都要设置)
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
......
global_defs { #定义全局参数
--10行--修改,邮件服务指向本地
smtp_server 127.0.0.1
--12行--修改,指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02
router_id LVS_01
}

vrrp_instance VI_1 { #定义VRRP热备实例参数
--20行--修改,指定热备状态,主为MASTER,备为BACKUP
state MASTER
--21行--修改,指定承载vip地址的物理接口
interface ens33
--22行--修改,指定虚拟路由器的ID号,每个热备组保持一致
virtual_router_id 10
--23行--修改,指定优先级,数值越大优先级越高,主为100,备为99
priority 100
advert_int 1 #通告间隔秒数(心跳频率)
authentication { #定义认证信息,每个热备组保持一致
auth_type PASS #认证类型
--27行--修改,指定验证密码,主备服务器保持一致
auth_pass 123456
}
virtual_ipaddress { #指定群集vip地址
192.168.116.200
}
}
--36行--修改,指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
virtual_server 192.168.116.200 80 {
delay_loop 6 #健康检查的间隔时间(秒)
lb_algo rr #指定调度算法,轮询(rr)
--39行--修改,指定群集工作模式,直接路由(DR)
lb_kind DR
persistence_timeout 50 #连接保持时间(秒)
protocol TCP #应用服务采用的是 TCP协议
--43行--修改,指定第一个Web节点的地址、端口
real_server 192.168.116.80 80 {
weight 1 #节点的权重
--45行--删除,添加以下健康检查方式
TCP_CHECK {
connect_port 80 #添加检查的目标端口
connect_timeout 3 #添加连接超时(秒)
nb_get_retry 3 #添加重试次数
delay_before_retry 4 #添加重试间隔
}
}

real_server 192.168.116.90 80 { #添加第二个 Web节点的地址、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
##删除后面多余的配置##
}


systemctl start keepalived
ip addr show dev ens33 #查看虚拟网卡vip

(2)配置分发策略(#keepalived配置好后,可以不用再配ipvsadm 分发策略)
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm

ipvsadm -C
ipvsadm -A -t 192.168.116.200:80 -s rr
ipvsadm -a -t 192.168.116.200:80 -r 192.168.116.80:80 -g
ipvsadm -a -t 192.168.116.200:80 -r 192.168.116.90:80 -g
ipvsadm

ipvsadm -ln
ipvsadm-save > /etc/sysconfig/ipvsadm

#如果没有vip的分发策略。则重启keepalived 服务

(3) 调整内核 proc 响应参数,关闭linux内核的重定向参数响应
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

sysctl -p

LVS+Keepalived 群集部署_ipad

LVS+Keepalived 群集部署_ipad_02

LVS+Keepalived 群集部署_服务器_03

LVS+Keepalived 群集部署_vim_04

LVS+Keepalived 群集部署_服务器_05

LVS+Keepalived 群集部署_ipad_06

LVS+Keepalived 群集部署_ipad_07

LVS+Keepalived 群集部署_服务器_08

LVS+Keepalived 群集部署_ipad_09

LVS+Keepalived 群集部署_vim_10

注:记得也要在备用DR 服务器进行相关操作,其中需要修改的配置文件内容如下图,其它地方保持不变

LVS+Keepalived 群集部署_服务器_11

2.部署共享存储(NFS服务器:192.168.116.60)

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0

yum install nfs-utils rpcbind -y
systemctl start nfs.service
systemctl start rpcbind.service
systemctl enable nfs.service
systemctl enable rpcbind.service

mkdir /opt/accp /opt/benet
chmod 777 /opt/accp /opt/benet

vim /etc/exports
/usr/share *(ro,sync)
/opt/accp 192.168.116.0/24(rw,sync)
/opt/benet 192.168.116.0/24(rw,sync)

--发布共享---
exportfs -rv

LVS+Keepalived 群集部署_ipad_12

LVS+Keepalived 群集部署_ipad_13

LVS+Keepalived 群集部署_vim_14

LVS+Keepalived 群集部署_ipad_15

LVS+Keepalived 群集部署_vim_16

LVS+Keepalived 群集部署_服务器_17

3.配置节点服务器(192.168.116.80;192.168.116.90)

systemctl stop firewalld
setenforce 0

yum -y install httpd
systemctl start httpd

vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.116.200
NETMASK=255.255.255.255

service network restart 或 systemctl restart network
ifup lo:0
ifconfig lo:0
route add -host 192.168.116.200 dev lo:0

vim /etc/sysctl.conf

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

sysctl -p
--192.168.116.80---
mount.nfs 192.168.116.60:/opt/accp /var/www/html

--192.168.116.90---
mount.nfs 192.168.116.60:/opt/benet /var/www/html

LVS+Keepalived 群集部署_服务器_18

LVS+Keepalived 群集部署_vim_19

LVS+Keepalived 群集部署_服务器_20

LVS+Keepalived 群集部署_服务器_21

LVS+Keepalived 群集部署_服务器_22

LVS+Keepalived 群集部署_vim_23

LVS+Keepalived 群集部署_服务器_24

LVS+Keepalived 群集部署_ipad_25

4.测试验证

在客户端访问 http://192.168.116.200/ ,默认网关指向 192.168.116.200
再在主服务器关闭 keepalived 服务后在测试,systemctl stop keepalived

LVS+Keepalived 群集部署_ipad_26

LVS+Keepalived 群集部署_vim_27

LVS+Keepalived 群集部署_服务器_28

LVS+Keepalived 群集部署_ipad_29

LVS+Keepalived 群集部署_ipad_30

LVS+Keepalived 群集部署_服务器_31

LVS+Keepalived 群集部署_服务器_32

LVS+Keepalived 群集部署_ipad_33