Using keepalived to achieve high availability of nginx

Source: Internet
Author: User

First, Introduction

Nginx is a lightweight Web server and reverse proxy server, it has a strong concurrency, more simple and flexible configuration, as well as ultra-strong performance. Nginx more and more people's attention, many domestic sites are using Nginx as its Web server or reverse proxy.

Keepalived is a software using the VRRP protocol in Linux to achieve high-availability, VRRP can be two or more routing devices virtual into a single device, and provide VIP for external network access, internal through the corresponding algorithm to elect the master, the VIP is configured on the master, Provide services to the outside, when Master is down, backup does not receive the message from master at a certain time, at this time, the election will happen again, the backup node will replace the master node, become the new master, and continue to provide services, This enables high availability of the service.

Second, the experimental environment

Two Linux hosts with a system version of redhat5.8 32-bit

The IP address of the Node1 is: 192.168.1.85

The IP address of the Node2 is: 192.168.1.86

VIP is: 192.168.1.90

keepalived software version keepalived-1.2.7.tar.gz:http://www.keepalived.org/download.html


Nginx software version: nginx-1.4.7.tar.gz for http://nginx.org/


Third, the experimental process

1. Unzip and install the appropriate software

Keepalived:

Tar XF keepalived-1.2.7.tar.gz CD keepalived-1.2.7./configure make do Install

(1) Copy the corresponding file into the corresponding directory

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/usr/local/etc/keepalived//etc/-R

(2) Editing the keepalived.conf configuration file

Cp/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bkup save a copy of the configuration

Edit the following

