一键安装keepalive
2021-06-12 14:02 那个,我 阅读(442) 评论(0) 编辑 收藏 举报启动应用程序后
将下载的keepalive*.tar.gz版本与该脚本放在同一目录。
执行命令一键安装,如:
sh ins_keep_app.sh keepalived-2.2.2.tar.gz
注意
- 部分变量需要根据自身情况修改脚本,不修改将提示输入,如:
ifname='' # 网卡名
rip='' # 网卡实IP
vip='' # 浮动IP
port='' # 监控端口
priority='' # 优先级 0-100
- keepalive的安装需要依赖部分软件包。需要提前配置好yum
ins_keep_app.sh
#!/bin/bash
# 说明:
# 监控应用端口是否正常,如果不正常则杀keepalive进程,实现切换。
#
# 变量设置,请根据实际情况变更。
ifname='' # 网卡名
rip='' # 网卡实IP
vip='' # 浮动IP
port='' # 监控端口
priority='' # 优先级 0-100
if [ ! $ifname ];then
read -p "网卡名:" ifname
fi
if [ ! $rip ];then
read -p "真实IP地址:" rip
fi
if [ ! $vip ];then
read -p "浮动IP地址:" vip
fi
if [ ! $port ];then
read -p "监控端口:" port
fi
if [ ! $priority ];then
read -p "节点优先级(0-100):" priority
fi
# 判断是否有参数
if [ x$1 == x ];then
echo "Usage: sh $0 keepalive_filename"
exit 1
fi
# 安装依赖包
yum install -y gcc openssl openssl-devel libnl libnl-devel libnl3 libnl3-devel
filename=$1
tar -xvf $filename
cd ${filename%.*.*}
./configure --prefix=/usr/local/keepalived
make && make install
confdir=/etc/keepalived
if [ ! -d $confdir ];then
mkdir $confdir
fi
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
router_id HA
script_user root #下面的脚本由谁执行
enable_script_security
}
vrrp_instance VI_1 {
state BACKUP
interface $ifname #网卡接口
virtual_router_id 20
priority $priority #优先级
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
$vip # VIP 地址
}
}
virtual_server $vip $port { # VIP 地址+端口
delay_loop 2 #每个2秒检查一次real_server状态
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP
real_server $rip $port { # 真实IP地址+端口
weight 3
notify_down /etc/keepalived/kill_keepalive.sh #检测到服务down后执行的脚本
TCP_CHECK {
connect_timeout 10 #连接超时时间
retry 3 #重连次数 老版本为: nb_get_retry
delay_before_retry 3 #重连间隔时间
connect_port $port #健康检查端口
}
}
}
EOF
cat > /etc/keepalived/kill_keepalive.sh <<EOF
#!/bin/bash
pkill keepalived
EOF
chmod +x /etc/keepalived/kill_keepalive.sh
systemctl restart keepalived
systemctl enable keepalived
systemctl status keepalived
如果不需要监控APP程序,只实现宕机情况下的IP切换,使用如下脚本
ins_keep_vip.sh
#!/bin/bash
# 说明:
# 实现虚拟IP的挂载,节点故障后自动切换到备机。
# 变量设置
# 请根据实际情况修改如下变量
ifname='eth0'
priority='100'
vip='192.168.10.20'
# 判断是否有参数
if [ x$1 == x ];then
echo "Usage:$0 keepalive_filename"
exit 1
fi
# 安装依赖包
yum install -y gcc openssl openssl-devel libnl libnl-devel libnl3 libnl3-devel
filename=$1
tar -xvf $filename
cd ${filename%.*.*}
./configure --prefix=/usr/local/keepalived
make && make install
confdir=/etc/keepalived
if [ ! -d $confdir ];then
mkdir $confdir
fi
cat >>/etc/keepalived/keepalived.conf << EOF
global_defs {
router_id HA
}
vrrp_instance VI_1 {
state BACKUP
interface $ifname #网卡接口
virtual_router_id 20
priority $priority #优先级
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
$vip # VIP 地址
}
}
EOF
systemctl restart keepalived
systemctl enable keepalived
systemctl status keepalived
学习如茶,需细细品味。