nginx+keepalived 高可用負載平衡

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

1.系統內容

[plain] view plaincopy

  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 plaincopy

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


內容如下:

[plain] view plaincopy

  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伺服器,利用它的反向 Proxy功能提供負載平衡支援
  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 plaincopy

  1. vi /etc/keepalived/keepalived.conf


內容如下:

[plain] view plaincopy

  1. global_defs {
  2. notification_email {
  3. root@localhost.localdomain
  4. }
  5. notification_email_from notify@keepalived.com
  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 plaincopy

  1. vi /etc/keepalived/check_nginx.sh


內容如下:

[plain] view plaincopy

  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 plaincopy

  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 plaincopy

  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 plaincopy

  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

以上就介紹了nginx+keepalived 高可用負載平衡,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.