nginx+keepalived的高可用脚本

Posted wanglinyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+keepalived的高可用脚本相关的知识,希望对你有一定的参考价值。

#/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 " ;"}
expect ":" {send " ;"}
expect ":" {send " ;"}
expect eof
EOF
fi
/usr/bin/expect <<EOF
spawn ssh-copy-id root@$IP2
expect "yes/no" {send "yes ;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 "}
expect "~" {send "sed -i ‘s/priority 100/priority 99/‘ /etc/keepalived/keepalived.conf "}
expect "~" {send "sed -i ‘s/MASTER/BACKUP/‘ /etc/keepalived/keepalived.conf "}
expect "~" {send "exit "}
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的高可用脚本的主要内容,如果未能解决你的问题,请参考以下文章

keepalived实现nginx的高可用

keepalived与nginx安装

nginx+keepalived+tomcat实现的高可用

nginx + keepalived 实现高可用性

nginx+keepalived的高可用负载均衡集群构建

Centos7+Nginx+Keepalived实现Apache服务的高可用&负载均衡