網站叢集架構實戰(LVS負載均衡、Nginx代理快取、Nginx動靜分離、Rsync+Inotify全網備份、Zabbix自動注...
前言
最近做了一個不大不小的專案,現就刪繁就簡單獨拿出來web叢集這一塊寫一篇部落格。資料庫叢集請參考《 ofollow,noindex" target="_blank">SQL/">MySQL叢集架構篇:MHA+MySQL-PROXY+LVS實現MySQL叢集架構高可用/高效能-技術流ken 》下面是專案的一些簡單介紹。
WEB叢集專案簡介
隨著網站訪問量的激增,勢必會導致網站的負載增加,現需求搭載一套高效能,高負載,高可用的網站叢集架構以保障網站的持續、高效、安全、穩定的執行。
針對以上需求,我們採用瞭如下的技術:
- 使用負載均衡技術來實現網站請求的排程分發,減小後端伺服器的壓力。
- 配置了 KEEPALIVED解決單點故障問題。
- 採用動靜分離的技術,客戶端請求會根據請求檔案型別往不同的後端節點進行轉發、排程,均衡每個節點的壓力。
- 資料庫採用讀寫分離及級聯複製的架構,使得資料的寫入和讀取更加的快捷。
- 為了保障資料庫的持續執行及安全,我們部署了 MHA實現資料庫的高可用,可實現即時報警及故障切換。
- 為了保障資料的安全,定時做了資料庫的完全備份以及增量備份。
- 針對網站資料做了實時差異化備份,保證資料的一致性和完整性。
- 分散式監控系統,實時監測各個節點的執行狀況;郵件報警通知機制,做到實時報警通知,快速定位問題,解決問題。
WEB叢集專案環境說明
² 系統版本: CentOS Linux release 7.5.1804 (Core)
² 核心版本: 3.10.0-862.el7.x86_64
² SELinux關閉狀態
² Firewalld關閉狀態
WEB叢集專案圖片示例
WEB叢集專案架構說明
第一部分:WEB叢集
1.當用戶請求經過路由器轉發進網路中,由Keepalived+Lvs組成的一臺伺服器首先接收請求,根據配置好的排程規則轉發至後端伺服器節點。在這裡準備了兩臺伺服器,使用Keepalived解決單點故障問題,實現服務的高可用。
2.由Keepalived+Lvs轉發過來的使用者請求進入到快取代理伺服器當中。這裡使用Nginx做了一個快取伺服器以及代理伺服器,如果快取中有使用者請求的快取資料,就會直接返回給客戶端,如果沒有使用者請求資料的快取就會根據檔案類別進行往後方節點轉發。
3.如果客戶請求是靜態檔案就會被排程到靜態伺服器。靜態伺服器由apache構建,靜態伺服器提供靜態檔案,並由後方的檔案共享伺服器提供檔案。
4.如果客戶請求是動態檔案就會被排程到動態伺服器。動態伺服器由nginx構建,動態伺服器提供動態檔案,這裡做了一個apache,mysql,php分離的架構。
5.把需要部署的網站包放在檔案共享伺服器。檔案共享伺服器部署NFS。
6.對檔案共享伺服器做好備份,防止資料丟失。這裡採用rsync+inotify實現資料的完全備份,保證資料的一致性。
第二部分:監控叢集
1.監控採用zabbix,並使用分散式監控系統,減小主監控節點的壓力。
2.兩個mysql-proxy分別監控web叢集和mysql叢集,實時反應每個節點的狀態資訊,快速定位故障節點進行修復。
3.在zabbix伺服器端一起配置了DNS以及NTP服務,分別提供內部伺服器的網站域名解析服務,以及時間同步服務。
IP地址分配
主機名(角色) |
IP地址 |
keepalived+lvs主 |
10.220.5.131 |
keepalived+lvs備 |
10.220.5.132 |
VIP |
10.220.5.133 |
代理快取1 |
10.220.5.134 |
代理快取2 |
10.220.5.135 |
靜態伺服器 |
10.220.5.137 |
動態伺服器 |
10.220.5.138 |
檔案共享伺服器 |
10.220.5.139 |
備份伺服器 |
10.220.5.140 |
zabbix.ntp.dns |
10.220.5.111 |
WEB叢集架構指令碼
上面的架構我已經寫成了指令碼,可以實現一鍵安裝,指令碼可以複用。先就針對每個指令碼進行講解
[root@ken ~]# ls arp.shfileshare.shinotify7.shkeepalived.confqimojiagou.tar.gzssh1.shzhengti.sh dongtai5.shhuancun3.shkeep2.shnginx.confrsync6.shstatic4.sh
第一步:祕鑰自動批量分發
1.實現指令碼一鍵安裝第一步要做的就是祕鑰的分發。
2.祕鑰自動分發藉助於expect來實現,所以指令碼中第一步是要安裝expect。
3.刪除儲存的祕鑰資訊,可以實現指令碼的複用。
4.使用ssh-keygen來生成祕鑰,藉助於expect實現自動化。
5.使用一個for迴圈的語句,把你需要聯絡的節點的IP地址寫進去,這樣就可以實現免祕鑰登入了。
[root@ken ~]# vim ssh1.sh #!/bin/bash . /etc/init.d/functions #下載expect yum install expect -y &>/dev/null if [ $? -eq 0 ];then echo -n "download expect" success echo "" else echo -n "download expect" failure echo "" exit 8 fi #刪除儲存的祕鑰資訊 if [ -f id_rsa -o -f id_rsa.pub -o known_hosts ];then rm -rf /root/.ssh/id* rm -rf /root/.ssh/known* fi #自動生成祕鑰對 /usr/bin/expect<<eof spawn ssh-keygen expect { "(/root/.ssh/id_rsa)" {send \r;exp_continue} "passphrase" {send \r;exp_continue} "again" {send \r} } expect eof exit eof #在各個節點分發祕鑰 for i in 31 32 34 35 37 38 39 40 do ken=10.220.5.1$i /usr/bin/expect<<eof spawn ssh-copy-id $ken expect { "yes/no" {send yes\r;exp_continue} "password" {send o\r} } expect eof exit eof done
第二步:配置keepalived+lvs
1.在兩個keepalived節點之上下載需要的軟體
2.使用ssh結合命令在備節點執行下載操作
3.把/root/下的keepalived檔案傳到備節點之上
4.使用sed修改keepalived配置檔案
5.做後端節點的arp抑制
[root@ken ~]# vim keep2.sh #!/bin/bash . /etc/init.d/functions bash /root/ssh1.sh yum install nginx keepalived ipvsadm libnl* popt* -y &>/dev/null if [ $? -ne 0 ];then echo -n "download error at master" failure echo "" exit 4 fi #keepalved備節點也下載軟體 ssh 10.220.5.132 yum install keepalived ipvsadm libnl* popt* -y &>/dev/null if [ $? -ne 0 ];then echo -n "download error at slave" failure echo "" exit 2 fi #把準備好的從節點keepalived檔案傳送到備節點並啟動 scp -q /root/keepalived.conf 10.220.5.132:/etc/keepalived &>/dev/null ssh10.220.5.132 systemctl restart keepalived&>/dev/null #準備主keepalived檔案並啟動 cp -f /root/keepalived.conf /etc/keepalived/ sed -i 's/router_id id2/router_id id1/' /etc/keepalived/keepalived.conf &>/dev/null sed -i 's/state SLAVE/state MASTER/'/etc/keepalived/keepalived.conf &>/dev/null sed -i 's/priority 100/priority 150/'/etc/keepalived/keepalived.conf &>/dev/null systemctl restart keepalived&>/dev/null #快取1arp抑制 scp -q /root/arp.sh 10.220.5.134:/root ssh 10.220.5.134 bash /root/arp.sh #快取2arp抑制 scp -q /root/arp.sh 10.220.5.135:/root ssh 10.220.5.135 bash /root/arp.sh systemctl restart nginx ipvsadm -L -n
第三步:配置代理快取節點
1.在後端節點10.220.5.134,10.220.5.135安裝nginx作為快取代理伺服器
2.把準備好的nginx配置檔案傳送到後端節點並進行重啟生效
#!/bin/bash . /etc/init.d/functions #配置134代理快取 ssh 10.220.5.134 yum install nginx -y &>/dev/null if [ $? -ne 0 ];then echo -n "download nginx" failure echo "" exit 5 fi mkdir /k&>/dev/null mkdir /kenken&>/dev/null scp -q /root/nginx.conf 10.220.5.134:/etc/nginx &>/dev/null ssh 10.220.5.134 systemctl restart nginx &>/dev/null #配置135代理快取 ssh 10.220.5.135 yum install nginx -y &>/dev/null if [ $? -ne 0 ];then echo -n "download nginx" failure echo "" exit 5 fi scp -q /root/nginx.conf 10.220.5.135:/etc/nginx&>/dev/null ssh 10.220.5.135 systemctl restart nginx &>/dev/null
第四步:配置靜態伺服器
1.安裝httpd以及nfs元件
2.掛載共享伺服器節點至本機的網站根目錄
3.重啟nginx使配置生效
[root@ken ~]# vim static4.sh #!/bin/bash . /etc/init.d/functions yum install httpd nfs-utils -y &>/dev/null #mkdir -p /var/www/html/static &>/dev/null mount -t nfs 10.220.5.139:/ken /var/www/html &>/dev/null systemctl restart httpd
第五步:配置動態伺服器
1.安裝配置lnmp環境
2.掛載共享伺服器節點至本機的網站根目錄之下
3.重啟apache使配置生效
[root@ken ~]# vim dongtai5.sh #!/bin/bash yum install httpd php php-mysqlnfs-utils -y &>/dev/null #mkdir -p /var/www/html/dongtai &>/dev/null mount -t nfs 10.220.5.139:/ken /var/www/html/ &>/dev/null systemctl restart httpd
第六步:配置rsync伺服器
[root@ken ~]# vim rsync6.sh #!/bin/bash . /etc/init.d/functions ruser=user1 vuser=kenken file=/ken hosts=10.220.5.139/24 yum install rsync -y &>/dev/null useradd -r -u 333 -s /sbin/nologin $ruser &>/dev/null if [ ! -e $file ];then mkdir $file &>/dev/null fi chown -R $ruser $file cat>/etc/rsyncd.conf<<eof pid file=/var/lock/subsys/pidfile lock file=/var/lock/subsys/rsync log file=/var/log/rsync uid=333 gid=333 timeout=100 max connections=199 [ken] path=/ken list=yes use chroot=yes read only=no auth users=kenken secrets file=/etc/rsyncd.pwd hosts allow=10.220.5.139/24 eof echo "$vuser:123" > /etc/rsyncd.pwd chmod 600 /etc/rsyncd.pwd rsync --daemon &>/dev/null if [ $? -eq 0 ];then echo -n "start rsync..." success echo "" else echo -n "start rsync..." failure echo "" fi
第七步:配置inotify
1.在本機安裝inotify和rsync
2.監控/ken下的檔案,如果發生更改就會被推送至rsync伺服器端
[root@ken ~]# vim inotify7.sh #!/bin/bash . /etc/init.d/functions file=/ken vuser=kenken host=10.220.5.140 mname=ken yum install inotify-tools -y &>/dev/null echo "123" >/etc/rsync.pwd chmod 600 /etc/rsync.pwd inotifywait -mrq --format "%w%f" -e create,delete,modify $file | while read line do rsync -rz --delete $file $vuser@$host::$mname --password-file=/etc/rsync.pwd done
第八步:配置nfs伺服器端
1.把本機的/ken目錄共享給10.220.5.137,10.220.5.138伺服器端檔案可以解壓在本目錄下即可
[root@ken ~]# vim fileshare.sh #!/bin/bash . /etc/init.d/functions yum install nfs-utils rpcbind -y &>/dev/null mkdir -p /ken/static &>/dev/null mkdir -p /ken/dongtai &>/dev/null chown -R nfsnobody.nfsnobody /ken cat>/etc/exports<<eof /ken 10.220.5.137/24(rw,async,all_squash) /ken 10.220.5.138/24(rw,async,all_squash) eof systemctl restart rpcbind systemctl restart nfs
至此所有的指令碼已經介紹完畢,下面來看一下nginx的配置檔案。
# For more information on configuration, see: #* Official English Documentation: http://nginx.org/en/docs/ #* Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log/var/log/nginx/access.logmain; sendfileon; tcp_nopushon; tcp_nodelayon; keepalive_timeout65; types_hash_max_size 2048; include/etc/nginx/mime.types; default_typeapplication/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. proxy_cache_path /klevels=1:2 keys_zone=scache:30m;#定義靜態檔案的快取 fastcgi_cache_path /kenken levels=1:2 keys_zone=kenken:30m;#定義動態檔案的快取 server { listen80 default_server; listen[::]:80 default_server; server_name_; root/var/www/html; indexindex.html; # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; location / { proxy_pass http://10.220.5.137;#靜態檔案會被代理到10.220.5.137 proxy_set_header host $host; proxy_set_header realip $remote_addr; proxy_cache scache;#使scache快取 proxy_cache_valid any 10m;#定義快取的型別及時間 } location ~ \.php$ { proxy_pass http://10.220.5.138;#動態的檔案會被快取到10.220.5.138 proxy_set_header host $host; proxy_set_header realip $remote_addr; fastcgi_cache kenken;#使用動態快取 fastcgi_cache_valid any 10m; fastcgi_cache_key http://$host$request_uri;#定義動態快取的key } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
最後來看一下整體的指令碼,即執行的指令碼
1.把寫好的指令碼及準備好的配置檔案都存放在10.220.5.131的/root下
2.使用scp及ssh進行遠端傳送指令碼及執行指令碼
[root@ken ~]# vim zhengti.sh #!/bin/bash . /etc/init.d/functions #執行祕鑰分發 bash /root/ssh1.sh #實現keepalived+LVS bash /root/keep2.sh #實現快取代理 bash /root/huancun3.sh #實現檔案共享 scp -q /root/fileshare.sh 10.220.5.139:/root ssh 10.220.5.139 bash /root/fileshare.sh #實現靜態伺服器 scp -q /root/static4.sh 10.220.5.137:/root ssh 10.220.5.137 bash /root/static4.sh #實現動態伺服器 scp -q /root/dongtai5.sh 10.220.5.138:/root ssh 10.220.5.138 bash /root/dongtai5.sh #實現備份伺服器rsync scp -q /root/rsync6.sh 10.220.5.140:/root ssh 10.220.5.140 bash /root/rsync6.sh #實現共享檔案伺服器inotify scp -q /root/inotify7.sh 10.220.5.139:/root ssh 10.220.5.139 bash /root/inotify7.sh& iptables -F
執行指令碼並測試
只要執行總的指令碼即可,會自動呼叫其他指令碼
[root@ken ~]# bash zhengti.sh
第一步:在10.220.5.137主機建立靜態測試檔案
[root@ken html]# cat index.html test for 137ddd
第二步:在10.220.5.138主機建立動態測試檔案
[root@ken html]# cat index.php <?php phpinfo(); ?>
第三步:輸入10.220.5.133/index.html,即虛擬IP 訪問10.220.5.137主機的動態檔案
第四步:輸入10.220.5.133/index.php,即虛擬IP 訪問10.220.5.138主機的動態檔案
看到這些資訊即表示成功
zabbix自動註冊全網監控
實現全網監控首先需要給每個待監控的主機安裝zabbix的客戶端,可以使用如下指令碼進行安裝\
第一步:執行如下指令碼
給每個待監控的客戶端安裝zabbix-agen客戶端
#!/bin/bash #author:技術流ken #date:2018-11-15 #desc:this script for downloading zabbix-agent for clients ip=10.220.5.1 for i in 31 32 34 35 37 38 39 40 do ssh $ip$i yum install zabbix-agent -y&>/dev/null ssh $ip$i sed -i 's/Server=127.0.0.1/Server=10.220.5.137/' /etc/zabbix/zabbix_agentd.conf &>/dev/null ssh $ip$i systemctl restart zabbix-agent &>/dev/null done
第二步:在zabbix伺服器端開啟自動註冊
點選configuration>actions>選擇右上角的auto redistration
點選creation action
選擇如下動作
點選add儲存
第三步:檢視
點選configure>hosts即可檢視自動註冊到的主機