1.編寫腳本自動部署反向代理、web、nfs;html
要求:
I、部署nginx反向代理三個web服務,調度算法使用加權輪詢;python
1 #!/bin/bash 2 systemctl disable firewalld #關閉firewalld自啓動 3 systemctl stop firewalld 關閉firewalld 4 yum -y install nginx #二進制安裝nginx 5 egrep -v "^$|^#|#" /etc/nginx/nginx.conf #把空行,#開頭的行,帶#號開頭的行去掉 6 mkdir -p /data/www/html 7 echo "welcome oldboy education" > /data/www/html/index.html 8 /usr/sbin/nginx -t #驗證nginx配置文件是否正確 9 systemctl start nginx #啓動nginx 10 yum install rpcbind nfs-utils -y #二進制安裝nfs及相關依賴包 11 mkdir /share 12 echo "/share 192.168.152.0/24(rw,sync,fsid=0)" > /etc/exports 13 echo "bogon 192.168.186.134">> /etc/hosts 14 echo "python-web1 192.168.186.135">> /etc/hosts 15 echo "python-web2 192.168.186.136">> /etc/hosts 16 echo "python-web3 192.168.186.133">> /etc/hosts 17 systemctl enable rpcbind.service 18 systemctl enable nfs-server.service 19 systemctl start rpcbind.service 20 systemctl start nfs-server.service 21 rpcinfo 22 exportfs 23 showmount -e 24 mount -t nfs 192.168.186.135:/share /data/www/html/ 25 mount -t nfs 192.168.186.136:/share /data/www/html/ 26 mount -t nfs 192.168.186.133:/share /data/www/html/ 27 showmount -a 28 chmod -R o+w /share/ #給其餘用戶添加可寫權限 29 echo "welcome oldboy education" > /data/www/html/test.html 30 31 32 touch /etc/nginx/conf.d/proxy.conf 33 cat > /etc/nginx/conf.d/proxy.conf <<EOF 34 upstream web { 35 server 192.168.186.135; 36 server 192.168.186.136; 37 server 192.168.186.133 38 } 39 40 41 msg='upstream { }' 42 sed -ri "/^http/a $msg" /etc/nginx/nginx.conf #增長upstream 43 sed -ri "/^ *location \/ \{$/a proxy_pass http://my_upstream\;" /etc/nginx/nginx.conf 44 II、全部web服務使用共享存儲nfs,保證全部web都對其有讀寫權限,保證數據一致性; 45 46 #!/bin/bash 47 ngxstatus = `ps -ef | grep nginx | grep -v grep | grep -c nginx` 48 function ngxProxyInstall() { 49 if [ -e /usr/sbin/nginx ]; then 50 echo "nginx is already installed" 51 exit 110 52 else 53 systemctl disable firewalld #關閉firewalld自啓動 54 systemctl stop firewalld #關閉firewalld 55 yum -y install nginx -q #二進制安裝nginx 56 /bin/cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak #備份Nginx配置文件 57 egrep -v "^$|^#|#" /etc/nginx/nginx.conf #把空行,‘#’開頭的行,帶‘#’號開頭的行去掉 58 mkdir -p /data/www/html 59 echo "install nginx successful" 60 fi 61 if (-e /etc/nginx/nginx.conf) then 62 sed -ri '/^http/a\\t upstream bing { \n\t server 192.168.186.135 weight=3;\n\t server 192.168.186.136;\n\t server 192.168.186.133;\n\t }' /etc/nginx/nginx.conf 63 sed -ri '/^ *location \/ \{/a\\t\t proxy_pass http://luchuangao;' /etc/nginx/nginx.conf 64 echo "Configuration successful!" 65 systemctl start nginx 66 } 67 68 #nfs安裝 69 nfsstatus=`ps -ef | grep nfs | grep -v grep | grep -c nfs` 70 function nfsInstall() { 71 if [ -e /usr/sbin/rpcinfo ];then 72 echo "nfs already installed" 73 exit 111 74 else 75 yum install rpcbind nfs-utils -y -q 76 echo "echo install nginx successful!" 77 fi 78 79 if [ ! -d /share ];then 80 mkdir -p /share 81 chmod -R o+w /share 82 fi 83 echo "bogon 192.168.186.134">> /etc/hosts 84 echo "python-web1 192.168.186.135">> /etc/hosts 85 echo "python-web2 192.168.186.136">> /etc/hosts 86 echo "python-web3 192.168.186.133">> /etc/hosts 87 echo "/share 192.168.168.134/24(rw,sync,fsid=0)">> /etc/exports 88 89 90 chmod -R o+w /share/ 91 systemctl enable rpcbind.service 92 systemctl enable nfs-server.service 93 systemctl start rpcbind.service 94 systemctl start nfs-server.service 95 96 rpcinfo 97 98 showmount -e 99 mkdir -p /data/www/html/ 100 mount -t nfs 192.168.186.135:/share /data/www/html/ 101 mount -t nfs 192.168.186.136:/share /data/www/html/ 102 mount -t nfs 192.168.186.133:/share /data/www/html/ 103 showmount -a 104 chmod -R o+w /share/ #給其餘用戶添加可寫權限 105 } 106 107 nginx installed 108 nfs installed
2.編寫監控腳本,監控集羣內全部服務存活狀態,內存、磁盤剩餘率檢測,異常則發送報警郵件nginx
監控集羣內全部服務存活狀態,內存、磁盤剩餘率檢測腳本web
1 #!/bin/sh 2 3 function ngxMonitor(){ #監控nginx服務 4 ps aux | grep nginx| grep -v grep &>/dev/null 5 if [ $? -ne 0 ];then 6 msg="TIME:$(date +%F_%T) 7 HOSTNAME:$(hostname) 8 IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}') 9 MSG:Nginx program is crash, Waiting to restart" 10 echo $msg 11 /usr/bin/my_mail $msg 12 systemctl restart nginx 13 fi 14 } 15 16 function nfsMonitor(){ #監控nfs服務 17 ps aux | grep nfs| grep -v grep &>/dev/null 18 if [ $? -ne 0 ];then 19 msg="TIME:$(date +%F_%T) 20 HOSTNAME:$(hostname) 21 IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}') 22 MSG:NFS program is crash, Waiting to restart" 23 echo $msg 24 /usr/bin/my_mail $msg 25 systemctl restart nginx 26 fi 27 } 28 29 function memMonitor(){ #監控內存 30 mem_use=`free | awk 'NR==2{print $3}'` 31 mem_total=`free | awk 'NR==2{print $2}'` 32 mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l |cut -d . -f2` 33 34 if [ ! -e /usr/bin/bc ];then 35 yum install bc -y -q 36 echo "bc install successful" 37 fi 38 if (( $mem_per > 10 )); then 39 msg="TIME:$(date +%F_%T) 40 HOSTNAME:$(hostname) 41 IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}') 42 MSG:Memory usage exceeds the limit,current value is ${mem_per}%" 43 echo $msg 44 /usr/bin/my_mail $msg 45 fi 46 } 47 48 function diskMonitor(){ #監控磁盤 49 space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1` 50 51 if [ $space_use -gt 80 ];then 52 msg="TIME:$(date +%F_%T) 53 HOSTNAME:$(hostname) 54 IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}') 55 MSG:Disk space usage exceeds the limit,current value is ${space_use}%" 56 echo $msg 57 /usr/bin/my_mail $msg 58 fi 59 } 60 61 62 ngxMonitor &>>/tmp/monitor.log 63 nfsMonitor &>>/tmp/monitor.log 64 memMonitor &>>/tmp/monitor.log 65 diskMonitor &>>/tmp/monitor.log
郵件腳本以下:算法
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 import sys 4 import smtplib 5 import email.mime.multipart 6 import email.mime.text 7 8 server = 'smtp.163.com' 9 port = '25' 10 11 def sendmail(server,port,user,pwd,msg): 12 smtp = smtplib.SMTP() 13 smtp.connect(server,port) 14 smtp.login(user, pwd) 15 smtp.sendmail(msg['from'], msg['to'], msg.as_string()) 16 smtp.quit() 17 print('郵件發送成功email has send out !') 18 19 20 if __name__ == '__main__': 21 msg = email.mime.multipart.MIMEMultipart() 22 msg['Subject'] = '你是風兒我是沙,纏纏綿綿回我家' 23 msg['From'] = 'python4_mail@163.com' 24 msg['To'] = 'python4_recvmail@163.com' 25 user = 'python4_mail' 26 pwd = 'sbalex3714' 27 content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式處理,專門針對咱們的郵件格式 28 29 txt = email.mime.text.MIMEText(content, _charset='utf-8') 30 msg.attach(txt) 31 32 sendmail(server,port,user,pwd,msg)
3.編寫計劃任務,定時運行監控腳本,完成監控操做bash
1 [root@bogon ~]# crontab -e 2 */5 * * * * /bin/bash monitor.sh