Turn--nginx+keepalived for load balancing and high availability in Ubuntu

Source: Internet
Author: User

Nginx has been used for a long time, but it has only recently been done using Nginx for load balancing and high availability.

General idea: According to the characteristics of keepalived, through a virtual IP to achieve a master-slave server switchover, if one server down, you can automatically switch to another backup server, so as not to affect user access.

Here are my installation configuration steps, please refer to the following.

1: Server Ready

Prepare two Ubuntu virtual host servers, the corresponding IP is 192.168.1.100 192.168.1.200

The IP of both hosts must be in the same network segment

2: Virtual IP Preparation

sudo ifconfig eth0:0 192.168.1.150 netmask 255.255.255.0

Then look at the ifconfig, it is not difficult to find the difference with the previous ifconfig. can also be viewed through IP A, the corresponding network card eth0 has two IPAddress

Two virtual hosts to use the same virtual ip:192.168.1.150

3: Install Nginx

sudo apt-get install Nginx

After the installation is complete, ps-ef|grep nginx view Nginx process, if no problem, you can view http://192.168.1.100 if the display is normal, the Nginx service starts normally.

Nginx configuration is as follows:

C code
  1. User Www-data;
  2. Worker_processes 4;
  3. Pid/var/run/nginx.pid;
  4. Events {
  5. Worker_connections 1024;
  6. # multi_accept on;
  7. }
  8. HTTP {
  9. ##
  10. # Basic Settings
  11. ##
  12. Sendfile on;
  13. Tcp_nopush on;
  14. Tcp_nodelay on;
  15. Keepalive_timeout 65;
  16. Fastcgi_buffer_size 64k;
  17. Fastcgi_buffers 64k;
  18. Types_hash_max_size 2048;
  19. # Server_tokens off;
  20. # Server_names_hash_bucket_size 64;
  21. # server_name_in_redirect off;
  22. Include/etc/nginx/mime.types;
  23. Default_type Application/octet-stream;
  24. ##
  25. # Logging Settings
  26. ##
  27. Access_log/var/log/nginx/access.log;
  28. Error_log/var/log/nginx/error.log;
  29. Upstream SERVER_LB {
  30. Server 192.168.1.100:9000; //9000 port for PHP
  31. Server 192.168.1.200:9000;
  32. Ip_hash;
  33. }
  34. server {
  35. Listen 80;
  36. server_name _;
  37. #root/var/www/html/demo/public;
  38. #charset Koi8-r;
  39. Try_files $uri $uri/$uri. php $args/index.php;
  40. Location ~ \.php$ {
  41. Fastcgi_pass server_lb; //Load balancing via fastcgi
  42. Include Fastcgi_params;
  43. }
  44. Error_page 502 503 504/50x.html;
  45. Location =/50x.html {
  46. root HTML;
  47. }
  48. }
  49. }

The Nginx configuration of the two servers is consistent

4: Install keepalived

sudo apt-get install keepalived

After the installation is complete, review the process discovery and there is no keepalived process, because the keepalived profile does not exist and requires the user to create a keepalived.conf file themselves to be stored in the/etc/keepalived directory

SH code
  1. Vrrp_script Chk_nginx {
  2. script "/etc/keepalived/check_nginx.sh"//To detect Nginx process
  3. Interval 2
  4. Weight 2
  5. }
  6. Global_defs {
  7. Notification_email {
  8. You can add email reminders
  9. }
  10. }
  11. Vrrp_instance Vi_1 {
  12. State master//master server
  13. Interface eth0
  14. virtual_router_id Wuyi
  15. Mcast_src_ip 192.168. 1.100
  16. Priority
  17. Advert_int 1
  18. Authentication {
  19. Auth_type PASS
  20. Auth_pass 123456
  21. }
  22. Track_script {
  23. Chk_nginx
  24. }
  25. virtual_ipaddress {
  26. 192.168.1.150
  27. }
  28. }

The keepalived.conf files for both servers are not the same as the state and mcast_src_ip, except for the priority, and the others are exactly the same, but the points to note are:

  • The primary server state is master, and the server's state is backup
  • The primary server priority must be greater than the priority from the server
  • MCAST_SRC_IP is the local area network IP of the corresponding server
  • "{" must be preceded by a space, not with the previous word Fulianqi, or there is a problem difficult to find
  • The check_nginx.sh script file must have the correct execution permissions.
  • The code for
  • check_nginx.sh is as follows: SH code   
    1. #more  /etc/keepalived/check_ http.sh  
    2. #!/bin/bash  
    3. #代码一定注意空格, the logic is: if the nginx process does not exist, then start Nginx, If Nginx fails to start, kill all keepalived processes   
    4. a= ' ps -c nginx --no-header |wc -l '   
    5. if [  $A  -eq 0 ];THEN  
    6.  /etc/init.d/nginx start  
    7. sleep 3  < /span>
    8. if [  ' ps -c nginx --no-header |wc -l '-eq  0&NBSP;];THEN&NBSP;&NBSP;
    9.  killall keepalived  
    10. fi  
    11. fi  

After the configuration file is complete, sudo/etc/init.d/keepalived start, and then check to see if the keepalived process exists, and if so, check the keepalived log (sudo tail/var/log/ syslog), if you see Nginx keepalived_vrrp:vrrp_script (Chk_nginx) succeeded, the configuration is successful.

The keepalived configuration and startup commands for both servers are consistent

5: Test

Because Check_nginx, when the start keepalived is, Nginx will also be started, so at this time, the view nginx and keepalived process should be there, or the configuration is a problem.

Now access to http://192.168.1.150, if the access is normal, it should be displayed at this time is the main server page, that is, 192.168.1.100 page, otherwise there is a configuration problem.

Then in the shutdown of the primary server Nginx access, access to the http://192.168.1.150 page should be normal, because of Check_nginx's sake.

Then close the keepalived process of the primary server, then access http://192.168.1.150 should display the page from the server, that is, the 192.168.1.200 page, otherwise it is not configured correctly.

If you shut down Nginx and keepalived from the server again, http://192.168.1.150 cannot be accessed.

The above is my configuration steps, very sketchy, but also hope that we have more guidance.

In addition, the following is the version information

Ubuntu:ubuntu 12.04.1 LTS (gnu/linux 3.2.0-29-generic x86_64)

keepalived:v1.2.2

Nginx version:nginx/1.1.19

Turn--nginx+keepalived for load balancing and high availability in Ubuntu

Related Article

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.