上一章節博主爲你們介紹了目前大型互聯網項目的系統架構體系,相信你們應該注意到其中很重要的一塊知識nginx技術,在本節博主將爲你們分享nginx的相關技術以及配置過程。css
1、nginx相關概念html
(1.1)反向代理linux
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個服務器。nginx
(1.2)負載均衡shell
負載均衡,英文名稱爲Load Balance,是指創建在現有網絡結構之上,並提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增長吞吐量、增強網絡數據處理能力、提升網絡的靈活性和可用性。其原理就是數據流量分攤到多個服務器上執行,減輕每臺服務器的壓力,多臺服務器共同完成工做任務,從而提升了數據的吞吐量。centos
2、安裝nginx瀏覽器
(2.1)下載nginxtomcat
官網:http://nginx.org/bash
(2.2)上傳並解壓nginx,tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/src服務器
(2.3)進入到nginx源碼目錄:cd /usr/local/src/nginx-1.14.0/
(2.4)檢查安裝環境,並指定未來要安裝的路徑: ./configure --prefix=/usr/local/nginx
缺包報錯 ./configure: error: C compiler cc is not found
(2.5)因爲上一步驟提示缺乏C編譯器,先安裝c編譯器;yum -y install gcc
(2.6)繼續2.4步驟中的檢查安裝環境./configure --prefix=/usr/local/nginx
缺包報錯./configure: error: the HTTP rewrite module requires the PCRE library.
(2.7)因爲上一步驟提示缺乏PCRE library,先安裝RCRE library;
yum list|grep pcre yum -y install pcre-devel
(2.7)繼續2.4步驟中的檢查安裝環境./configure --prefix=/usr/local/nginx
缺包報錯./configure: error: the HTTP gzip module requires the zlib library.
(2.8)因爲上一步驟提示缺乏zlib library,先安裝zlib library;
yum list|grep zlib yum -y install zlib-devel
(2.9)繼續2.4步驟中的檢查安裝環境./configure --prefix=/usr/local/nginx,成功
(2.10)執行安裝nginx命令:make && make install
(2.11)安裝完成後,測試檢查nginx是否正常
./usr/loca/nginx/sbin/nginx
(2.12)查看端口是否有ngnix進程監聽
netstat -ntlp | grep 80
(2.13)使用瀏覽器訪問,發現打不開nginx默認頁面
(2.14)檢查linux防火牆發現只接受端口22的數據包
(2.15)添加容許端口80的數據訪問,執行:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT iptables -I OUTPUT -p tcp --sport 80 -j ACCEPT
(2.16)瀏覽器訪問,nginx已經能夠正常訪問
(2.17)配置nginx反向代理
(2.18)啓動tomcat,並重啓nginx,瀏覽器訪問;./sbin/nginx
(2.19)nginx補充配置
#動態資源 index.jsp location ~ .*\.(jsp|do|action)$ { proxy_pass http://centos-aaron-04:8080; } #靜態資源 location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ { expires 3d; }
(2.20)nginx負載均衡配置
在http這個節下面配置一個叫upstream的,後面的名字能夠隨意取,可是要和location下的proxy_pass http://後的保持一致。 http { #是在http裏面的, 已有http, 不是在server裏,在server外面 upstream tomcats { server 192.168.29.132:8080 weight=1;#weight表示多少個 server 192.168.29.133:8080 weight=1; server 192.168.29.134:8080 weight=1; } #卸載server裏 location ~ .*\.(jsp|do|action) { proxy_pass http://tomcats; #tomcats是後面的tomcat服務器組的邏輯組號 } }
(2.21)從新加載nginx配置文件./sbin/nginx -s reload;並瀏覽器訪問服務
3、利用keepalived實現高可靠(HA)
(3.1)高可用概念
HA(High Available), 高可用性集羣,是保證業務連續性的有效解決方案,通常有兩個或兩個以上的節點,且分爲活動節點及備用節點。
(3.2)高可靠軟件keepalived
keepalive是一款能夠實現高可靠的軟件,一般部署在2臺服務器上,分爲一主一備。Keepalived能夠對本機上的進程進行檢測,一旦Master檢測出某個進程出現問題,將本身切換成Backup狀態,而後通知另一個節點切換成Master狀態。
(3.3)keepalived安裝,下載keepalived,官網地址:http://www.keepalived.org/download.html
(3.4)上傳keepalived壓縮包到服務器
(3.5)將keepalived解壓到/usr/local/src目錄下:tar -zxvf keepalived-1.4.5.tar.gz -C /usr/local/src
(3.6)進入到/usr/local/src/keepalived-1.4.5/目錄:cd /usr/local/src/keepalived-1.4.5/
(3.7)開始configure編譯檢查:./configure --prefix=/usr/local/keepalived
缺包報錯configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
(3.8)因爲上一步驟提示缺乏OpenSSL包,先安裝OpenSSL
yum list|grep openssl yum install -y openssl openssl-devel
(3.9)繼續執行3.7步驟中的編譯檢查:./configure --prefix=/usr/local/keepalived,編譯檢查經過
(3.10)編譯並安裝:make && make install
安裝過程當中有不少警告信息:分析後是由於沒安裝lvs引發.
(3.11)安裝LVS
yum install ipvsadm
(3.12)將keepalived添加到系統服務中
拷貝執行文件 cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 將init.d文件拷貝到etc下,加入開機啓動項 cp /usr/local/src/keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived 將keepalived文件拷貝到etc下 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 建立keepalived文件夾 mkdir -p /etc/keepalived 將keepalived配置文件拷貝到etc下 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf 添加可執行權限 chmod +x /etc/init.d/keepalived ##以上全部命令一次性執行: cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/src/keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir -p /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf chmod +x /etc/init.d/keepalived chkconfig --add keepalived chkconfig keepalived on 添加keepalived到開機啓動 chkconfig --add keepalived chkconfig keepalived on
(3.13)配置keepalived虛擬IP;修改配置文件: /etc/keepalived/keepalived.conf
#MASTER節點 global_defs { } vrrp_instance VI_1 { state MASTER #指定A節點爲主節點 備用節點上設置爲BACKUP便可 interface eth0 #綁定虛擬IP的網絡接口 virtual_router_id 51 #VRRP組名,兩個節點的設置必須同樣,以指明各個節點屬於同一VRRP組 priority 100 #主節點的優先級(1-254之間),備用節點必須比主節點優先級低 advert_int 1 #組播信息發送間隔,兩個節點設置必須同樣 authentication { #設置驗證信息,兩個節點必須一致 auth_type PASS auth_pass 1111 } virtual_ipaddress { #指定虛擬IP, 兩個節點設置必須同樣 192.168.29.190/24 #若是兩個nginx的ip分別是192.168.29.135,,...139,則此處的虛擬ip跟它倆同一個網段便可 } } #BACKUP節點 global_defs { } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.29.190/24 } }
(3.14)分別啓動兩臺機器上的keepalived;命令:service keepalived start
(3.15)測試:
殺掉master上的keepalived進程,你會發現,在slave機器上的eth0網卡多了一個ip地址
查看ip地址的命令: ip addr
(3.16).配置keepalived心跳檢查
原理:Keepalived並不跟nginx耦合,它倆徹底不相關的兩個軟件可是keepalived提供一個機制:讓用戶自定義一個shell腳本去檢測用戶本身的程序,返回狀態給keepalived就能夠了.
#MASTER節點 global_defs { } vrrp_script chk_health { script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1" interval 1 #每隔1秒執行上述的腳本,去檢查用戶的程序ngnix weight -2 } vrrp_instance VI_1 { state MASTER #指定A節點爲主節點 備用節點上設置爲BACKUP便可 interface eth0 #綁定虛擬IP的網絡接口 virtual_router_id 51 #VRRP組名,兩個節點的設置必須同樣,以指明各個節點屬於同一VRRP組 priority 100 #主節點的優先級(1-254之間),備用節點必須比主節點優先級低 advert_int 1 #組播信息發送間隔,兩個節點設置必須同樣 authentication { #設置驗證信息,兩個節點必須一致 auth_type PASS auth_pass 1111 } track_script { chk_health } virtual_ipaddress { #指定虛擬IP, 兩個節點設置必須同樣 192.168.29.190/24 #若是兩個nginx的ip分別是192.168.29.135,,...139,則此處的虛擬ip跟它倆同一個網段便可 } notify_master "/usr/local/keepalived/sbin/notify.sh master" notify_backup "/usr/local/keepalived/sbin/notify.sh backup" notify_fault "/usr/local/keepalived/sbin/notify.sh fault" } #BACKUP節點 global_defs { } vrrp_script chk_health { script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1" interval 1 #每隔1秒執行上述的腳本,去檢查用戶的程序ngnix weight -2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_health } virtual_ipaddress { 192.168.29.190/24 } notify_master "/usr/local/keepalived/sbin/notify.sh master" notify_backup "/usr/local/keepalived/sbin/notify.sh backup" notify_fault "/usr/local/keepalived/sbin/notify.sh fault" }
(3.17)添加切換通知腳本:vi /usr/local/keepalived/sbin/notify.sh
#!/bin/bash case "$1" in master) /usr/local/nginx/sbin/nginx exit 0 ;; backup) /usr/local/nginx/sbin/nginx -s stop /usr/local/nginx/sbin/nginx exit 0 ;; fault) /usr/local/nginx/sbin/nginx -s stop exit 0 ;; *) echo 'Usage: notify.sh {master|backup|fault}' exit 1 ;; esac
(3.18)添加執行權限:chmod +x /usr/local/keepalived/sbin/notify.sh
(3.19)啓動兩臺nginx、tomcat、keepalived測試
在mater活着的時候,瀏覽器訪問一次,master上nginx日誌多一條訪問記錄;
關閉master後,瀏覽器訪問一次,backup上的nginx多一條訪問日誌
以上是nginx安裝搭建以及keeperalived配置主備nginx服務器的全過程;總結:能夠將nginx設置爲開機啓動,這樣通知腳本可使用nginx服務來啓動。最後,若是你們對nginx高可用負載均衡或其餘技術感興趣,請點贊博主,博主隨時歡迎交流。
參考文檔:
http://nginx.org/en/docs/
http://www.keepalived.org/doc/