shell脚本企业实战系列-keepalived主从脚本,交互式

14 篇文章 0 订阅
3 篇文章 0 订阅

交互式keepalived主从配置脚本,安装直接选择安装的主或者从即可,实战过无数次

#!/bin/bash
#日期20200723
#editor by zyh
#版本v0.1
#用户手动输入要创建的角色是主master 还是从
echo "
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      + 1、此脚本为keepalived主从脚本,请首先确定安装机器是主还是从.  +
      + 3、默认的安装路径在/etc下,如果你要改变安装路径,请修         +
      + 4、运行脚本会让您选择是主节点还是从节点,                     +
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"

echo "请输入keepalived的主从信息,如果是主,请输入1,如果是从,请输入0"
read  -p "请输入server的角色信息,主从信息 master(1)or slave(0)  > " roleinfo  #设置服务器ip地址 
if [ ${roleinfo} == "1" ];then
   router_id=100
   state=MASTER
elif [ ${roleinfo} == "0" ];then
   router_id=90
   state=BACKUP
else 
    echo "您输入的信息有误,请检查输入的是否正确"
    exit 0
fi

#设置服务器的虚地址vip,需要根据项目去调整  
vip=192.168.0.116
#服务器网卡
interface=ens33
OS=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`

#检测服务器是否可以上网,如果不可以退出
echo "检查网络是否可以访问外网,不能访问外网将停止脚本运行"
check_net=`curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com`
if [ ${check_net} -ne 200 ];then
	echo "请检查网络是否正常,能否访问外网"
	exit 1
fi

#安装keepalived软件

yum install  net-tools keepalived -y


#创建脚本存放目录
mkdir /etc/keepalived/scripts
#备份并替换keepalived配置文件
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

#增加虚地址到配置文件
cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived

global_defs {
    notification_email {
        544198458@qq.com
    }
    notification_email_from sendmail@jqsoft.com
    smtp_server mail.jqsoft.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script chk_ngnix {
    script "/etc/keepalived/scripts/chk_nginx.sh"
    interval 2
    weight 1
}

vrrp_instance VI_1 {
    state ${state}
    interface ${interface}
    virtual_router_id ${router_id}
    priority 100
    garp_master_delay 30
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_ngnix
    }
    virtual_ipaddress {
      ${vip}  
    }
}
EOF

#启动keepalived
if [ "$OS" == "7" ];then
	systemctl start keepalived
else
        service   keepalived  start
fi

#检测keepalived是否起来
status=`ps -ef |grep "keepalived"|grep -v "grep"`
if [ -z "${status}" ];then
   echo "请检查配置文件是否有问题"
else
  echo "您已经成功安装了keepalived"
fi
echo "您的keepalived安装路径为/etc/keepalived"


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值