腳本安裝nging實現高可用加負載

ssh.sh免密腳本

#!/bin/bash
VIP=10.0.0.30
IP_0=10.0.0.41
IP_1=10.0.0.42
PORT1=10.0.0.43:8080
PORT2=10.0.0.43:8081
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xzf nginx-1.16.1.tar.gz
cd nginx-1.16.1
yum -y install gcc gcc-c++ zlib-devel pcre-devel keepalived
./configure && make && make install
c_port=`cat /etc/keepalived/keepalived.conf | grep 'connect_port 80' | wc -l`
if [ $c_port -ne 1 ];then
sed -i '/notification_email {/,/vrrp_garp_interval 0/d' /etc/keepalived/keepalived.conf
sed -i 's/interface eth0/interface ens33/' /etc/keepalived/keepalived.conf
sed -i '/192.168.200.16/d' /etc/keepalived/keepalived.conf
sed -i '/192.168.200.17/d' /etc/keepalived/keepalived.conf
sed -i "s/192.168.200.18/$VIP/" /etc/keepalived/keepalived.conf
sed -i "s/virtual_server 192.168.200.100 443 {/virtual_server $VIP 80 {/" /etc/keepalived/keepalived.conf
sed -i '/persistence_timeout 50/d' /etc/keepalived/keepalived.conf
sed -i "s/real_server 192.168.201.100 443 {/real_server $IP_0 80 {/" /etc/keepalived/keepalived.conf
sed -i '/weight 1/anotify_down /root/nginx.sh' /etc/keepalived/keepalived.conf
sed -i 's/SSL_GET {/TCP_CHECK {/' /etc/keepalived/keepalived.conf
sed -i '/url {/,/connect_timeout 3/d' /etc/keepalived/keepalived.conf
sed -i '/TCP_CHECK {/aconnect_timeout 3' /etc/keepalived/keepalived.conf
sed -i '/delay_before_retry 3/aconnect_port 80' /etc/keepalived/keepalived.conf
sed -i '/virtual_server 10.10.10.2 1358/,$d' /etc/keepalived/keepalived.conf
fi
echo '#!bin/bash
systemctl stop keepalived' > /root/nginx.sh
chmod +x /root/nginx.sh
up_tom=`cat /usr/local/nginx/conf/nginx.conf | grep 'upstream tomcats {' | wc -l`
if [ $up_tom -ne 1 ];then
sed -i "/#gzip  on;/aupstream tomcats {\nserver $PORT1;\nserver $PORT2;\n}" /usr/local/nginx/conf/nginx.conf
sed -i 's/index  index.html index.htm;/index  index.jsp index.html index.htm;/' /usr/local/nginx/conf/nginx.conf
sed -i '/404.html;/alocation ~ \\.jsp$ {\nindex index.jsp;\nproxy_pass http://tomcats;\n}' /usr/local/nginx/conf/nginx.conf
fi
/usr/local/nginx/sbin/nginx
systemctl restart keepalived

chmod +x /root/ssh.sh
sh /root/ssh.sh

scp /root/nginx-1.16.1.tar.gz root@$IP_1:/root
/usr/bin/expect<<EOF
spawn ssh root@$IP_1
expect "~" {send "tar -xzf nginx-1.16.1.tar.gz\n"}
expect "~" {send "cd nginx-1.16.1\n"}
set timeout 1000
expect "nginx-1.16.1" {send "yum -y install gcc gcc-c++ zlib-devel pcre-devel keepalived\n"}
expect "nginx-1.16.1" {send "./configure && make && make install\n"}
expect "nginx-1.16.1" {send "exit\n"}
expect eof
EOF
scp /etc/keepalived/keepalived.conf root@$IP_1:/etc/keepalived/keepalived.conf
ssh root@$IP_1 "sed -i 's/state MASTER/state BACKUP/' /etc/keepalived/keepalived.conf;sed -i 's/priority 100/priority 90/' /etc/keepalived/keepalived.conf"
ssh root@$IP_1 "sed -i 's/real_server $IP_0 80 {/real_server $IP_1 80 {/' /etc/keepalived/keepalived.conf"

scp /usr/local/nginx/conf/nginx.conf root@$IP_1:/usr/local/nginx/conf/nginx.conf
ssh root@$IP_1 "/usr/local/nginx/sbin/nginx;systemctl restart keepalived"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章