nginx+keepalived
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。
1.1. 負載均衡
負載均衡,英文名稱為Load Balance,是指建立在現有網絡結構之上,並提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增加吞吐量、加強網絡數據處理能力、提高網絡的靈活性和可用性。其原理就是數據流量分攤到多個服務器上執行,減輕每臺服務器的壓力,多臺服務器共同完成工作任務,從而提高了數據的吞吐量。
2.1. 下載nginx
官網:http://nginx.org/
2.2. 上傳並解壓nginx
tar -zxvf nginx-1.8.1.tar.gz -C/usr/local/src
2.3. 編譯nginx
#進入到nginx源碼目錄
cd /usr/local/src/nginx-1.8.1
#檢查安裝環境,並指定將來要安裝的路徑
./configure --prefix=/usr/local/nginx
#缺包報錯 ./configure: error: C compiler cc is not found
#使用YUM安裝缺少的包
yum-y install gcc pcre-devel openssl openssl-devel
#編譯安裝
make&& make install
安裝完後測試是否正常:
/usr/loca/nginx/sbin/nginx
查看端口是否有ngnix進程監聽
netstat-ntlp | grep 80
3. 配置nginx
3.1. 配置反向代理
1.修改nginx配置文件
server { listen 80; server_name nginx-01.itcast.cn; #nginx所在服務器的主機名 #反向代理的配置 location / { #攔截所有請求 root html; proxy_pass http://192.168.0.21:8080; #這裏是代理走向的目標服務器:tomcat } } |
2.啟動tomcat-01上的tomcat
3.啟動nginx-01上的nginx
./nginx
重啟:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
參考網址:http://www.cnblogs.com/jianxie/p/3990377.html
3.2. 動靜分離
#動態資源 index.jsp
location ~ .*\.(jsp|do|action)$ { proxy_pass http://tomcat-01.itcast.cn:8080; } |
#靜態資源
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ { expires 3d; } |
3.3. 負載均衡
在http這個節下面配置一個叫upstream的,後面的名字可以隨意取,但是要和location下的proxy_pass http://後的保持一致。
http { 是在http裏面的, 已有http, 不是在server裏,在server外面 upstream tomcats { server shizhan02:8080 weight=1;#weight表示多少個 server shizhan03:8080 weight=1; server shizhan04:8080 weight=1; } #卸載server裏 location ~ .*\.(jsp|do|action) { proxy_pass http://tomcats; #tomcats是後面的tomcat服務器組的邏輯組號 } } |
4. 利用keepalived實現高可靠(HA)
4.1. 高可靠概念
HA(High Available), 高可用性集群,是保證業務連續性的有效解決方案,一般有兩個或兩個以上的節點,且分為活動節點及備用節點。
4.2. 高可靠軟件keepalived
keepalive是一款可以實現高可靠的軟件,通常部署在2臺服務器上,分為一主一備。Keepalived可以對本機上的進程進行檢測,一旦Master檢測出某個進程出現問題,將自己切換成Backup狀態,然後通知另外一個節點切換成Master狀態。
4.3. keepalived安裝
下載keepalived官網:http://keepalived.org
將keepalived解壓到/usr/local/src目錄下
tar -zxvf keepalived-1.2.19.tar.gz -C /usr/local/src
進入到/usr/local/src/keepalived-1.2.19目錄
cd /usr/local/src/keepalived-1.2.19
開始configure
./configure--prefix=/usr/local/keepalived
#編譯並安裝
make && make install
4.4. 將keepalived添加到系統服務中
拷貝執行文件
cp /usr/local/keepalived/sbin/keepalived/usr/sbin/
將init.d文件拷貝到etc下,加入開機啟動項
cp /usr/local/keepalived/etc/rc.d/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/keepalived/etc/rc.d/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
4.5. 配置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.33.60/24 #如果兩個nginx的ip分別是192.168.33.61,,...62,則此處的虛擬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.33.60/24 } } |
#分別啟動兩臺機器上的keepalived
service keepalived start
測試:
殺掉master上的keepalived進程,你會發現,在slave機器上的eth0網卡多了一個ip地址
查看ip地址的命令: ip addr
4.6. 配置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 interface eth0 virtual_router_id 1 priority 100 advert_int 2 authentication { auth_type PASS auth_pass 1111 }
track_script { chk_health }
virtual_ipaddress { 10.0.0.10/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" } |
#添加切換通知腳本
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 |
#添加執行權限
chmod +x/usr/local/keepalived/sbin/notify.sh
global_defs { }
vrrp_script chk_health { script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1" interval 1 weight -2 }
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 1 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 }
track_script { chk_health }
virtual_ipaddress { 10.0.0.10/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" } |
#在第二臺機器上添加notify.sh腳本
#分別在兩臺機器上啟動keepalived
service keepalived start
chkconfig keepalived on
nginx+keepalived