Nginx + keepalived + apache

Source: Internet
Author: User

This article Description: nginx achieves high Availability Based on keepalived, proxy web server based on its own proxy function, and implements load balancing.

Lab environment: redhat5.8
Experiment topology: Two nginx + keepalived servers and two apache servers are used to build a simple high-availability cluster.

 

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/02201631Z-0.png "/>

Ip Address Allocation:

 
 
  1. nginx1.weiyang.org 192.168.3.22 nginx1
  2. nginx2.weiyang.org 192.168.3.33 nginx2
  3. apache1.weiyang.org 192.168.3.205 apache205
  4. apache2.weiyang.org 192.168.3.206 apache206

I. installation and configuration of the experiment environment
It works from 205 and it works from 206, respectively. This helps us determine whether Server Load balancer has been implemented. Note: This is just a simple experiment in a virtual machine, files must be consistent in the actual production environment)

Ii. Install keepalived on the front-end nginx
Keepalived: http://www.keepalived.org/download.html#here I use the latest 1.2.7.
Note: The Development package group "Development Tools" and "Development Libraries" has been installed in advance by yum.
<1> unzip the downloaded package and install it:

 
 
  1. # Tar xvf keepalived-1.2.7.tar.gz
  2. # Cd keepalived-1.2.7
  3. #./Configure -- prefix =/usr/local/keepalived # specify the keepalived installation directory
  4. # Make & make install

<2> after the installation is complete, provide the configuration file and service script for keepalived.

 
 
  1. # Mkdir/etc/keepalived # create a configuration file directory for keepalived. The default configuration file is read from/etc/keepalived.
  2. # Cp/usr/local/keepalived/sbin/keepalived/usr/sbin/# copy the Binary command of keepalived
  3. # Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/init. d/# provide the Startup Script
  4. # Cp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/# Master configuration file of keepalived

<3> edit the/etc/keepalived. conf file. The modification content is as follows:

 
 
  1. ! Configuration File for keepalived
  2.  
  3. Global_defs {
  4. Notification_email {
  5. Acassen@firewall.loc
  6. Failover@firewall.loc
  7. Sysadmin@firewall.loc # these three are mailbox addresses, meaning to send mail to these three mailboxes in case of a fault
  8. }
  9. Icationication_email_from Alexandre.Cassen@firewall.loc # Which mailbox to send mail to the three mailboxes defined above
  10. Smtp_server 127.0.0.1
  11. Smtp_connect_timeout 30 # smtp connection timeout
  12. Router_id LVS_DEVEL # server identifier
  13. }
  14.  
  15. Vrrp_instance VI_1 {
  16. State MASTER # master server, slave server is set to BACKUP; when the MASTER node fails, it is automatically transferred to the master node as the MASTER node. When the master node recovers, it is automatically transferred to the defined MASTER node.
  17. Interface eth0 # specify the network card from which VRRP information is sent
  18. Virtual_router_id 51 # vro group id. The master and slave nodes must be in the same group.
  19. Priority 100 # priority, from 80
  20. Advert_int 1
  21. Authentication {
  22. Auth_type PASS # authentication method
  23. Auth_pass 1111 # Authentication Key
  24. }
  25. Virtual_ipaddress {
  26. 192.168.3.18 # the ip address of VRRP, that is, the ip address opened to the client, that is, the vip address.
  27. }
  28. }
  29. Configuration ends here, but not here;

<4> Configure the parameter in the same way as the parameter in another method. You only need to change priority 100 to 80, as long as it is smaller than 100 defined by the master );
Start the keepalived service on both servers: service keepalived start
 
<5> test the keepalived effect.
Run the following command on nginx1:

 
 
  1. [Root @ nginx1 ~] # Ip addr
  2. 1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue
  3. Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
  4. Inet 127.0.0.1/8 scope host lo
  5. Inet6: 1/128 scope host
  6. Valid_lft forever preferred_lft forever
  7. 2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  8. Link/ether 00: 0c: 29: e7: cb: a0 brd ff: ff
  9. Inet 192.168.3.22/24 brd 192.168.2.255 scope global eth0
  10. Inet 192.168.3.18/32 scope global eth0
  11. Inet6 fe80: 20c: 29ff: fee7: cba0/64 scope link
  12. Valid_lft forever preferred_lft forever
  13. 3: sit0: <NOARP> mtu 1480 qdisc noop
  14. Link/sit 0.0.0.0 brd 0.0.0.0
  15. ### The vip is bound to nginx1. Now we have stopped keepalived on nginx1.
  16. # Service keepalived stop
  17. ### Run the ip addr command on nginx2. the following result is displayed:
  18. [Root @ nginx2 ~] # Ip addr
  19. 1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue
  20. Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
  21. Inet 127.0.0.1/8 scope host lo
  22. Inet6: 1/128 scope host
  23. Valid_lft forever preferred_lft forever
  24. 2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  25. Link/ether 00: 0c: 29: 4f: 9a: 9d brd ff: ff
  26. Inet 192.168.3.33/24 brd 192.168.2.255 scope global eth0
  27. Inet 192.168.3.18/32 scope global eth0
  28. Inet6 fe80: 20c: 29ff: fe4f: 9a9d/64 scope link
  29. Valid_lft forever preferred_lft forever
  30. 3: sit0: <NOARP> mtu 1480 qdisc noop
  31. Link/sit 0.0.0.0 brd 0.0.0.0
  32. So far, nginx high availability is achieved based on keepalived ..

