Nginx主主負(fù)載均衡架構(gòu)
原創(chuàng)【51CTO.com 獨(dú)家特稿】在和一些朋友交流Nginx+Keepalived技術(shù)時(shí),我雖然已成功多次實(shí)施Nginx+Keepaived項(xiàng)目方案,但這些都是用的單主Nginx在工作,從Nginx長(zhǎng)期只是處于備份狀態(tài),所以我們想將二臺(tái)Nginx負(fù)載均衡器都處于工作狀態(tài),其實(shí)用Nginx+Keepalived也很容易實(shí)現(xiàn)。
此方法適用場(chǎng)景:適合中小型網(wǎng)站應(yīng)用場(chǎng)景。
一般為了維護(hù)方便,企業(yè)網(wǎng)站的服務(wù)器都在自己的內(nèi)部機(jī)房里,只開(kāi)放了Keepalived的VIP地址的兩個(gè)端口80、443,通過(guò)Juniper SSG550防火墻映射出去,外網(wǎng)DNS對(duì)應(yīng)映射后的公網(wǎng)IP。此架構(gòu)的防火墻及網(wǎng)絡(luò)安全說(shuō)明如下:
此系統(tǒng)架構(gòu)僅映射內(nèi)網(wǎng)VIP的80及443端口于外網(wǎng)的Juniper SSG550防火墻下,其他端口均關(guān)閉,內(nèi)網(wǎng)所有機(jī)器均關(guān)閉iptables防火墻;外網(wǎng)DNS指向即通過(guò)Juniper SSG550映射出來(lái)的外網(wǎng)地址。
Nginx負(fù)載均衡作服務(wù)器遇到的故障一般有:服務(wù)器網(wǎng)線松動(dòng)等網(wǎng)絡(luò)故障;服務(wù)器硬件故障發(fā)生損壞現(xiàn)象而crash;
Nginx服務(wù)進(jìn)程死掉(這種情況理論上會(huì)遇到,但事實(shí)上生產(chǎn)環(huán)境下的Linux服務(wù)器沒(méi)有出現(xiàn)過(guò)這種情況,足以證明了Nginx作為負(fù)載均衡器/反向代理服務(wù)器的穩(wěn)定性,我們可以通過(guò)技術(shù)手段來(lái)解決這一問(wèn)題)。
測(cè)試實(shí)驗(yàn)環(huán)境:
主Nginx之一:192.168.1.5
主Nginx之二:192.168.1.6
Web服務(wù)器一:192.168.1.17
Web服務(wù)器二:192.168.1.18
VIP地址一:192.168.1.8
VIP 地址二:192.168.1.9
一、 Nginx和Keepalived的安裝比較簡(jiǎn)單,我這里就不重復(fù)了,大家可以參考我的專題系列的文章,如下地址http://network.51cto.com/art/201007/209823.htm,我這里附上Nginx.conf配置文件,如下所示:
- user www www;
- worker_processes 8;
- pid /usr/local/nginx/logs/nginx.pid;
- worker_rlimit_nofile 51200;
- events
- {
- use epoll;
- worker_connections 51200;
- }
- http{
- include mime.types;
- default_type application/octet-stream;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- upstream backend
- {
- ip_hash;
- server 192.168.1.17:80;
- server 192.168.1.18:80;
- }
- server {
- listen 80;
- server_name www.1paituan.com;
- location / {
- root /var/www/html ;
- index index.php index.htm index.html;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://backend;
- }
- location /nginx {
- access_log off;
- auth_basic "NginxStatus";
- #auth_basic_user_file /usr/local/nginx/htpasswd;
- }
- log_format access '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- access_log /data/logs/access.log access;
- }
- }
#p#
二、 配置Keepalived文件。
我這里簡(jiǎn)單說(shuō)下原理,其實(shí)也就是通過(guò)Keepalived生成二個(gè)實(shí)例,二臺(tái)Nginx互為備份,即***臺(tái)是第二臺(tái)機(jī)器的備機(jī),而第二臺(tái)機(jī)器也是***臺(tái)的備機(jī),而生成的二個(gè)VIP地址分別對(duì)應(yīng)我們網(wǎng)站http://www.1paituan.com,這樣大家在公網(wǎng)上可以通過(guò)DNS輪詢來(lái)訪問(wèn)得到我們的網(wǎng)站,任何一臺(tái)Nginx機(jī)器如果發(fā)生硬件損壞,Keepalived會(huì)自動(dòng)將它的VIP地址切換到另一臺(tái)機(jī)器,不影響客戶端的訪問(wèn),這個(gè)跟我們以前的LVS+Keepalived多實(shí)例的原理是一樣的,相信大家也能明白。
主Nginx機(jī)器之一的Keepalived.conf配置文件如下:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- yuhongchun027@163.com
- }
- notification_email_from keepalived@chtopnet.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_instance VI_1 {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1paituan.com
- }
- virtual_ipaddress {
- 192.168.1.8
- }
- }
- vrrp_instance VI_2 {
- state BACKUP
- interface eth0
- virtual_router_id 52
- priority 99
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1paituan.com
- }
- virtual_ipaddress {
- 192.168.1.9
- }
- }
主Nginx之二的keepalivd.conf配置文件如下:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- yuhongchun027@163.com
- }
- notification_email_from keepalived@chtopnet.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- priority 99
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1paituan
- }
- virtual_ipaddress {
- 192.168.1.8
- }
- }
- vrrp_instance VI_2 {
- state MASTER
- interface eth0
- virtual_router_id 52
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1paituan
- }
- virtual_ipaddress {
- 192.168.1.9
- }
- }
#p#
三、大家都知道Keepalived是實(shí)現(xiàn)不了程序級(jí)別的高可用的,所以我們要寫SHELL腳本,來(lái)實(shí)現(xiàn)Nginx的高可用,腳本/root/nginxpid.sh內(nèi)容如下:
- #!/bin/bash
- while :
- do
- nginxpid=`ps -C nginx --no-header | wc -l`
- if [ $nginxpid -eq 0 ];then
- /usr/local/nginx/sbin/nginx
- sleep 5
- nginxpid=`ps -C nginx --no-header | wc -l`
- if [ $nginxpid -eq 0 ];then
- /etc/init.d/keepalived stop
- fi
- fi
- sleep 5
- done
我們分別在二臺(tái)主Nginx上執(zhí)行,命令如下所示:
- nohup sh /root/nginxpid.sh &
此腳本我是直接從生產(chǎn)服務(wù)器上下載的,大家不要懷疑它會(huì)引起死循環(huán)和有效性的問(wèn)題,我稍為解釋一下,這是一個(gè)無(wú)限循環(huán)的腳本,放在主Nginx機(jī)器上(因?yàn)槟壳爸饕怯伤峁┓?wù)),每隔5秒執(zhí)行一次,用ps -C 命令來(lái)收集nginx的PID值到底是否為0,如果是0的話(即Nginx進(jìn)程死掉了),嘗試啟動(dòng)nginx進(jìn)程;如果繼續(xù)為0,即nginx啟動(dòng)失改, 則關(guān)閉本機(jī)的Keeplaived進(jìn)程,VIP地址則會(huì)由備機(jī)接管,當(dāng)然了,整個(gè)網(wǎng)站就會(huì)由備機(jī)的Nginx來(lái)提供服務(wù)了,這樣保證Nginx進(jìn)程的高可 用。
#p#
四、正常啟動(dòng)二臺(tái)主Nginx的Nginx和Keealived程序后,二臺(tái)機(jī)器的正常IP顯示應(yīng)該如下所示:
這臺(tái)是IP為192.168.1.5的機(jī)器的ip addr命令顯示結(jié)果:
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link/ether 00:0c:29:99:fb:32 brd ff:ff:ff:ff:ff:ff
- inet 192.168.1.5/24 brd 192.168.1.255 scope global eth0
- inet 192.168.1.8/32 scope global eth0
這臺(tái)是IP為192.168.1.6的機(jī)器的ip addr命令顯示結(jié)果:
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link/ether 00:0c:29:7d:58:5e brd ff:ff:ff:ff:ff:ff
- inet 192.168.1.6/24 brd 192.168.1.255 scope global eth0
- inet 192.168.1.9/32 scope global eth0
- inet6 fe80::20c:29ff:fe7d:585e/64 scope link
- valid_lft forever preferred_lft forever
- 3: sit0: <NOARP> mtu 1480 qdisc noop
- link/sit 0.0.0.0 brd 0.0.0.0
五、測(cè)試過(guò)程如下:
一、我們要分別在二臺(tái)主Nginx上用killall殺掉Nginx進(jìn)程,然后在客戶端分別訪問(wèn)192.168.1.8和192.168.1.9這二個(gè)IP(模擬DNS輪詢)看能否正常訪問(wèn)Web服務(wù)器。
二、嘗試重啟192.168.1.5的主Nginx負(fù)載均衡器,測(cè)試過(guò)程如上;
三、嘗試重啟192.168.1.6的主Nginx負(fù)載均衡器,測(cè)試過(guò)程如下;
四、嘗試分別關(guān)閉192.168.1.5和192.168.1.6的機(jī)器,測(cè)試過(guò)程如上,看影響網(wǎng)站的正常訪問(wèn)不?
六、目前投入生產(chǎn)要解決的問(wèn)題:
一、Cacti和Nagios等監(jiān)控服務(wù)要重新部署,因?yàn)楝F(xiàn)在客戶機(jī)是分別訪問(wèn)二臺(tái)負(fù)載均衡器;
二、日志收集要重新部署,現(xiàn)在訪問(wèn)日志是分布在二臺(tái)負(fù)載均衡器上;
三、要考慮google收錄的問(wèn)題;
四、證書的問(wèn)題,二臺(tái)機(jī)器都需要;
五、其它問(wèn)題暫時(shí)沒(méi)有想到,待補(bǔ)充。
【51CTO.com獨(dú)家特稿,非經(jīng)授權(quán)謝絕轉(zhuǎn)載!合作媒體轉(zhuǎn)載請(qǐng)注明原文出處及出處!】