使用Keepalived实现linux高可用集群
安装
apt install libipset-dev keepalived -y
创建账户
useradd -s/usr/sbin/nologin -M -g root keepalived_script
修改Linux
vim /etc/sysctl.conf
#在末尾追加一行:net.ipv4.ip_nonlocal_bind=1
创建Keepalived配置文件
vim /etc/keepalived/keepalived.conf
vrrp_script chk_appsvc {
#定义一个脚本文件,这个脚本文件将用来检测本机是否可用,如果可用则返回0
script /etc/keepalived/keepalived-check-appsvc.sh
interval 1
fall 2
rise 2
}
vrrp_instance VI_1 {
#选择一个网卡
interface ens7
authentication {
auth_type PASS
auth_pass secr3t
}
virtual_router_id 51
virtual_ipaddress {
10.1.96.100 #整个集群的对外地址
}
track_script {
chk_appsvc
}
#状态:MASTER BACKUP STOP FAULT
state MASTER
priority 101
unicast_src_ip 10.1.96.5 #本机地址(DIP)
unicast_peer {
10.1.96.3 #对端地址,其它机器的ip(DIP)
}
#状态切换动作脚本,定义了状态切换时执行的动作
notify /etc/keepalived/keepalived-action.sh
}
创建状态切换动作脚本
vim /etc/keepalived/keepalived-action.sh
chmod +x /etc/keepalived/keepalived-action.sh
#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
case $STATE in
"MASTER")
systemctl start nginx
exit 0
;;
"BACKUP"|"STOP")
#systemctl stop nginx
exit 0
;;
"FAULT")
#systemctl stop nginx
exit 0
;;
*)
exit 1
;;
esac
#当某个服务器成为MASTER的时候,启动nginx服务。
创建状态检测脚本
vim /etc/keepalived/keepalived-action.sh
chmod +x /etc/keepalived/keepalived-check-appsvc.sh
#!/bin/bash
exit 0
#返回0表示当前主机状态正常.如果返回非0,或者设备无法连接,就会触发转移.
"随笔"类型下的内容为原创,转载请注明来源. http://www.cnblogs.com/DragonStart/