Nginx + keepalived dual-host Hot Standby + Load Balancing ??? To be continued

Source: Internet
Author: User
Tags reflector nginx server

Keepalived + nginx Hot Standby + Load Balancing

Recently, due to business expansion, You need to convert Apache to nginx (Web), and then place nginx (Server Load balancer) on the web front end ). At the same time, combined with keepalived to implement ha for front-end nginx.
The nginx process is based on the master + slave (worker) multi-process model and has a very stable sub-process management function. In master process allocation mode, the master process never performs business processing, but only distributes tasks to achieve high reliability of the master process) all the business signals of the process are sent by the main process, and all the timeout tasks of the slave (worker) process are aborted by the master, which is a non-blocking task model.
Keepalived is a high-reliability running tool that implements vrrp backup routing in Linux. The Service Model Based on keepalived can truly achieve seamless IP address transfer when the master server and backup server fail. A stable software LB solution can be built by combining the two.




Prepare four computers for this experiment:

192.168.232.132 Web Server
192.168.232.20.web Server
192.168.232.134 keepalived nginx
192.168.232.135 keepalived nginx

Virtual IP address (VIP): 192.168.232.16

134 \ 135 configure virtual IP addresses for two hosts


The following uses 135 as an example:
VI/etc/sysconfig/network-scripts/ifcfg-eth2: 0

DEVICE=eth2:0TYPE=EthernetONBOOT=yesBOOTPROTO=staticDNS1=192.168.232.2IPADDR=192.168.232.16NETMASK=255.255.255.0GETWAY=192.168.232.2

Service Network restart

Use ifconfig to view the effect:

eth2      Link encap:Ethernet  HWaddr 00:0C:29:49:90:5B            inet addr:192.168.232.135  Bcast:192.168.232.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe49:905b/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:66322 errors:0 dropped:0 overruns:0 frame:0          TX packets:31860 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:67624991 (64.4 MiB)  TX bytes:2723877 (2.5 MiB)          Interrupt:19 Base address:0x2000eth2:0    Link encap:Ethernet  HWaddr 00:0C:29:49:90:5B            inet addr:192.168.232.16  Bcast:192.168.232.255  Mask:255.255.255.0          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          Interrupt:19 Base address:0x2000lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:22622 errors:0 dropped:0 overruns:0 frame:0          TX packets:22622 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0          RX bytes:1236328 (1.1 MiB)  TX bytes:1236328 (1.1 MiB)

The description takes effect.

134 \ 135 install keepalived and nginx on two hosts


Nginx installation:

1. Import the external software library
Rpm-uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm
Rpm-uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm
Rpm-uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Add Note below
Using list = http://dmirr.iuscommunity.org/mirrorlist? Ius-el6 & arch = $ basearch
Delete comments below
# Baseurl = http://dl.iuscommunity.org/pub/ius/stable/redhat/5/?basearch
2. Install nginx in yum
Yum install nginx

Install keepalived:

Install dependency
Yum-y install GCC + gcc-C ++
Yum install popt-devel OpenSSL-devel libssl-dev libnl-devel popt-devel

Install Kernel
Yum-y install kernel-devel
The current kernel code establishes connection ln-S/usr/src/kerners/2. 6 ...... // usr/src/Linux

Install keepalived
Wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
Tar-zxvf keepalived-1.2.2.tar.gz
CD keepalived-1.2.2
./Configure
Make
Make install

Copy the corresponding file

CP/usr/local/sbin/keepalived/usr/sbin/
CP/usr/local/etc/rc. d/init. d/keepalived/etc/init. d/
CP/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
CP-r/usr/local/etc/keepalived/etc/

Configure the keeplived and nginx hosts


134/135 perform the following operations:
VI/etc/nginx/CONF. d/Default. conf

server {    listen       8088;    server_name  localhost;    location / {        root   /var/www/html;        index  index.html index.htm;    }    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   /usr/share/nginx/html;    }}

135 perform the following operations:
VI/var/www/html/index.html

 

134 perform the following operations:
VI/var/www/html/index.html

 

134 perform the following operations:
VI/etc/keepalived. conf

