一、脚本自动部署反向代理、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 #每天每时每分钟检测一下内存、硬盘使用情况,和服务的运行状态。