Keepalived+nginx provides front-end load balancer + master-slave dual-machine hot standby + automatic switching

Source: Internet
Author: User
Tags nginx server nginx reverse proxy

Original link: http://unun.in/linux/156.html

Scheme:

Adopt two Nginx server as front-end, provide static Web content, distribute Web request, one from, keepalived implement condition monitoring, ensure nginx normal service, that is, after the main nginx services process dies, Keepalived can switch access to the Web site from Nginx by script or through a program detection mechanism. The monitoring of the backend Web application server is the responsibility of Nginx, keepalived only monitor the health status of Nginx.

Performance: Approximately 10% of the hardware load balancer

Advantage: Although performance is far weaker than the lvs+keepalived scheme, Nginx itself is a Web server, can handle static files, page caching, rewrite and other web container functions, load balancing applications in the Web front-end preferred this scenario.

Application scenario: As a load balancer on the front-end of a Web application server cluster.

Environment:

Operating system: centos6.5

Keepalived version: keepalived-1.2.13.tar.gz

Nginx Version: nginx-1.6.2.tar.gz

Host IP

node0:192.168.99.138

node1:192.168.99.155

Virtual ip:192.168.99.168

Back-end Web server IP:

ng2:192.168.99.152

ng3:192.168.99.135

----------------------------------------------------

Here we choose is LNMP environment, LNMP Environment can build their own installation, here also has a simple configuration tutorial: CentOS + nginx + php-fpm +mysql Simple configuration, the production environment is best to compile their own installation.

----------------------------------------------------

Keepalived installation configuration steps:

1.1 Download keepalived, official website: http://www.keepalived.org/download.html

Cd/usr/local/src/wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

Have to say is keepalived download slow into Xiang, eventually also no wget download down, I spread to Oschina on the

: http://git.oschina.net/xiaoshusheng/Slow-download-speed/tree/master/keepalived

1.2 Unzip, and compile the installation keepalived

TAR-ZXVF keepalived-1.2.13.tar.gzcd keepalived-1.2.13./configure--prefix=/usr/local/keepalivedmake && make Install

2.1 Setting environment variables and INIT.D, configuring Conf files

cp/usr/local/keepalived/sbin/keepalived/usr/sbin/cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/ sysconfig/cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/mkdir/etc/keepalivedcp/usr/local/ Keepalived/etc/keepalived/keepalived.conf/etc/keepalivedvi/etc/keepalived/keepalived.conf

Node1 This section according to the following changes, the other default on the line

Vrrp_instance vi_1 {State MASTER interface eth0 virtual_router_id Wuyi priority Advert_int 1 Authenti cation {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.99.168}}

NODE0 This section according to the following changes, the other default on the line

Vrrp_instance vi_1 {State BACKUP interface eth0 virtual_router_id 1 advert_int ation {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.99.168}}

Here is let Node1 when the Lord, Node0 do from, other parts, you can, as appropriate, according to their actual situation to make corresponding changes!

In fact, in addition to global_defs{} and Vrrp_instance vi_1{} two surrounded by the left, the rest of the configuration can be deleted

Start keepalived

/etc/init.d/keepalived start

Use IP addr To see if the virtual IP is bound,

Results of Node1:

Results of NODE0:

You can see that Node1 is taking over the virtual IP (VIP).

----------------------------------------------------

2.1 Nginx installation, see: CentOS + nginx + php-fpm +mysql Simple configuration of the Nginx configuration section, or: part of this post: http://unun.in/linux/153.html

----------------------------------------------------

3.1 Nginx reverse proxy and load balancing please refer to: Using Nginx to do reverse proxy and load balancing

----------------------------------------------------

Let keepalived monitor nginx status

4.1 Installing Nmap

Yum Install-y Nmap

Here the direct Yum installs, if you compile the installation can also, here do not do too much to speak clearly

4.2 Write the shell script, check whether the port is open, if not, reboot, still not, then discard

vi/opt/chk_nginx.sh

Add the following content:

#!/bin/bash# Check nginx server Statusnginx=/usr/local/nginx/sbin/nginxport=80nmap localhost-p $PORT | grep "$PORT/tcp Open" #echo $?if [$?-ne 0];then $NGINX-S Stop $NGINX sleep 3 nmap localhost-p $PORT | grep "$PORT/tcp open" [$?-ne 0] &&/etc/init.d/keepalived STOPFI

Write the script and don't forget to give it permission to execute, otherwise it's invalid

Cd/opt/chmod +x chk_nginx.sh

----------------------------------------2015.08.03 More-------------------------------

Modify Keepalived.conf again

Vi/etc/keepalived/keepalived.conf

Add these two parts to the configuration

Vrrp_script chk_http_port {script "/opt/chk_nginx.sh" Interval 2 weight 2}

Then add the following configuration in the Vrrp_instance vi_1

Track_script {Chk_http_port}

The final structure of the configuration looks like this, notice the difference between the master and slave configuration, here is the final configuration of the main:

global_defs {   router_id lvs_devel}vrrp_script  chk_http_port {    script  "/opt/chk_nginx.sh"      interval 2    weight 2}vrrp_instance vi_1 {     State master    interface eth0    virtual_router_id 51     priority 100    advert_int 1     authentication {        auth_type pass         auth_pass 1111    }    virtual_ ipaddress {        192.168.99.168    }     track_script {        chk_http_port     }} 


Further, in order to avoid starting keepalived before the Nginx, you can start the/etc/init.d/keepalived in the first nginx:

Vi/etc/init.d/keepalived

Modify the Start () section to look like this

Start () {/usr/local/nginx/sbin/nginx sleep 3 echo-n $ "Starting $prog:" Daemon keepalived ${keepalived_optio    NS} retval=$? echo [$RETVAL-eq 0] && touch/var/lock/subsys/$prog}

------------------------------------------------------------------------------

Detection is successful

Close Nginx, reboot keepalived, see if Virtual IP is taken over

/etc/init.d/keepalived restart

Use on Node1 (master)

IP addr

As you can see, the virtual IP has been Node1 (master) taken over

Then use virtual ip:192.168.99.168 access, access to normal

Use the NETSTAT-LTUNP command to see if nginx exists: result exists

Then kill the nginx process of the main service.

Then use the NETSTAT-LTUNP command to see if Nginx restarts: results have been restarted

And then kill the Nginx process for the main service.

Then use the NETSTAT-LTUNP command to see if Nginx restarts: results have been restarted

Use service keepalived status to view keepalived state: Results show: keepalived (PID 2439) is running ...

Modify the Nginx configuration on the Node1, deliberately create errors, and then kill the Nginx process

Use the NETSTAT-LTUNP command to view nginx status: It's not up.

Then use service keepalived status to view the Keepalived status: Results displayed: keepalived is stopped

Then access the virtual ip:192.168.99.168 and still be able to access it normally

Use IP addr on Node0 (from) to see that the virtual IP has been taken over by the NODE0 (slave)

Use the NETSTAT-LTUNP command to see if nginx exists: result exists

Then kill the nginx process of the main service.

Then use the NETSTAT-LTUNP command to see if Nginx restarts: results have been restarted

On the switch to Node1 (master), change the Nginx configuration to correct, and then start the keepalived

/etc/init.d/keepalived start

When viewed using the IP addr command, it is found that the virtual IP has been Node1 (master) re-takeover

Use the NETSTAT-LTUNP command to view nginx status: Started

Through the above test can be found that the availability of greatly improved, overcome the load Balancer single point of problem, but from the load balancer a lot of time in idle state, or there is a certain waste!

Keepalived+nginx provides front-end load balancer + master-slave dual-machine hot standby + automatic switching

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.