! Configuration File for keepalivedglobal_defs {   notification_email {     #[email protected]     #[email protected]     #[email protected]   }   #notification_email_from [email protected]   #smtp_server 192.168.200.1   #smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_script chk_http_port {    script "</dev/tcp/127.0.0.1/8088"    interval 1    weight -2}vrrp_instance VI_1 {    state MASTER    interface eth2    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.232.16    }    track_script {        chk_http_port    }}

135 perform the following operations:
VI/etc/keepalived. conf

! Configuration File for keepalivedglobal_defs {   notification_email {     #[email protected]     #[email protected]     #[email protected]   }   #notification_email_from [email protected]   #smtp_server 192.168.200.1   #smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_script chk_http_port {    script "</dev/tcp/127.0.0.1/8088"    interval 1    weight -2}vrrp_instance VI_1 {    state BACKUP    interface eth2    virtual_router_id 51    priority 99    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.232.16    }    track_script {        chk_http_port    }}

TIPS:

State parameter value: Master and backup
Priority parameter value: Master> Backup
Virtual_router_id: The parameter value must be the same

Test:


Keepalived and nginx are enabled on both Test Machines 134 \ 135
Service keepalived restart
Service keepalived nginx

Verify that nginx is started properly:
Access master: http: // 192.168.232.134: 8088/
Access backup: http: // 192.168.232.135: 8088/

View the logs of keepalived:

134 \ 135 open the log information to facilitate viewing the dynamic keepalived:
Tail-F/var/log/messages

Open the virtual IP address access in the browser: http: // 192.168.232.16: 8080/. The IP address is 192.168.232.134.

Test on Dual-machine hot standby on the server layer (for example, when the server goes down and keepalived goes down:


Kill the keepalived process of 192.168.232.134 (master)
Killall keepalived
134 of the log information is as follows:

Jun 11 18:03:10 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.232.16Jun 11 18:03:15 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.232.16Jun 11 19:30:44 localhost Keepalived: Terminating on signalJun 11 19:30:44 localhost Keepalived: Stopping Keepalived v1.2.2 (06/10,2014)Jun 11 19:30:44 localhost Keepalived_vrrp: Terminating VRRP child process on signalJun 11 19:30:44 localhost Keepalived_healthcheckers: Terminating Healthchecker child process on signal

135 of the log information is as follows:

Jun 11 19:30:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Jun 11 19:30:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16Jun 11 19:30:50 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.232.16 addedJun 11 19:30:55 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16

Refresh http: // 192.168.232.16: 8080/. The IP address is 192.168.232.135.

Start the keepalived process of 192.168.232.134 again. 192.168.232.134 is automatically taken over as the master and 192.168.232.135 is automatically converted to backup. From the test results, the backup function is successfully taken over and hot backup has been implemented.

Application Layer (Web) Dual-machine Hot Standby (for example, nginx process is accidentally killed, Web port failure) test:


Disable the nginx service of 192.168.232.134 (master:
Service nginx stop

134 of the log information is as follows:

Jun 11 19:38:49 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) failedJun 11 19:38:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advertJun 11 19:38:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATEJun 11 19:38:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.Jun 11 19:38:51 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.232.16 removed

135 of the log information is as follows:

Jun 11 19:38:52 localhost Keepalived_vrrp: VRRP_Instance(VI_1) forcing a new MASTER electionJun 11 19:38:53 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATEJun 11 19:38:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATEJun 11 19:38:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Jun 11 19:38:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16Jun 11 19:38:54 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.232.16 addedJun 11 19:38:59 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.232.16

Refresh http: // 192.168.232.16: 8080/. The IP address is 192.168.232.135.

Start the nginx process 192.168.232.134 again, And 192.168.232.134 will automatically take over as the master, and 192.168.232.135 will be automatically converted to backup. From the test results, the backup function is successfully taken over, and hot backup has been implemented.

Why are the state parameters of the master and slave databases all master databases? If you have no errors, you must set them to the same one. Otherwise, the desired VIP floating effect cannot be achieved, I found it after a long test. after the State is set to master, it determines who is the real master based on the value competition of priority. When the script detection fails, the weight is subtracted from the corresponding value, for example, if the original master (181) Priority = 100, if the script detects that port 8088 cannot be connected, it will priority-2 = 98, <S-B (150) priority (99 ), at this time, the S-B (150) will compete to become the master, so as to achieve the hot standby of Web applications.


If the above experiments are no problem, then the nginx Server Load balancer configuration, Configuration Modification see the following: http://blog.csdn.net/e421083458/article/details/30086413






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.