Nginx+keepalived high-availability load balancing

Source: Internet
Author: User
Tags reflector haproxy nginx load balancing
Words do not say more, nginx installation and configuration, and load balancing, you can see me write another article, "Nginx load Balancing Combat", as well as about load balancing, we can look at the other two articles I wrote, one is "lvs+keepalived load balance", the other is " haproxy+keepalived load balancer, three kinds of load balancing differences, you can look at my reproduced an article "Software-level load balancer (Lvs/haproxy/nginx) characteristics of the introduction and contrast," the following directly into the configuration steps:

1. System environment

[Plain] View plaincopy

    1. System version: CentOS release 5.9 (Final) x86 32-bit
    2. Nginx Version: 1.2.8
    3. Keepalived version: 1.2.4
    4. Main keepalived:192.168.207.130
    5. From keepalived:192.168.207.131
    6. vip:192.168.207.140
    7. WEB_1:192.168.207.129 80 Port
    8. web_2:192.168.207.130 8080 Port
    9. web_3:192.168.207.131 8080 Port

2. Customizing the Nginx configuration file


Operating on 192.168.207.130 and 192.168.207.131

[Plain] View plaincopy

    1. Useradd Nginx
    2. Vi/usr/local/nginx/conf/nginx.conf


The contents are as follows:

[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服务器, using its reverse proxy function to provide load balancing support
  14. HTTP {
  15. #设定mime类型
  16. Include Mime.types; #这个是说nginx支持哪些多媒体类型, you can go to Conf/mime.types to see which multimedia is supported
  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参数表示权值, the higher the weight, the greater the chance of being assigned.
  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服务的, listening on port 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 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/* Resources, then take the local file directly, not through squid
  71. #如果这些文件较多, this method is not recommended because the cache effect is better through squid
  72. #location ~ ^/(IMG|JS|CSS)/{
  73. # root/data3/html;
  74. # expires 24h;
  75. #}
  76. #对 "/" Enable load balancing
  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状态的地址, add the--with-http_stub_status_module parameter to the installation
  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. Customizing the keepalived configuration file

[Plain] View plaincopy

    1. Vi/etc/keepalived/keepalived.conf


The contents are as follows:

[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 # # # is set primarily
  17. Interface Eth0 # # # Monitor network card
  18. VIRTUAL_ROUTER_ID 51 # # # This two servers must be the same
  19. Priority 101 # # # Weight value Mastre must be higher than bauckup
  20. Authentication {
  21. Auth_type PASS
  22. Auth_pass 1111
  23. }
  24. Track_script {
  25. Chk_http_port # # # services to perform monitoring
  26. }
  27. virtual_ipaddress {
  28. 192.168.207.140 # # # VIP Address
  29. }
  30. }


4. Write a custom script

[Plain] View plaincopy

    1. vi/etc/keepalived/check_nginx.sh


The contents are as follows:

[Plain] View plaincopy

    1. !/bin/bash
    2. A= ' ps-c nginx--no-header |wc-l ' # # See if there are nginx processes assigning values to variable a
    3. If [$A-eq 0];then # # If no process is worth zero
    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 # # ends the keepalived process
    8. Fi
    9. Fi


here is to check if Nginx is started well, if not started, first start Nginx, after 3 seconds after the start is not good, then the keepalived process is also closed, so from keepalived can take over, provide high availability, here, Keepalived services provide high availability, and Nginx provides load balancing for back-end Web servers.
The script is also given execute permissions, as follows

[Plain] View plaincopy

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


5. Start the service and test


Here first say Ah, on the web_1, I use the system comes with the Apache Web server last night, it is easier, so, I just start a good master and slave keepalived OK, because it will use the check_nginx.sh script automatically start Nginx.
It's all up and ready.
Access to http://192.168.207.140 can rotation access to three Web server content on the backend
Here we turn off the main keepalived service to test high availability
The logs are then seen from/var/log/messages on the keepalived server.

[Plain] View plaincopy

    1. APR 17:42:44 localhost keepalived_vrrp:vrrp_instance (vi_1) Transition to MASTER State
    2. APR 17:42:45 localhost keepalived_vrrp:vrrp_instance (vi_1) Entering MASTER State
    3. APR 17:42:45 localhost keepalived_vrrp:vrrp_instance (vi_1) setting protocol VIPs.
    4. APR 17:42:45 localhost keepalived_vrrp:vrrp_instance (vi_1) sending gratuitous ARPs on eth0 for 192.168.207.140
    5. APR 17:42:45 localhost keepalived_vrrp:netlink reflector reports IP 192.168.207.140 added
    6. APR 17:42:45 localhost keepalived_healthcheckers:netlink reflector reports IP 192.168.207.140 added
    7. APR 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.


continue to access http://192.168.207.140 and still have access to three Web servers on the backend
and then the original keepalived open, you can observe the origin from the keepalived server log display

[Plain] View plaincopy

    1. APR 17:42:50 localhost keepalived_vrrp:vrrp_instance (vi_1) sending gratuitous ARPs on eth0 for 192.168.207.140
    2. APR 17:44:06 localhost keepalived_vrrp:vrrp_instance (vi_1) Received higher Prio advert
    3. APR 17:44:06 localhost keepalived_vrrp:vrrp_instance (vi_1) Entering BACKUP State
    4. APR 17:44:06 localhost keepalived_vrrp:vrrp_instance (vi_1) removing protocol VIPs.
    5. APR 17:44:06 localhost keepalived_vrrp:netlink reflector reports IP 192.168.207.140 removed
    6. APR 17:44:06 localhost keepalived_healthcheckers:netlink reflector reports IP 192.168.207.140 removed
    7. APR 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.


indicates that the original master-slave result has been restored.
in the production environment, the back-end machine may also be hung up, but you don't have to worry about it, Nginx will automatically assign the session to a good backend Web server .

OK, here it's all over, practice pro-Test, wish June success

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

The above describes the nginx+keepalived high-availability load balancing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.