! configuration file for keepalivedglobal_defs {   notification_email  {    [email protected]      #定义收件人    }    notification_email_from [email protected]     #定义发件人     smtp_server 127.0.0.1   smtp_connect_timeout 30   router_ id lvs_devel}vrrp_script chk_nginx {    script  "killall -0  Nginx "      #检测nginx是否在线     interval 1          #检测周期为1秒     weight 2            #如果nginx不在线, the weight is reduced 2}vrrp_script chk_keepalived_down {    script  "[[ -f /etc/keepalived/down ]] && exit 1 | |  exit 0 "   #测试keepaliveD Primary and Standby switching conditions    interval 1   weight 2}vrrp_instance VI_1 {     state MASTER       #node1为主节点      interface eth0     #监听的接口     virtual_router_id 52      #虚拟路由ID, must be consistent at both ends

Priority 101 #优先级, Master end is higher than the backup side Advert_int 1 #通告周期 1 seconds authentication {Auth_type PASS #认证方式 Auth_pass 1111 #认证秘钥} virtual_ipaddress {192.168.1.90 #vip的设定} track_script {#要执行 The detection script Chk_nginx Chk_mantaince_down}

There is a lot of content in the keepalived.conf, but it's all for LVS, and there's no function to start the LVS, so it's all erased.

The configuration of the Node2 is similar to the Node1, except that the state master is changed to backup and the priority is lower than master.


Nginx:

(1) Resolving dependencies

Yum Install Pcre-devel-y

(2) Adding users and Groups

Groupadd-r Nginxuseradd-r-G nginx

(3) Unzip and compile the installation

Tar xf  nginx-1.4.7.tar.gzcd nginx-1.4.7 ./configure   --prefix=/usr    --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf    --error-log-path=/var/log/nginx/error.log   --http-log-path=/var/log/nginx/access.log    --pid-path=/var/run/nginx/nginx.pid    --lock-path=/var/lock/nginx.lock    --user=nginx   --group=nginx   --with-http_ssl_module    --with-http_flv_module   --with-http_stub_status_module   -- with-http_gzip_static_module   --http-client-body-temp-path=/var/tmp/nginx/client/    --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/ Fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var /tmp/nginx/scgi   --with-pcre 

Make && make install

(4) After modifying the homepage, you can start the service.


Start keepalived on two nodes

Service keepalived Start

Viewing logs on the Node1

Tail /var/log/messagesaug 19 19:12:16 localhost keepalived_vrrp[27587]: vrrp_ Script (Chk_nginx)  succeededAug 19 19:12:16 localhost Keepalived_vrrp[27587]:  Vrrp_script (Chk_mantaince_down)  succeededaug 19 19:12:17 localhost keepalived_vrrp[ 27587]: vrrp_instance (vi_1)  Transition to MASTER STATEAug 19 19:12:18  Localhost keepalived_vrrp[27587]: vrrp_instance (vi_1)  Entering MASTER STATEAug  19 19:12:18 localhost keepalived_vrrp[27587]: vrrp_instance (VI_1)  setting  protocol vips.aug 19 19:12:18 localhost keepalived_healthcheckers[27586]:  netlink reflector reports ip 192.168.1.90 addedaug 19 19:12:18  localhost avahi-daemon[3327]: registering new address record for  192.168.1.90 on eth0. aug 19 19:12:18 localhost keepalived_vrrp[27587]: vrrp_instance (vi_1)  Sending gratuitous  ARPs on eth0 for 192.168.1.90Aug 19 19:12:18 localhost  keepalived_vrrp[27587]: netlink reflector reports ip 192.168.1.90 addedaug  19 19:12:23 localhost keepalived_vrrp[27587]: vrrp_instance (VI_1)  Sending  gratuitous arps on eth0 for 192.168.1.90

You can see that Node1 was elected for MASTER,VIP configuration on Node1.

Use IP addr to view

... 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast Qlen, Link/ether 00:0c:29:a3:12:75 BRD F F:ff:ff:ff:ff:ff inet 192.168.1.85/24 BRD 192.168.1.255 scope global eth0 inet 192.168.1.90/32 scope Global eth0


Viewing log information on Node2

aug 19 19:12:17 localhost keepalived_vrrp[16800]: registering kernel  Netlink reflectoraug 19 19:12:17 localhost keepalived_vrrp[16800]: registering  kernel netlink command channelaug 19 19:12:17 localhost keepalived_ vrrp[16800]: registering gratuitous arp shared channelaug 19 19:12:17  localhost keepalived_vrrp[16800]: opening file  '/etc/keepalived/keepalived.conf '. Aug 19 19:12:17 localhost keepalived_vrrp[16800]: configuration is using  : 40529 bytesaug 19 19:12:17 localhost keepalived_vrrp[16800]: using  linkwatch kernel netlink reflector ... Aug 19 19:12:17 localhost keepalived_vrrp[16800]: vrrp_instance (VI_1)  Entering  backup stateaug 19 19:12:17 localhost keepalived_vrrp[16800]: vrrp sockpool: [ifindex (2),  proto, &NBSP;FD (11,12)]aug 19 19:12:17  localhost keepalived_vrrp[16800]: vrrp_script (Chk_nginx)  succeededAug 19  19:12:17 localhost keepalived_vrrp[16800]: vrrp_script (Chk_mantaince_down)  succeeded

For backup status


At this point, open the browser access VIP 192.168.1.90 provides the page is Node1


Drop the Nginx service on the Node1

Service Nginx Stop

Check the logs on the Node1 again

Aug 19 19:18:15 localhost keepalived_vrrp[27587]: vrrp_script (Chk_nginx)   Failedaug 19 19:18:16 localhost keepalived_vrrp[27587]: vrrp_instance (VI_1)   RECEIVED&NBSP;HIGHER&NBSP;PRIO&NBSP;ADVERTAUG&NBSP;19&NBSP;19:18:16&NBSP;LOCALHOST&NBSP;KEEPALIVED_VRRP[27587] :  vrrp_instance (vi_1)  Entering BACKUP STATEAug 19 19:18:16 localhost  Keepalived_vrrp[27587]: vrrp_instance (vi_1)  removing protocol VIPs.Aug 19  19:18:16 localhost keepalived_healthcheckers[27586]: netlink reflector reports  ip 192.168.1.90 removedaug 19 19:18:16 localhost avahi-daemon[3327]:  Withdrawing address record for 192.168.1.90 on eth0. Aug 19 19:18:16 localhost keepalived_vrrp[27587]: netlink reflector reports  ip 192.168.1.90 removedaug 19 19:18:17 localhost keepalived_vrrp[27587]: vrrp_script (Chk_nginx)  succeeded 

At this point, the Node1 has become backup status, VIP has been removed


View log information for Node2

Aug 19 19:12:17 localhost keepalived_vrrp[16800]: vrrp_script (Chk_nginx)   Succeededaug 19 19:12:17 localhost keepalived_vrrp[16800]: vrrp_script (Chk_mantaince_ Down)  succeededaug 19 19:18:16 localhost keepalived_vrrp[16800]: vrrp_instance ( Vi_1)  forcing a new MASTER electionAug 19 19:18:17 localhost  Keepalived_vrrp[16800]: vrrp_instance (vi_1)  transition to master stateaug 19  19:18:18 localhost keepalived_vrrp[16800]: vrrp_instance (VI_1)  Entering MASTER  stateaug 19 19:18:18 localhost keepalived_vrrp[16800]: vrrp_instance (VI_1) &NBSP;SETTING&NBSP;PROTOCOL&NBSP;VIPS.AUG&NBSP;19&NBSP;19:18:18&NBSP;LOCALHOST&NBSP;KEEPALIVED_VRRP[16800]:  vrrp_instance (vi_1)  sending gratuitous arps on eth0 for 192.168.1.90

The node2 becomes the master node and the entire switchover process is complete.


This article is from the Linux OPS blog, so be sure to keep this source http://hld1992.blog.51cto.com/10543286/1686166

Using keepalived to achieve high availability of nginx

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.