在高可用(HA)系统中,当联系2个节点的“心跳线"断开时,本来为一整体、动作协的HA系统,就分裂成为2个独立的个体。由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人"一样,争抢“共享资源"、争起"“应用服务",就会发生严重后果——或者共享资源被瓜分、2边"服务"都起不来了;或者2边“服务"都起来了,但同时读写"共享存储",导致数据损坏(常见如数据库轮询着的联机日志出错)。
Server1:192.168.100.10
Server2:192.168.100.20
VIP:192.168.100.100
一、Server1:Master的配置
Server1:192.168.100.10
#创建etc下的keepalived目录,编辑配置文件
[root@server1 ~]# yum -y install keepalived #安装keepalived
[root@server1 ~]# vim /etc/keepalived/keepalived.conf #编辑主配置文件
#将原有的配置文件清空添加如下
!Configuration File for keepalived #keepalived配置文件文档声明
global_defs {
#全局配置
router_id 1 #路由器编号,设备在组中的标识
}
#vrrp_script chk_nginx { #健康检查
# script "/etc/keepalived/ck_ng.sh" #检查脚本
# interval 2 #检查频率/秒
# weight -5 #优先级减5
# fall 3 #失败三次
# }
vrrp_instance VI_1 {
#VRRP模块实例 VI_1,实例名,两台路由器相同
state MASTER #主或从状态
interface ens32 #监控心跳网卡,(因为实验环境就一张网卡所以监控ens32)
mcast_src_ip 192.168.100.10 #心跳源IP
virtual_router_id 55 #虚拟路由编号,主备一致
priority 100 #优先级
advert_int 1 #心跳间隔/秒
authentication {
#认证模块
auth_type PASS #认证类型为密码认证
auth_pass 123456 #认证密码123456
}
virtual_ipaddress {
#VIP
192.168.100.100
}
#track_script { #跟踪脚本
# chk_nginx
# }
}
[root@server1 ~]# scp -r /etc/keepalived/keepalived.conf 192.168.100.20:/etc/keepalived/ #复制至back服务器
[root@server1 ~]# systemctl enable keepalived #开机自启
#安装nginx
[root@server1 ~]# yum -y install nginx #安装nginx,默认yum源中1.16.1版本
[root@server1 ~]# systemctl enable nginx --now
[root@server1 ~]# vim /usr/share/nginx/html/index.html #测试keepalived修改默认主页内容
...
131 <div id="content">
132
133 <h1>Welcome to CentOS Pakho one</h1>
...
[root@server1 ~]# systemctl start keepalived #启动keepalived
http://192.168.100.10/
Server2:192.168.100.20
#backup服务器的配置几处修改
[root@server2 ~]# yum -y install keepalived
[root@server2 ~]# vim /etc/keepalived/keepalived.conf
...
state BACKUP #改为backup服务器
...
mcast_src_ip 192.168.100.20 #改为backup真实服务器
...
priority 99 #优先级99
[root@server2 ~]# systemctl enable keepalived
[root@server2 ~]# yum -y install nginx
[root@server2 ~]# systemctl enable nginx --now
[root@server2 ~]# vim /usr/share/nginx/html/index.html
...
131 <div id="content">
132
133 <h1>Welcome to CentOS Pakho two</h1>
...
[root@server2 ~]# systemctl start keepalived
http://192.168.100.20/
#访问vip: http://192.168.100.100/
#拔掉master服务器网线模拟master服务器宕机
http://192.168.100.100/ #再次访问vip
[root@server1 ~]# systemctl stop nginx
一、编辑监控脚本
Server1:192.168.100.10
#添加nginx监控脚本
[root@server1 ~]# vim /etc/keepalived/ck_ng.sh
#!/bin/bash
#检查nginx进程是否存在
counter=$(ps -C nginx --no-heading | wc -l)
if [ "${counter}" = 0 ] ;then
#尝试启动一次nginx,停止五秒后再次检测
systemctl restart nginx
sleep 5
if [ "${counter}" = 0 ];then
#如果没有启动成功,杀死keepalived进程触发主备切换
systemctl stop keepalived
fi
fi
[root@server1 ~]# chmod +x /etc/keepalived/ck_ng.sh
[root@server1 ~]# scp /etc/keepalived/ck_ng.sh 192.168.100.20:/etc/keepalived/
Server2:192.168.100.20
[root@server2 ~]# chmod +x /etc/keepalived/ck_ng.sh
二、启动监控脚本
Server1:192.168.100.10
[root@server1 ~]# vim /etc/keepalived/keepalived.conf
#取消注释即可
...
vrrp_script chk_nginx {
script "/etc/keepalived/ck_ng.sh" #脚本位置
interval 2 #每两秒钟执行一次
weight -5 #减去本机优先级
fall 3 #尝试三次
}
...
track_script {
chk_nginx #chk_nginx对应上方策略名
}
...
[root@server1 ~]# systemctl restart nginx keepalived
Server2:192.168.100.20同理
[root@server2 ~]# vim /etc/keepalived/keepalived.conf
...
[root@server2 ~]# systemctl restart nginx keepalived
Master:192.168.100.10
backup:192.168.100.20
web1:192.168.100.30
web2:192.168.100.40
VIP:192.168.100.100
一、在Master上安装Keepalived
ipvsadm安装并不启动,因为在keepalived配置中集成了ipvs代码
[root@master ~]# yum -y install keepalived ipvsadm
二、在Master上修改配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf
#将原有的配置文件清空添加如下
#Keepalived配置
!Configuration File for keepalived #keepalived配置文件文档声明
global_defs {
#默认的全局配置
route_id Director1 #该设备在组当中的名字,两边不一样
}
vrrp_instance VI_1 {
#VRRP模块实例 VI_1,实例名,两台路由器相同
state MASTER #主或从状态,另外一台是BACKUP
interface ens32 #心跳网卡
virtual_router_id 51 #虚拟路由编号,两边一样即可
priority 150 #优先级
advert_int 1 #检查间隔/秒
authentication {
#认证模块
auth_type PASS #认证类型为密码认证
auth_pass 1111 #认证密码1111
}
virtual_ipaddress {
#VIP和工作接口
192.168.100.100/24 dev ens32
}
}
#LVS-DR配置
virtual_server 192.168.100.100 80 {
#VIP
delay_loop 3 #服务轮询时间间隔,每三秒测试真实服务器是否存活
lb_algo rr #负载均衡策略算法 rr:轮询
lb_kind DR #LVS集群模式 DR
protocol TCP #协议为TCP协议
real_server 192.168.100.30 80 {
#真实后端服务器
weight 1 #权重1
TCP_CHECK {
#TCP校验
connect_timeout 3 #健康检查方式,连接超时时间,与delay_loop配合使用
}
}
real_server 192.168.100.40 80 {
#真实后端服务器
weight 1 #权重1
TCP_CHECK {
#TCP校验
connect_timeout 3 #健康检查方式,连接超时时间,与delay_loop配合使用
}
}
}
三、在backup上安装keepalived
ipvsadm安装不启动
[root@backup ~]# yum -y install keepalived ipvsadm
四、拷贝Master上的keepalived.conf到backup上
[root@backup ~]# scp 192.168.100.10:/etc/keepalived/keepalived.conf /etc/keepalived/
五、拷贝后修改配置文件
[root@backup ~]# vim /etc/keepalived/keepalived.conf
...
route_id Director2
...
state BACKUP
...
priority 100 #优先级100,低于Master即可
...
六、Master和backup上启动服务
[root@master ~]# systemctl enable keepalived --now
[root@master ~]# shutdown -r now #重启
[root@backup ~]# systemctl enable keepalived --now
[root@backup ~]# shutdown -r now
七、Web服务器配置
web1和web2相同配置
[root@web1 ~]# yum -y install httpd && systemctl enable httpd --now
[root@web1 ~]# echo Pakho1 > /var/www/html/index.html
[root@web2 ~]# yum -y install httpd && systemctl enable httpd --now
[root@web2 ~]# echo Pakho2 > /var/www/html/index.html
八、配置虚拟地址
[root@web1 ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0
[root@web1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
#其余行删除,修改如下
DEVICE=lo:0 #子接口
IPADDR=192.168.100.100 #VIP地址
NETMASK=255.255.255.255 #子网掩码
ONBOOT=yes #开机自启
[root@web2 ~]# scp 192.168.100.30:/etc/sysconfig/network-scripts/ifcfg-lo:0 /etc/sysconfig/network-scripts/
九、配置路由
[root@web1 ~]# vim /etc/rc.local
...
/sbin/route add -host 192.168.100.100 dev lo:0
[root@web2 ~]# vim /etc/rc.local
...
/sbin/route add -host 192.168.100.100 dev lo:0
十、配置ARP
[root@web1 ~]# vim /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 #忽略arp请求,可以回复
[root@web2 ~]# vim /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@web1 ~]# shutdown -r now
[root@web2 ~]# shutdown -r now
十一、测试
#将Master网线断开
http://192.168.100.100/
#将Master网线连接,断开backup
http://192.168.100.100/