开发脚本自动部署及监控
1.编写脚本自动部署反向代理、web、nfs;
(1)、部署nginx反向代理三个web服务,调度算法使用加权轮询;
(2)、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
脚本如下
#!/bin/sh Nginx_WebInstall(){ systemctl stop firewalld setenforce 0 ps -aux | grep nginx |grep -v "grep" if [ $? -ne 0 ] then yum install epel-release -y &>/dev/null; yum install nginx -y &>/dev/null; systemctl start nginx; echo "nginx is successful!!" else systemctl restart nginx fi } Conf_Nginx(){ if [ -f "/etc/nginx/nginx.conf" ] then sed -ri '/^http/a upstream xyyweb { ' /etc/nginx/nginx.conf sed -ri '/^upstream/a server 192.168.31.101 weight=3; }' /etc/nginx/nginx.conf sed -ri '/^upstream/a server 192.168.31.164 weight=3;' /etc/nginx/nginx.conf sed -ri '/^upstream/a server 192.168.31.131 weight=3;' /etc/nginx/nginx.conf sed -ri '47a \n location / { proxy_pass http://xyyweb; }' /etc/nginx/nginx.conf elif [ $? -eq 0 ] then systemctl restart nginx else echo "input file false." fi } Nginx_WebInstall ##这个命令只安装web服务器 Conf_Nginx ##这个是进行修改配置文件(两者同时运行时是安装代理服务器) Nfs_install(){ if [ -s "/etc/exports" ] then systemctl stop firewalld setenforce 0 yum -y install rpcbind nfs-utils &>/dev/null; echo "rpcbind and nfs is successful!!" elif [ $? -eq 0 ] then systemctl enable nfs-server.service; systemctl enable rpcbind.service; systemctl start rpcbind; systemctl start nfs; Rpc=`rpcinfo -p localhost|wc -l;` ps -ef|grep nfsd elif [ $Rpc -eq 7 -o $? -eq 0] then echo "rpcbind and nfs is start success!!" fi mkdir /share; echo "/share 192.168.31.0/24 (rw,sync,fsid=0)" >/etc/exports chmod 777 /share } Nfs_install ##安装nfs的命令;
web节点服务器挂载nfs是 只需在上面执行mount -t nfs ipaddress:/share /web-serverdir
安装nginx:
修改配置文件:
安装nfs:
2、编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
#!/bin/sh NgxMonitor(){ ps -ef | grep nginx| grep -v grep if [ $? -ne 0 ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:Nginx program is crash, Waiting to restart" echo $msg /usr/bin/my_mail $msg systemctl restart nginx fi } NfsMonitor(){ ps -ef | grep nfs| grep -v grep if [ $? -ne 0 ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:NFS program is crash, Waiting to restart" echo $msg /usr/bin/my_mail $msg systemctl restart nginx fi } MemMonitor(){ mem_use=`free | awk 'NR==2{print $3}'` mem_total=`free | awk 'NR==2{print $2}'` mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l |cut -d . -f2` if (( $mem_per > 10 )) 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}%" echo $msg /usr/bin/my_mail $msg fi } DiskMonitor(){ space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1` if [ $space_use -gt 80 ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}') MSG:Disk space usage exceeds the limit,current value is ${space_use}%" echo $msg /usr/bin/my_mail $msg fi } NgxMonitor NfsMonitor MemMonitor DiskMonitor
3.编写计划任务,定时运行监控脚本,完成监控操作