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!,还需要等待

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值