III. The following is a reverse proxy for nginx to build a simple high-availability server Load balancer cluster:
Make the same changes on the front-end two nginx servers:
Edit the nginx configuration file:

 
 
  1. # Vim/etc/nginx. conf
  2. User nginx; # the user and user group to use to run nginx. Before installing nginx, you must create this group and user.
  3. Worker_processes 2; # Start several processes
  4. Events {
  5. Use epoll; # nginx Working Mechanism
  6. Worker_connections 1024; # maximum number of connections of a single process. This is the default value and can be changed to 65535.
  7. }
  8.  
  9.  
  10. Http {
  11. Include mime. types;
  12. Default_type application/octet-stream;
  13. Sendfile on;
  14. # Tcp_nopush on;
  15.  
  16. # Keepalive_timeout 0;
  17. Keepalive_timeout 65; # nginx persistent connection duration
  18.  
  19. # Gzip on;
  20. Upstream weiyang {# define the cluster of the backend web Server
  21. Server 192.168.3.205: 80 max_fails = 3 fail_timeout = 10 s; # the ip address of the backend server, and the maximum number of errors and the length of time
  22. Server 192.168.3.206: 80 max_fails = 3 fail_timeout = 10 s;
  23. }
  24.  
  25.  
  26. Server {
  27. Listen 80;
  28. Server_name 192.168.3.18; # IP address of vrrp
  29. Location /{
  30. Root html;
  31. Index index.html;
  32. Proxy_pass http: // weiyang/; # Which backend performs reverse proxy?
  33. Proxy_redirect off;
  34. Proxy_set_header X-Real-IP $ remote_addr;
  35. Proxy_set_header X-Forwarded-For Proxy_add_x_forwarded_for;
  36. }
  37. Location/nginx {# nginx status page
  38. Access_log off;
  39. Stub_status on;
  40. }
  41.  
  42. # Error_page 404/404 .html;
  43.  
  44. # Redirect server error pages to the static page/50x.html
  45. #
  46. Error_page 500 502 503 x.html;
  47. Location =/50x.html {
  48. Root html;
  49. }
  50. }
  51. }
  52.  
  53. # Restart the nginx service after the configuration file is changed for nginx, and enable the httpd service for the backend two web servers at the same time
  54.  
  55. # Service nginx restart
  56. # Service httpd start

4. Implement nginx + keepalived + apache to build a high-availability cluster. We also need to modify the following information:
Imagine that if the nginx service on the nginx1 server fails, but the keepalived service runs normally, a single point of failure will still be formed;
To check whether the nginx service is running normally in real time, write a small script to check its running status.
Script implementation idea: When the nginx service fails, we will directly kill the keepalived process to achieve failover:
The script is as follows:

 
 
  1. # Vim ngpid. sh
  2. #! /Bin/bash
  3. N = 'ps-C nginx -- no-header | wc-l' # check whether there is an nginx process, perform row Calculation on it, and assign the calculation value to Variable N
  4. If [$ N-eq 0]; then # if statement, if N is not 0, the script is executed. if N is 0, restart the nginx service, sleeping for 2 seconds,
  5. Service nginx start
  6. Sleep 2
  7. If ['ps-C nginx -- no-header | wc-l'-eq 0]; then # judge again, if it is still 0,
  8. Killall keepalived # Kill all keepalived processes and implement failover.
  9. Fi
  10. Fi
  11. # Chmod + x ng_pid.sh
  12.  
  13. Put the script under/root/bash/and write it into the task plan. The script is executed every hour, as shown below:
  14. Echo-e '* 1 ***/root/bash/ngpid. Sh'>/etc/crontab

5. Access Test
Enter the following address in the local host Browser:
Http: // 192.168.3.18 check whether the result is polling,
At this time, nginx1 is a highly available master: Stop the nginx service. Wait for 3 seconds ..
Visit http: // 192.168.3.18 again to check whether the page shows normal polling

Nginx-based high-availability server Load balancer has been configured successfully. For more information, see ..

This article from the "when the soul of the body" blog, please be sure to keep this source http://19920601.blog.51cto.com/6110108/1154392

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.