Nginx keepalived Configuration

Source: Internet
Author: User

Objective:

For large Web projects, it is essential to be highly available.

That with the company's business development, although the use of Nginx load balancer can still support the daily average million PV access, and run for two years without a single point of failure, but still have to consider to avoid a single point of failure caused by business interruption.


keepalived Introduction:

The main goal of the Keepalived project is to provide a balanced and highly available infrastructure for Linux systems and Linux.

The load-balancing architecture relies on the Ipvs kernel module to provide four-tier load balancing, and keepalived enables dynamic checks and load pool-based management services. On the other hand, high availability is achieved through the VRRP protocol. A simple framework can provide a resilient infrastructure, either individually or together.


keepalived configuration file:

The keepalived.conf file consists of three configuration parts:

    • Globals Configurations Global Configuration

    • VRRP Configuration VRRP Configurations

    • LVS Configuration LVs


Conventions:

This article does not do Nginx installation introduction, about Nginx installation can refer to the previous article

System: with CentOS 6.8 minimized

Source Files directory:/USR/LOCAL/SRC

master_ip:192.168.1.10

backup_ip:192.168.1.11

vip:192.168.1.20

Keepalived official website http://www.keepalived.org/

keepalived Master lists a number of configuration items, which you can choose to use as appropriate.

In the main installation of the Nginx configuration keepalived


Get ready:

Download Keepalived Source Package:

wget http://www.keepalived.org/software/keepalived-1.3.2.tar.gz

To install the development tools:

Yum Install gcc-c++ Openssl-devel


Iptables configuration:

Turn off iptables, or allow primary and standby

Iptables-i input-i eth1-s 192.168.1.11-j ACCEPT
Iptables-i input-i eth1-s 192.168.1.10-j ACCEPT


Install keepalived:

Extract:

Tar zxf keepalived-1.3.2.tar.gz

To configure a compilation installation:

CD Keepalived-1.3.2./configuremakemake Install

Create Profiles and services:

Cp-r keepalived/etc/keepalived//ETC/CP KEEPALIVED/ETC/INIT.D/KEEPALIVED.RH.INIT/ETC/INIT.D/KEEPALIVEDCP keepalived/etc/sysconfig/keepalived/etc/sysconfig/


Configuration keepalived:

Master keepalived.conf

! configuration file for keepalivedglobal_defs {   notification_email  {     [email protected]     #接收通知的邮件地址     }   notification_email_from [email protected]oc    # e-mail notification address    smtp_server 192.168.200.1     #发送邮件的SMTP服务地址, but IP or domain name. Optional port number   (default)    smtp_connect_timeout 30     #smtp连接超时时间秒     router_id LVS_DEVEL     #主机标识 for email notification    vrrp_skip_check_adv_addr         #vrrp_strict      #严格执行VRRP协议规范, this mode does not support node unicast     vrrp_garp_interval 0   vrrp_gna_interval 0   script_user  keepalived_script       #  Specifies the user name and group to run the script. Default user groups are used.                                             #  If not specified, the default is keepalived_script  user, if no such user, use root   enable_script_security                   #如过路径为非root可写, Do not configure the script to execute for the root user. }vrrp_script chk_nginx_service {     #VRRP   Script declarations      script  "/usr/local/keepalived/nginx-ha-check"      #周期性执行的脚本      interval 3     #运行脚本的间隔时间, Sec     weight -50      #权重, adjust the priority (-255-255). Priority value minus this value is less than the priority value of the standby service                               #否则备服务选举为主服务时会失败.     fall 3               #  failed to detect several failures, integer     rise 2               #  detect several states as normal, only confirm normal, integer     user keepalived_ script    #  the user or group that executed the script}vrrp_instance vi_1 {     #VRRP   instance declarations     state MASTER     #实例默认状态, master| backup    interface eth0     #绑定的接口      #mcast_ Source IP address used by src_ip 192.168.1.10        # vrrp  messages      unicast_src_ip 192.168.1.10        # vrrp Source IP address   (ALIAS TO MCAST_SRC_IP) used for   messages     unicast_peer {             #  does not use VRRP multicast, both unicast       192.168.1.11             #   node IP, can be multiple     virtual_router _id 51     #VRRP   Route identification (-1-255), Primary and standby     priority 100      #优先数,-1-255, master service is larger than standby     advert_int 1     #VRRP   AD interval     authentication {     #主备认证          auth_type PASS     #认证方式          auth_pass 1111     #主备需统一     }     virtual_ipaddress {     #VRRP   Address, can have multiple          192.168.1.20    }    track_script {     #脚本监控状态         chk_nginx_service     #可加权重, but overrides the declared script weight value. chk_nginx_service weight -20    }}


Backup keepalived.conf

! configuration file for keepalivedglobal_defs {   notification_email  {     [email protected]   }   notification_ Email_from [email protected]   smtp_server 192.168.200.1   smtp_ Connect_timeout 30   router_id lvs_devel   vrrp_skip_check_adv_addr    vrrp_strict   vrrp_garp_interval 0   vrrp_gna_interval  0}vrrp_script chk_nginx_service {    script  "/usr/local/keepalived/ Nginx-ha-check "    interval 3    weight -10}vrrp_instance  VI_1 {    state BACKUP    interface eth0     virtual_router_id 51    priority 90     advert_int 1    authentication {        auth_type pass         auth_pass 1111    }     virtual_ipaddress {        192.168.1.20    }     track_script {        chk_nginx_service     }}


Nginx-ha-check Script

chmod +x Nginx-ha-check
#!/bin/shpath=/bin:/sbin:/usr/bin:/usr/sbinstatefile=/var/run/nginx-ha-keepalived.stateif [ -s  "$STATEFILE"  ]; then    .  "$STATEFILE"     case  "$ State " in        " BACKUP "|" MASTER "|" FAULT ")                 service  nginx status                 exit $?                 ;;         *| "")                 logger -t  nginx-ha-keepalived  "unknown state:  ' $STATE '"                  exit 1                ;;     esacfiservice nginx statusexit $?


Start the service:

Service keepalived Start


Verify:

After the primary and standby service installation configuration is complete, the 192.168.1.20 address can be accessed via the browser, while the main Nginx service is stopped and the 192.168.1.20 view page is visited again.

Nginx Primary and standby services need to be able to confirm a uniquely identified page, such as in index.html, indicate the primary and standby services respectively.


/var/log/messages log information can be observed at the same time





This article is from the "Morrowind" blog, make sure to keep this source http://morrowind.blog.51cto.com/1181631/1879447

Nginx keepalived Configuration

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.