标签:keepaliv 等等 conf with server 步骤 写入 interval 内容
前言:名词解释:
VIP:用户通过访问VIP虚拟IP地址,从而访问对应的主节点
选举:两台业务系统,由选举的方式确认谁是主节点,谁是备节点
心跳线:选举之后主要用来检测对方的状态
列脑:主备权重一致,造成列脑
keepalived的高可用列脑
由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方的心跳消息,各自取得资源及服务的所有权,而此时的两台高可用服务器又都还活着。
原因:
服务器网线松动等网络故障
硬件故障发生损坏现象而崩溃
都开启firewalld防火墙
nginx服务死掉等等
实验环境:
服务器系统 | 角色 | 外网IP | 内网IP |
---|---|---|---|
centos7 | master | 10.0.0.5 | 172.16.1.5 |
centos7 | backup | 10.0.0.6 | 172.16.1.6 |
环境准备:
两台nginx_proxy服务器都需安装
[root@1b01t ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@1b01 ~]# yum install keepalived -y
[root@1b01 ~]# yum install nginx -y
实验步骤:
1.两台nginx_proxy都执行以下步骤
[root@1b01 ~]# mkdir -p /server/scripts/
[root@1b01 scripts]# vim keepalived.sh
#!/bin/sh
nginxpid=$(ps -C nginx --no-header|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginxpid -eq 0 ];then
systemctl start nginx
sleep 3
#2.等待3秒后再次获取一次Nginx状态
nginxpid=$(ps -C nginx --no-header|wc -l)
#3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
fi
fi
2.master keepalibed 的配置文件
[root@1b01 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_01
}
#1.每5秒执行一次脚本, 脚本执行内容不能超过5秒,否则会被中断再次重新运行脚本
vrrp_script keepalived {
script "/server/scripts/keepalived.sh"
interval 5
weight 50
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
#2.调用并运行该脚本
track_script {
keepalived
}
}
3.backup keepalived的配置文件
[root@1b02 scripts]# vim /etc/keepalived/keepalived.conf
lobal_defs {
router_id lb02
}
#1.每5秒执行一次脚本, 脚本执行内容不能超过5秒,否则会被中断再次重新运行脚本
vrrp_script keepalived {
script "/server/scripts/keepalived.sh"
interval 5
weight 50
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
#2.调用并运行该脚本
track_script {
keepalived
}
}
3.2 在备上编写测试脚本,如果能ping 通主,并且还有vip的话则认为产生列脑
[root@lb02 ~]# cat check_split_brain.sh
#!/bin/sh
lb01_vip=10.0.0.3
lb01_ip=10.0.0.5
while true;do
ping -c 2 -W 3 $lb01_ip &>/dev/null
if [ $? -eq 0 -a ip add|grep "$lb01_vip"|wc -l
-eq 1 ];then
echo "ha is split brain.warning."
else
echo "ha is ok"
fi
sleep 5
done
测试:
随意在[root@1b01 ~]# vim /etc/nginx/nginx.conf 的配置文件里面写入字符,让nginx启动失效
[root@1b01 ~]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
检查会发现它自已通过脚本切换到backup keepalived
[root@1b01 ~]# hostname -I
10.0.0.5 172.16.1.5
[root@1b02 scripts]# hostname -I
10.0.0.6 10.0.0.3 172.16.1.6
则成功!!!!!!
标签:keepaliv 等等 conf with server 步骤 写入 interval 内容
原文地址:http://blog.51cto.com/13523963/2334475