天天看點

nginx+keepalived 高可用負載均衡

話就不多說了,nginx安裝與配置,還有負載均衡呢,可以看我寫的另一篇文章《nginx負載均衡實戰》,還有關于負載均衡呢,大家可以看一下我寫的另外兩篇文章,一個是《lvs+keepalived負載均衡》,另一個是《haproxy+keepalived負載均衡》,三種負載均衡的差別呢,可以看一下我轉載的一篇文章《軟體級負載均衡器(LVS/HAProxy/Nginx)的特點簡介和對比》,下面直接進入配置步驟:

1.系統環境

[plain]  view plain copy

  1. 系統版本:CentOS release 5.9 (Final) x86 32位      
  2. nginx版本:   1.2.8    
  3. keepalived版本:    1.2.4  
  4. 主keepalived:192.168.207.130  
  5. 從keepalived:192.168.207.131  
  6. VIP:192.168.207.140   
  7. WEB_1:192.168.207.129 80端口   
  8. WEB_2:192.168.207.130 8080端口   
  9. WEB_3:192.168.207.131 8080端口  

2.自定義nginx配置檔案

在192.168.207.130和192.168.207.131上操作

[plain]  view plain copy

  1. useradd nginx  
  2. vi /usr/local/nginx/conf/nginx.conf  

内容如下:

[plain]  view plain copy

  1. #運作使用者      
  2. user nginx nginx;      
  3. #啟動程序      
  4. worker_processes 2;      
  5. #全局錯誤日志及PID檔案      
  6. error_log logs/error.log notice;      
  7. pid logs/nginx.pid;      
  8. #工作模式及每個程序連接配接數上限      
  9. events {      
  10.     use epoll;      
  11.     worker_connections 1024;     #是以nginx支援的總連接配接數就等于worker_processes * worker_connections    
  12. }      
  13. #設定http伺服器,利用它的反向代理功能提供負載均衡支援      
  14. http {      
  15.     #設定mime類型      
  16.     include mime.types;  #這個是說nginx支援哪些多媒體類型,可以到conf/mime.types檢視支援哪些多媒體    
  17.     default_type application/octet-stream;   #預設的資料類型     
  18.     #設定日志格式      
  19.     log_format main '$remote_addr - $remote_user [$time_local] '     
  20.     '"$request" $status $bytes_sent '     
  21.     '"$http_referer" "$http_user_agent" '     
  22.     '"$gzip_ratio"';      
  23.     log_format download '$remote_addr - $remote_user [$time_local] '     
  24.     '"$request" $status $bytes_sent '     
  25.     '"$http_referer" "$http_user_agent" '     
  26.     '"$http_range" "$sent_http_content_range"';      
  27.     #設定請求緩沖      
  28.     client_header_buffer_size 1k;      
  29.     large_client_header_buffers 4 4k;      
  30.     #開啟gzip子產品      
  31.     #gzip on;      
  32.     #gzip_min_length 1100;      
  33.     #gzip_buffers 4 8k;      
  34.     #gzip_types text/plain;      
  35.     #output_buffers 1 32k;      
  36.     #postpone_output 1460;      
  37.     #設定access log      
  38.     access_log logs/access.log main;      
  39.     client_header_timeout 3m;      
  40.     client_body_timeout 3m;      
  41.     send_timeout 3m;      
  42.     sendfile on;      
  43.     tcp_nopush on;      
  44.     tcp_nodelay on;      
  45.     keepalive_timeout 65;      
  46.     #設定負載均衡的伺服器清單      
  47.     upstream mysvr {      
  48.         #weigth參數表示權值,權值越高被配置設定到的幾率越大     
  49.         server 192.168.207.129:80 weight=5;      
  50.         server 192.168.207.130:8080 weight=5;      
  51.         server 192.168.207.131:8080 weight=5;    
  52.     }      
  53.     server { #這個是設定web服務的,監聽8080端口    
  54.         listen        8080;    
  55.         server_name    192.168.207.131;          #這個根據系統ip變化  
  56.         index     index.html index.htm;    
  57.         root        /var/www/html;    
  58.         #error_page     500 502 503 504    /50x.html;    
  59.         #location = /50x.html {    
  60.         #    root     html;    
  61.         #}    
  62.         }     
  63.     #設定虛拟主機      
  64.     server {      
  65.         listen 80;      
  66.         server_name 192.168.207.140;                   #這裡是VIP  
  67.         #charset gb2312;      
  68.         #設定本虛拟主機的通路日志      
  69.         access_log logs/three.web.access.log main;      
  70.         #如果通路 /img/*, /js/*, /css/* 資源,則直接取本地檔案,不通過squid      
  71.         #如果這些檔案較多,不推薦這種方式,因為通過squid的緩存效果更好      
  72.         #location ~ ^/(img|js|css)/{      
  73.         #   root /data3/Html;      
  74.         #   expires 24h;    
  75.         #}     
  76.             #對 "/" 啟用負載均衡      
  77.         location / {      
  78.             proxy_pass http://mysvr;  #以這種格式來使用後端的web伺服器    
  79.             proxy_redirect off;      
  80.             proxy_set_header Host $host;      
  81.             proxy_set_header X-Real-IP $remote_addr;      
  82.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      
  83.             client_max_body_size 10m;      
  84.             client_body_buffer_size 128k;      
  85.             proxy_connect_timeout 90;      
  86.             proxy_send_timeout 90;      
  87.             proxy_read_timeout 90;      
  88.             proxy_buffer_size 4k;      
  89.             proxy_buffers 4 32k;      
  90.             proxy_busy_buffers_size 64k;      
  91.             proxy_temp_file_write_size 64k;    
  92.         }      
  93.         #設定檢視Nginx狀态的位址 ,在安裝時要加上--with-http_stub_status_module參數    
  94.         location /NginxStatus {      
  95.             stub_status on;      
  96.             access_log on;      
  97.             auth_basic "NginxStatus";      
  98.             auth_basic_user_file conf/htpasswd;     #設定通路密碼,htpasswd -bc filename username password    
  99.         }    
  100.     }    
  101. }     

