2020-10-26
linux
00
请注意,本文编写于 1310 天前,最后修改于 1310 天前,其中某些信息可能已经过时。

目录

1. 主脚本
2. 安装主服务器脚本
3. 安装备用服务器脚本
4. 操作

1. 主脚本

bash
# vim install_keepalive.sh #!/bin/bash echo "该操作会清除现在已用的keepalived服务,请谨慎使用。 相关操作: 1. /opt/keepalived开头的文件 2. /usr/local/keepalived开头的文件 3. 备份/etc/keepalived/keepalived.conf为/etc/keepalived/keepalived.confbak " read -p "请输入模型(1|2): 1): 单主模型 2): 主备模型 " ARCH if [ $ARCH -eq 1 ];then read -p "请输入需要安装keepalived的版本:" VERSION read -p "请输入绑定的网卡:" CARD_NAME read -p "请输入VIP:" VIP read -p "请输入MASTE的ip地址:" MASTE_IP #清理文件 rm -rf /opt/keepalived* rm -rf /usr/local/keepalived* mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.confbak ssh-copy-id root@$MASTE_IP scp /root/install_keepalive_master.sh root@$MASTE_IP:/root ssh root@$MASTE_IP "bash /root/install_keepalive_master.sh $VERSION MASTER $VIP $CARD_NAME" fi if [ $ARCH -eq 2 ];then read -p "请输入需要安装keepalived的版本:" VERSION read -p "请输入绑定的网卡:" CARD_NAME read -p "请输入VIP:" VIP read -p "请输入MASTE的ip地址:" MASTE_IP read -p "请输入BACKUP的ip地址:" BACKUP_IP ssh-copy-id root@$MASTE_IP ssh-copy-id root@$BACKUP_IP # 主 scp /root/install_keepalive_master.sh root@$MASTE_IP:/root ssh root@$MASTE_IP "rm -rf /opt/keepalived* && rm -rf /usr/local/keepalived* && mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.confbak" ssh root@$MASTE_IP "bash /root/install_keepalive_master.sh $VERSION MASTER $VIP $CARD_NAME" #备 scp /root/install_keepalive_backup.sh root@$BACKUP_IP:/root ssh root@$BACKUP_IP "rm -rf /opt/keepalived* && rm -rf /usr/local/keepalived* && mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.confbak" ssh root@$BACKUP_IP "bash /root/install_keepalive_backup.sh $VERSION BACKUP $VIP $CARD_NAME" fi

2. 安装主服务器脚本

bash
# vim install_keepalive_master.sh #!/bin/bash VERSION=$1 ROLE=$2 VIP=$3 CARD_NAME=$4 cat>/etc/keepalived/keepalived.conf<<EOF ! Configuration File for keepalived global_defs { notification_email { acassen } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state $ROLE interface $CARD_NAME garp_master_delay 10 smtp_alert virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { $VIP dev $CARD_NAME label $CARD_NAME:1 } } EOF apt-get install curl gcc make libssl-dev libnl-3-dev libnl-genl-3-dev libsnmp-dev libnl-route-3-dev -y cd /opt/ curl --progress https://keepalived.org/software/keepalived-$VERSION.tar.gz | tar xz cd keepalived-$VERSION ./configure --prefix=/usr/local/keepalived-$VERSION make && make install ln -s /usr/local/keepalived-$VERSION /usr/local/keepalived /usr/local/keepalived/sbin/keepalived

3. 安装备用服务器脚本

bash
# vim install_keepalive_backup.sh #!/bin/bash VERSION=$1 ROLE=$2 VIP=$3 CARD_NAME=$4 cat>/etc/keepalived/keepalived.conf<<EOF ! Configuration File for keepalived global_defs { notification_email { acassen } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state $ROLE interface $CARD_NAME garp_master_delay 10 smtp_alert virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { $VIP dev $CARD_NAME label $CARD_NAME:1 } } EOF apt-get install curl gcc make libssl-dev libnl-3-dev libnl-genl-3-dev libsnmp-dev libnl-route-3-dev -y cd /opt/ curl --progress https://keepalived.org/software/keepalived-$VERSION.tar.gz | tar xz cd keepalived-$VERSION ./configure --prefix=/usr/local/keepalived-$VERSION make && make install ln -s /usr/local/keepalived-$VERSION /usr/local/keepalived /usr/local/keepalived/sbin/keepalived

4. 操作

演示视频

本文作者:mykernel

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!