1 keepalived
lvs+keepalive:高可用集群
keepalived为lvs应运而生的高可用服务,lvs的调度器无法做高可用,也是keepalived这个软件,实现的是调度器的高可用
但是:keepalived不是专门为lvs集群服务的,可以做其他代理服务器的高可用。
lvs的高可用集群:主调度器和备调度器(可以有多个)一主二备 一主一备。
主调度器能够正常运行时,由主调度器进行后端真实服务器的分配处理。其余的备用调度器处在冗余状态,不参与集群的运转,只有主调度器出现了故障无法运行,备调度器才会承担主调度器的工作。
一旦主调度器恢复工作,继续由主调度器进行处理,备调度器又成了冗余。
2.VRRP
VRRP:keepalived是基于vrrp协议实现lvs服务的高可用。解决了调度器单节点的故障问题。
VRRP协议:提供网络路由器的可靠性开发的一种协议。
选举出主和备,预先设备好了主和备的优先级,主的优先级较高,备的优先级低,一旦开启服务器,优先级高的,会自动抢占主的位置。
VRRP组播通信:224.0.0.18 VRRP协议当中的主备服务器通过组播地址进行通信,交换主备服务器之间的运行状态,主服务器会发生VRRP报文消息,以告知其他备服务器,主服务器现在的状态。
主备切换:主服务器发生故障或者不可达,VRRP协议会把请求转移到备服务器,通过组播地址,VRRP可以迅速的通知其他服务器发生了主备切换,确保新的主服务器可以正常的处理客户端的请求
故障恢复:一旦主服务器恢复通信,由组播地址进行通信,发现恢复的主服务器优先级更高,会抢占原主服务器的位置,成为主服务器,调度和接收请求。
3.keepalived的体系架构
全局模块:core模块 负载整个keepalived启动加载和维护
VRRP模块:实现vrrp协议,主备切换
check模块:负责健康检查,检查后端真实服务器的健康检查。配置在真实服务器的模块当中
4.lvs-DR模式结合keepalived
test1:主调度器 192.168.233.10
test2:备调度器 192.168.233.20
后端服务器1 192.168.233.30
后端服务器2 192.168.233.40
vip 192.168.66.100
客户端:192.168.233.61
systemctl stop firewalld
setenforce 0
test1 test2
yum -y install ipvsadm keepalived
cd /etc/keepalived
ls
cp keepalived.conf keepalived.conf.bak
test1
vim keepalived.conf
set nu
10行修改
smtp_server 127.0.0.1

12行修改
router_id LVS_01

第15行注释
#vrrp_strict

22行

26行

28行 优先级

29行

31行 验证密码,要主备一致 默认即可

35行vip地址

41行

52行
先把54行 100dd

配置一个 复制粘贴

systemctl restart keepalived.service
所有注释都要删除
ifconfig

打开/etc/sysctl.conf

sysctl -p
test2:
scp root@192.168.66.15:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
vim /etc/keepalived
systemctl restart keepalived
ipvsadm -ln
test3,4
yum -y install nginx
test4
cd /usr/local/nginx/html
vim index.html
this is test4
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.66.100
NETMASK=255.255.255.255
ifup ifcfg-lo:0
route add -kost 192.168.233.100 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
test3 同理
客户机:
curl 192.168.233.100

test1
ipaddr

test2
ip addr

5.脑裂
脑裂:主和备同时拥有vip地址,在高可用系统当中,联系两个节点的心跳线,本来是一体的,动作协调的高可用系统。心跳线断开之后,分裂成两个独立的个体。主备之间失去了联系,都以为是对方出现了故障,两个调度器,就像脑裂人一样,开始抢占主的位置,抢占vip,主也有vip,备也有vip地址,导致整个集群失败。
软件层面:
1.配置文件
2.通过tcpdump抓包分析
重启
网络层面:
高可用服务器之间心跳线检测失败。主备之间无法进行通信,
硬件层面:
连接主备之间的心跳线老化
网卡或者网卡驱动失效,IP地址配置冲突
防火墙没有配置心跳线消息的传输通道,导致检测失败
后端服务器的配置问题,心跳方式不同,心跳广播冲突,软件bug
如何解决keepalived脑裂问题:
1、硬件:准备两条心跳线,这样断了一条,依然能够传输心跳消息
2、设置防火墙,一定要让心跳消息通过
3、依靠监控软件,实时监测(zabbix)第一时间邮件告警
6.nginx+keepalived
nginx 3 台
nginx1 192.168.233.61 主
nginx2 192.168.233.62 备
nginx3 192.168.233.63 客
nginx先关防火墙
nginx1 nginx2
systemctl stop firewalld
setenforce 0
systemctl restart nginx
yum -y install keepalived
nginx1
cd /etc/keepalived
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf



剩余全部删除
在全局和vrrp之间插一段

vrrp_script check_nginx {
script "/opt/check_nginx.sh"
interval 5
}

nginx2

vim


cd /opt/
vim check_nginx.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &> /dev/null
if [ $? -ne 0 ]
then
systemctl stop keepalived
fi
wq!
chmod 777 check_nginx.sh
sz check_nginx.sh
nginx1
cd /opt
把sh 投进去
chmod 777 check_nginx.sh
nginx1
systemctl restart keepalived
ip addr

nginx2
systemctl restart keepalived
ipaddr

nginx1
systemctl stop nginx
nginx2
ip addr

nginx1
systemctl restart nginx
systemctl restart keepalived
ip addr

test2
