一、腳本自動部署反向代理、web、nfs
I、部署nginx反向代理三個web服務,調度算法使用加權輪詢:
服務端:192.168.16.254
#!/bin/bash
echo '開始安裝........'
yum install epel-release -y
yum install nginx -y
echo '開始配置...........'
psd = "/etc/nginx/nginx.conf"
aa=1
read -p "輸入服務器組名: " up
sed -i "17a upstream $up {" $psd
sed -i "18a }" /test/1.txt
for i in {IP1,IP2,IP3}
do
read -p "輸入第$aa台服務器IP地址: " i
while :
do
read -p "是否設置加權:[Y/N] " en
if [ "$en" = 'Y' ]
then
while :
do
read -p "輸入加權數,介於1-5之間: " a
if [ "$a" -gt 5 -o "$a" -le 0 ]
then
echo "輸入有誤!!!"
else
break
fi
done
break
elif [ "$en" = 'N' ]
then
a=''
break
else
echo "輸入有誤!!!"
fi
done
if [ -n "$a" ]
then
sed -i "18a server $i weight=$a;" $psd
else
sed -i "18a server $i;" $psd
fi
((aa++))
done
c="proxy_pass http://$up;"
sed -i "51a $c" $psd
echo "配置完成,啟動服務............"
systemctl start nginx
客戶端:
#!/bin/bash
echo '開始安裝........'
yum install epel-release -y
yum install nginx -y
echo "安裝完成,啟動服務............"
systemctl start nginx
然后根據自己在服務端設置好的分組IP,設置靜態IP。
所有web服務使用共享存儲nfs,保證所有web都對其有讀寫權限
服務端:
#!/bin/bash
echo "開始安裝......."
yum install rpcbind -y
yum install nfs-utils -y
echo "安裝完成,開始更改配置文件......"
a=`ifconfig | awk 'NR==2{print $2}'|awk -F. '{print $1"."$2"."$3"."0}'`
echo "正在設置共享的文件夾......."
mkdir /share
chmod +rw /share
echo "共享文件夾:/share"
b="/share $a/24(rw,sync,fsid=0)"
echo $b >> /etc/exports
echo "配置完成,啟動服務......"
systemctl start rpcbind
systemctl start nfs
exportfs
客戶端:
#!/bin/bash
echo "開始安裝......."
yum install rpcbind -y
yum install nfs-utils -y
echo "安裝完成,開始更改配置文件......"
echo "掛載共享文件......."
a=`exportfs |awk '{print $1}'`
mount -t nfs 192.168.16.254:$a /usr/share/nginx/html
echo "配置完成,啟動服務......"
systemctl start rpcbind
systemctl start nfs
exportfs
編寫監控腳本,監控集群內所有服務存活狀態,內存、磁盤剩余率檢測,異常則發送報警郵件
python發送郵件小程序:
#!/usr/bin/python
#-*-coding:UTF-8-*-
import smtplib
import sys
from email.mime.text import MIMEText
mailto_list=['m18831253190@163.com',] #收件人(列表)
mail_host="smtp.163.com" #使用的郵箱的smtp服務器地址,這里是163的smtp地址
mail_user="wwwpeng_22" #用戶名
mail_postfix="163.com" #郵箱的后綴,網易就是163.com
def send_mail(to_list,sub,content):
me="server"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list) #將收件人列表以‘;’分隔
server = smtplib.SMTP()
server.connect(mail_host) #連接服務器
server.login("wwwpeng_22@163.com","gao123yue") #登錄操作,密碼是授權碼,而不是郵箱登錄密碼
server.sendmail(me, to_list, msg.as_string())
server.close()
send_mail(mailto_list,"服務器","服務器有異常!!\n%s\n%s"%('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])))
print("\n郵件發送成功!!!!!")
將上述文件內容拷貝到/usr/bin/mail並chmod+x /usr/bin/mail
監測內存小程序:server.sh
#!/bin/bash
mem_usef=80 #內存使用超過80%則報警
disk='/dev/sda1' #需要監控的磁盤名
disk_space_used=80 #磁盤空間使用超過80%則報警
function monitor_mem(){
mem_total=`free |awk 'NR==2{print $2}'`
mem_use=`free |awk 'NR==2{print $3}'`
mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2`
if [ $mem_per -gt $mem_limit ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:Memory usage exceeds the limit,current value is ${mem_per}%"
/usr/bin/mail $msg
fi
}
function monitor_disk_space(){
space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
if [ $space_use -gt $disk_space_limit ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
/usr/bin/mail $msg
fi
}
fuction server_a(){
server_use=`ps aux |grep nginx | grep -v 'grep'`
server_use1=`ps aux |grep nfs | grep -v 'grep'`
if [ -Z $server_use ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:Disk The nginx.server is stop!"
/usr/bin/mail $msg
fi
if [ -Z $server_use1 ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:Disk The nfs.server is stop!"
/usr/bin/mail $msg
fi
}
monitor_mem &>> /tmp/monitor.log
monitor_disk_space &>> /tmp/monitor.log
server_a & >> /tmp/monitor.log
下面簡單測試下:內存和硬盤標准都設置成0.關閉nginx服務。可以立即發送郵件。
設置計划任務
在命令行中輸入crontab -e,添加計划任務並保存退出:
* * * * * /test/serve.sh #每天每時每分鍾檢測一下內存、硬盤使用情況,和服務的運行狀態。