1,安装keepalived高可用软件
yum -y install keepalived
2, 创建一个监听端口脚本
vi /etc/keepalived/check_port.sh
#!/bin/bash
#keepalived 监控端口脚本
#使用方法:
#在keepalived的配置文件中
#vrrp_script check_port {#创建一个vrrp_script脚本,检查配置
#script “/etc/keepalived/check_port.sh 6379” #配置监听的端口
#interval 2 #检查脚本的频率,单位(秒)
#}
CHK_PORT=CHK_PORT" ];then
PORT_PROCESS=ss -lt|grep $CHK_PORT|wc -l if [ $PORT_PROCESS -eq 0 ];then
echo “Port $CHK_PORT Is Not Used,End.”
exit 1
fi
else
echo “Check Port Cant Be Empty!”
fib,创建主配置文件:(在10.4.7.11上)
vi /etc/keepalived/keepalived.conf! Configuration File for keepalived
global_defs {
router_id 10.4.7.11}
vrrp_script chk_nginx {
script “/etc/keepalived/check_port.sh 7443”
interval 2
weight -20
}vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 251
priority 100
advert_int 1
mcast_src_ip 10.4.7.11
nopreempt
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.4.7.10
}
}
b,创建辅助节点主配置文件:
(在10.4.7.12上)
vi! Configuration File for keepalived
global_defs {
router_id 10.4.7.12
}
vrrp_script chk_nginx {
script “/etc/keepalived/check_port.sh 7443”
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 251
mcast_src_ip 10.4.7.12
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.4.7.10
}
}
keepalived的抢占与非抢占模式 背景:俩节点haproxy通过keepalived实现高可用
说明:harpxy的实际运行过程中,当master发生异常,且后期恢复master正常后,存在抢占或非抢占两种情况。简单点说抢占模式就是,当master宕机后,backup
接管服务。后续当master恢复后,vip漂移到master上,master重新接管服务,多了一次多余的vip切换,而在实际生产中是不需要这样。实际生产中是,当
原先的master恢复后,状态变为backup,不接管服务,这是非抢占模式。接下来分4种情况说明
1)俩台都为master时,比如server1的优先级大于server2,keepalived启动后server1获得master,server2自动降级为backup。此时server1宕机的话,server2接替
服务,当server1恢复后,server1又变为master,重新接管服务,server2变为backup。属于抢占式。2)server1为master,server2位backup,且master优先级大于backup。keepalived启动后server1获得master,server2为backup。当server1宕机后,
server2接管服务。当server1恢复后,server1重新接管服务变为master,而server2变为backup。属于抢占式3)server1为master,server2位backup,且master优先级低于backup。keepalived启动后server2获得master,server1为backup。当server2宕机后,
server1接管服务。此时server2恢复后抢占服务,获得master,server1降级将为backup。属于抢占式以上3种,只要级别高就会获取master,与state状态是无关的
4)server1和server2都为backup。我们要注意启动server服务的启动顺序,先启动的升级为master,与优先级无关。。且配置nopreempt项
比如server1获得master权限,server2为backup。此时server1宕机后,server2接管服务升级为master。当server1恢复后权限将为backup,不会争抢
server2的master权限,server2将会继续master权限。属于非抢占式重点:第4种非抢占式俩节点state必须为bakcup,且必须配置nopreempt
注意:这样配置后,我们要注意启动服务的顺序,优先启动的获取master权限,与优先级没有关系了
总结:抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来。非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP