码迷,mamicode.com
首页 > 其他好文 > 详细

nginx+keepalived的高可用脚本

时间:2020-04-22 16:05:52      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:div   configure   bsp   down   zip   zlib   gre   keepaliv   输入   

#/bin/bash

read -ep "请输入keepalived所在的本地IP:" IP
read -ep "请输入另一台keepalived所在的本地IP:" IP2
read -ep "请输入你创建的keepalived的VIP:" net
read -ep "请输入ngixn负载的IP:" fu
read -ep "请输入ngixn负载的另一条IP:" fu2
read -ep "是否终止(y),默认不终止" yn
if [ $yn == y ];then
 exit
fi
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
chmod +x /etc/keepalived/bb.sh
shu=`cat /etc/keepalived/keepalived.conf|grep real_server|wc -l`
if [ $shu -ne 1 ];then
echo "    real_server $IP 80 {
        weight 1
        notify_down /etc/keepalived/bb.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }  
    }  
}
" >>/etc/keepalived/keepalived.conf
fi
echo ‘#!/bin/bash
systemctl stop keepalived‘>/etc/keepalived/bb.sh
chmod +x /etc/keepalived/bb.sh
yum -y install expect
if [ -f /root/.ssh/id_rsa ]||[ -f id_rsa.pub ];then
  continue
else
/usr/bin/expect <<EOF 
spawn ssh-keygen -t rsa
expect ":" {send "\n;"}
expect ":" {send "\n;"}
expect ":" {send "\n;"}
expect eof
EOF
fi
/usr/bin/expect <<EOF
spawn ssh-copy-id root@$IP2
expect "yes/no" {send "yes\n;exp_continue"}
EOF
ssh root@$IP2 ‘yum -y install keepalived‘
scp /etc/keepalived/keepalived.conf $IP2:/etc/keepalived/keepalived.conf
scp /etc/keepalived/bb.sh $IP2:/etc/keepalived/bb.sh
/usr/bin/expect<<EOF
spawn ssh root@$IP2 
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 "exit\n"}
expect eof
EOF
wget http://nginx.org/download/nginx-1.16.1.tar.gz
yum -y install gcc gcc-c++ zlib-devel pcre-devel
tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1 && ./configure && make && make install
shulian=`cat /usr/local/nginx/conf/nginx.conf|grep ‘server $fu2:8081;‘|wc -l`
if [ $shulian -ne 1 ];then
sed -i  ‘/#gzip  on;/aupstream wg{‘ /usr/local/nginx/conf/nginx.conf
sed -i  "/upstream wg{/aserver $fu:8080;" /usr/local/nginx/conf/nginx.conf
sed -i  "/server $fu:8080;/aserver $fu2:8081;" /usr/local/nginx/conf/nginx.conf
sed -i  "/server $fu2:8081/a\}" /usr/local/nginx/conf/nginx.conf
sed -i  ‘s/index.htm;$/index.jsp;/‘ /usr/local/nginx/conf/nginx.conf
sed -i  ‘/404.html;/alocation ~ \\.jsp$ {‘ /usr/local/nginx/conf/nginx.conf
sed -i  ‘/location ~ \\.jsp$ {/aproxy_pass   http://wg;‘ /usr/local/nginx/conf/nginx.conf
sed -i  ‘/proxy_pass   http:\/\/wg;/a\}‘ /usr/local/nginx/conf/nginx.conf
fi
pkill -15 nginx
/usr/local/nginx/sbin/nginx
systemctl restart keepalived
ssh root@$IP2 ‘wget http://nginx.org/download/nginx-1.16.1.tar.gz;yum -y install gcc gcc-c++ zlib-devel pcre-devel;tar zxf nginx-1.16.1.tar.gz;cd nginx-1.16.1 && ./configure && make && make install‘
scp /usr/local/nginx/conf/nginx.conf $IP2:/usr/local/nginx/conf/nginx.conf
ssh root@$IP2 ‘pkill -15 nginx;/usr/local/nginx/sbin/nginx;systemctl restart keepalived‘

nginx+keepalived的高可用脚本

标签:div   configure   bsp   down   zip   zlib   gre   keepaliv   输入   

原文地址:https://www.cnblogs.com/wanglinyan/p/12752534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!