【转载】centos7系统shell一键keepalived高可用
vim aa.sh
#/bin/bash
read -ep "请输入keepalived所在的本地IP:" IP
read -ep "请输入另一台keepalived所在的本地IP:" IP2
read -ep "请输入你创建的keepalived的VIP:" net
yum -y install keepalived
sed -i '/notification_email {/,/smtp_connect_timeout 30/d' /etc/keepalived/keepalived.conf
sed -i '/vrrp_skip_check_adv_addr/,/vrrp_gna_interval 0/d' /etc/keepalived/keepalived.conf
sed -i 's/eth0/ens33/' /etc/keepalived/keepalived.conf
sed -i '/persistence_timeout 50/d' /etc/keepalived/keepalived.conf
sed -i '/real_server/,$d' /etc/keepalived/keepalived.conf
sed -i '/192.168.200.16/,/192.168.200.17/d' /etc/keepalived/keepalived.conf
sed -i "s/192.168.200.18/$net/" /etc/keepalived/keepalived.conf
sed -i "s/192.168.200.100 443/$net 80/" /etc/keepalived/keepalived.conf
shu=`cat /etc/keepalived/keepalived.conf|grep real_server|wc -l`
if [ $shu -ne 1 ];then
echo " real_server $IP 80{
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
" >>/etc/keepalived/keepalived.conf
fi
systemctl restart keepalived
yum -y install expect
/usr/bin/expect<<EOF
spawn ssh root@$IP2
expect "yes/no" {send "yes\n;exp_continue"}
expect "password" {send "123456\n"}
set timeout 1000
expect "~" {send "yum -y install keepalived\n"}
expect "~" {send "exit\n"}
expect eof
EOF
/usr/bin/expect<<EOF
spawn scp /etc/keepalived/keepalived.conf $IP2:/etc/keepalived/keepalived.conf
expect "yes/no" {send "yes\n;exp_continue"}
expect "password" {send "123456\n;"}
expect eof
EOF
/usr/bin/expect<<EOF
spawn ssh root@$IP2
expect "yes/no" {send "yes\n;exp_continue"}
expect "password" {send "123456\n"}
expect "~" {send "sed -i 's/$IP/$IP2/' /etc/keepalived/keepalived.conf\n"}
expect "~" {send "sed -i 's/priority 100/priority 99/' /etc/keepalived/keepalived.conf\n"}
expect "~" {send "sed -i 's/MASTER/BACKUP/' /etc/keepalived/keepalived.conf\n"}
expect "~" {send "systemctl restart keepalived\n"}
expect "~" {send "exit\n"}
expect eof
EOF
sh aa.sh
记住,我的ssh连接密码是123456!,还需要等待
原文链接:https://blog.csdn.net/a13568hki/article/details/102960722