ngixn负载+keepalived高可用shell脚本
1、记住负载端口自己修改nginx配置
vim aa.sh
#/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
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"}
expect "password" {send "123456\n;"}
expect eof
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 #在gzip on;下一行追加upstream wg{
sed -i "/upstream wg{/aserver $fu:8080;" /usr/local/nginx/conf/nginx.conf #在upstream wg{下一行追加server 192.168.11.135:8080;
sed -i "/server $fu:8080;/aserver $fu2:8080;" /usr/local/nginx/conf/nginx.conf #在server 192.168.11.135:8080;下一行追加server 192.168.11.135:8081;
sed -i "/server $fu2:8080/a\}" /usr/local/nginx/conf/nginx.conf #在server 192.168.11.135:8081;下一行追加}
sed -i 's/index.htm;$/index.jsp;/' /usr/local/nginx/conf/nginx.conf #把以index.htm;结尾换成index.jsp;
sed -i '/404.html;/alocation ~ \\.jsp$ {' /usr/local/nginx/conf/nginx.conf #在404.html;下一行追加location ~ \.jsp$ {
sed -i '/location ~ \\.jsp$ {/aproxy_pass http://wg;' /usr/local/nginx/conf/nginx.conf #在location ~ \.jsp$ {下一行追加proxy_pass http://wg;
sed -i '/proxy_pass http:\/\/wg;/a\}' /usr/local/nginx/conf/nginx.conf #在proxy_pass http://wg;下一行追加}
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'
sh aa.sh
原文地址:https://blog.csdn.net/a13568hki/article/details/102978716