Nginx + keepalived High-Availability Load Balancing

Source: Internet
Author: User
Tags reflector haproxy
: This article mainly introduces nginx + keepalived high-availability server load balancer. if you are interested in the PHP Tutorial, refer to it. Let's just talk about nginx installation and configuration, as well as server load balancer. you can refer to another article I wrote, "nginx server load balancer practice", and what about server load balancer, let's take a look at the two other articles I have written: lvs + keepalived server load balancer and haproxy + keepalived server load balancer. What are the differences between the three types of server load balancer, let's take a look at the reposted article "Introduction and Comparison of software-level load balancer (LVS/HAProxy/Nginx) features". the configuration steps are as follows:

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. Master 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 port 80
  8. WEB_2: 192.168.207.130 port 8080
  9. WEB_3: 192.168.207.131 port 8080

2. customize the nginx configuration file


Operate 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 content is as follows:

[Plain] view plaincopy

  1. # Running user
  2. User nginx;
  3. # Start a process
  4. Worker_processes 2;
  5. # Global error logs and PID files
  6. Error_log logs/error. log notice;
  7. Pid logs/nginx. pid;
  8. # Working mode and maximum number of connections per process
  9. Events {
  10. Use epoll;
  11. Worker_connections 1024; # The total number of connections supported by nginx is equal to worker_processes * worker_connections.
  12. }
  13. # Set the http server and use its reverse proxy function to provide load balancing support
  14. Http {
  15. # Set the mime type
  16. Include mime. types; # This indicates the multimedia types supported by nginx. you can view the supported multimedia types in conf/mime. types.
  17. Default_type application/octet-stream; # default data type
  18. # Set the log format
  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. # Set request buffer
  28. Client_header_buffer_size 1 k;
  29. Large_client_header_buffers 4 4 k;
  30. # Enable the gzip module
  31. # Gzip on;
  32. # Gzip_min_length 1100;
  33. # Gzip_buffers 4 8 k;
  34. # Gzip_types text/plain;
  35. # Output_buffers 1 32 k;
  36. # Postpone_output 1460;
  37. # Setting access log
  38. Access_log logs/access. log main;
  39. Client_header_timeout 3 m;
  40. Client_body_timeout 3 m;
  41. Send_timeout 3 m;
  42. Sendfile on;
  43. Tcp_nopush on;
  44. Tcp_nodelay on;
  45. Keepalive_timeout 65;
  46. # Set the server list of server load balancer
  47. Upstream mysvr {
  48. # The weigth parameter indicates the weight. a higher weight indicates a higher probability 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 {# This is the web service, listening to port 8080
  54. Listen 8080;
  55. Server_name 192.168.207.131; # this parameter varies depending on the system ip address.
  56. Index index.html index.htm;
  57. Root/var/www/html;
  58. # Error_page 500 502 503 504/50 x.html;
  59. # Location =/50x.html {
  60. # Root html;
  61. #}
  62. }
  63. # Set virtual hosts
  64. Server {
  65. Listen 80;
  66. Server_name 192.168.207.140; # VIP here
  67. # Charset gb2312;
  68. # Set access logs for the current virtual host
  69. Access_log logs/three. web. access. log main;
  70. # If you access/img/*,/js/*,/css/* resources, you can directly retrieve the local file without passing squid
  71. # This method is not recommended if there are many files, because the squid cache works better.
  72. # Location ~ ^/(Img | js | css )/{
  73. # Root/data3/Html;
  74. # Expires 24 h;
  75. #}
  76. # Enable server load balancer "/"
  77. Location /{
  78. Proxy_pass http: // mysvr; # Use the backend web server in this format
  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 10 m;
  84. Client_body_buffer_size 128 k;
  85. Proxy_connect_timeout 90;
  86. Proxy_send_timeout 90;
  87. Proxy_read_timeout 90;
  88. Proxy_buffer_size 4 k;
  89. Proxy_buffers 4 32 k;
  90. Proxy_busy_buffers_size 64 k;
  91. Proxy_temp_file_write_size 64 k;
  92. }
  93. # Set the address for viewing Nginx status, add -- with-http_stub_status_module parameters during installation
  94. Location/NginxStatus {
  95. Stub_status on;
  96. Access_log on;
  97. Auth_basic "NginxStatus ";
  98. Auth_basic_user_file conf/htpasswd; # set the access password, htpasswd-bc filename username password
  99. }
  100. }
  101. }


3. customize the keepalived configuration file

[Plain] view plaincopy

  1. Vi/etc/keepalived. conf


The content is 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" ### monitoring script
  12. Interval 2 ### monitoring time
  13. Weight 2 ### unclear
  14. }
  15. Vrrp_instance VI_1 {
  16. State MASTER ### set as MASTER
  17. Interface eth0 ### monitor Nic
  18. Virtual_router_id 51 ### the two servers must be the same
  19. Priority 101 ### weight MASTRE must be higher than BAUCKUP
  20. Authentication {
  21. Auth_type PASS
  22. Auth_pass 1111
  23. }
  24. Track_script {
  25. Chk_http_port ### service for monitoring execution
  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 content is as follows:

[Plain] view plaincopy

  1. ! /Bin/bash
  2. A = 'PS-C nginx -- no-header | wc-L' # check whether any nginx process assigns the value to variable.
  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 # end the keepalived process.
  8. Fi
  9. Fi


Check whether nginx is started. if nginx is not started, start nginx first. if it has not been started three seconds later, the keepalived process is also disabled, in this way, keepalived can take over and provide high availability. here, the keepalived service provides high availability, while nginx provides load balancing for backend web servers.
Add the execution permission to the script as follows:

[Plain] view plaincopy

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


5. start the service and test


Here, let's talk about it first. on WEB_1, I used the system's self-contained apache web server last night, which is quite easy. in this case, I only need to start the master-slave keepalived, because it will use the check_nginx.sh script to automatically start nginx.
All started.
Access http: // 192.168.207.140 to access the content of the three backend web servers.
Here we turn off the master keepalived service to test high availability.
Then, the logs are displayed in/var/log/messages on the keepalived server.

[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.


Continue to access http: // 192.168.207.140. you can still access the three backend web servers.
Then open the original master keepalived, and you can observe the log display of the original keepalived server.

[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) stored Ed 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.


It indicates that the original master-slave result is restored.
In the production environment, backend machines may also fail, but you don't have to worry about it. nginx will automatically allocate the session to a good backend web server.

OK. Now it's all over. practice the test and wish you success.

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

The above introduces nginx + keepalived high-availability server load balancer, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.