3.自定義keepalived配置檔案

[plain]  view plain copy

  1. vi /etc/keepalived/keepalived.conf  

内容如下:

[plain]  view plain copy

  1. global_defs {  
  2.    notification_email {  
  3.         [email protected]  
  4.    }  
  5.    notification_email_from [email protected]  
  6.    smtp_server 127.0.0.1  
  7.    smtp_connect_timeout 30  
  8.    router_id LVS_DEVEL  
  9. }  
  10. vrrp_script chk_http_port {  
  11.                 script "/etc/keepalived/check_nginx.sh"         ###監控腳本  
  12.                 interval 2                             ###監控時間  
  13.                 weight 2                                ###目前搞不清楚  
  14. }  
  15. vrrp_instance VI_1 {  
  16.         state MASTER                            ### 設定為 主  
  17.         interface eth0                             ### 監控網卡  
  18.         virtual_router_id 51                    ### 這個兩台伺服器必須一樣  
  19.         priority 101                                 ### 權重值 MASTRE 一定要高于 BAUCKUP  
  20.         authentication {  
  21.                      auth_type PASS  
  22.                      auth_pass 1111  
  23.         }  
  24.         track_script {  
  25.                 chk_http_port                     ### 執行監控的服務  
  26.         }  
  27.         virtual_ipaddress {  
  28.              192.168.207.140                            ###    VIP 位址  
  29.         }  
  30. }  

4.寫自定義腳本

[plain]  view plain copy

  1. vi /etc/keepalived/check_nginx.sh  

内容如下: [plain]  view plain copy

  1. !/bin/bash  
  2. A=`ps -C nginx --no-header |wc -l`                ## 檢視是否有 nginx程序 把值賦給變量A  
  3. if [ $A -eq 0 ];then                                         ## 如果沒有程序值得為 零  
  4.         /usr/local/nginx/sbin/nginx  
  5.         sleep 3  
  6.         if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
  7.                   /etc/init.d/keepalived stop                       ## 則結束 keepalived 程序  
  8.         fi  
  9. fi  

這裡是檢查nginx是否啟動好,如果沒有啟動,先啟動 nginx,隔了3秒後還沒有啟動好,則将keepalived程序也關閉,這樣從keepalived就能接手過去了,提供高可用性,在這裡呢,keepalived服務是提供高可用性,而nginx是提供後端web伺服器的負載均衡。

這裡還要給腳本加上執行權限,如下

[plain]  view plain copy

  1. chmod +x /etc/keepalived/check_nginx.sh  

5.啟動服務,并測試

在這裡先說一下啊,在WEB_1上,我是使用系統自帶的apache昨晚web伺服器的,比較省事,這樣呢,我隻要啟動好主從keepalived就ok了,因為它會利用check_nginx.sh腳本來自動啟動nginx。

都啟動好了。

通路http://192.168.207.140就可以輪訓通路後端的三台web伺服器内容啦

這裡我們把主keepalived服務給關掉,來測試高可用性

然後會在從keepalived伺服器上的/var/log/messages看到這樣的日志

[plain]  view plain copy

  1. Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  
  2. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  
  3. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  
  4. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140  
  5. Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added  
  6. Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 added  
  7. Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.  

繼續通路http://192.168.207.140,依舊可以通路後端的三台web伺服器

然後再把原主keepalived打開,可以觀察到原從keepalived伺服器的日志顯示 [plain]  view plain copy

  1. Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140  
  2. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert  
  3. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
  4. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.  
  5. Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed  
  6. Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removed  
  7. Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.  

說明有恢複了原來的主從結果。

生産環境中,後端的機器也可能會挂掉,但是呢,這就不用你操心啦,nginx會自動把session配置設定到好的後端web伺服器上的啦

ok,到這裡全部結束了,實踐親測,祝君成功

From: http://blog.csdn.net/zmj_88888888/article/details/8825471

繼續閱讀