Keepalived + nginx for highly available cluster scenarios

Source: Internet
Author: User

Keepalived + nginx for highly available cluster scenarios

I. Introduction to the usage scenario:
Nginx do load balancing, to achieve the purpose of distribution requests, but not good to avoid a single point of failure, if the Nginx server hanging point, then all services will be followed by paralysis. Keepalived+nginx, it will be a good solution to this problem.

Two. Introduction of principle:
Keepalived is a high-performance, server-ready or hot-standby solution, keepalived can be used to prevent the server from a single point of
The high availability of Web front-end services can be achieved with Nginx.
Keepalived is based on the VRRP protocol and uses the VRRP protocol to achieve high availability (HA). VRRP (Virtual Router
Redundancy Protocol) protocol is used to implement router redundancy protocol, VRRP protocol to virtual two or more router devices into a
device, providing a virtual router IP (one or more), while inside the router group, if the router that actually owns this external IP is
If the result is master, or it is elected by the algorithm, master implements various network functions for virtual router IP,
such as ARP requests, ICMP, and data forwarding, etc., other devices do not own the virtual IP, the status is BACKUP, in addition to receiving the MASTER
External network functionality is not performed outside of the VRRP status notification information. When the host fails, BACKUP takes over the network functionality of the original master.
The VRRP protocol uses multicast data to transmit VRRP data, VRRP data to send data using a special virtual source MAC address instead of itself
The MAC address of the network card, VRRP runtime only the master router periodically sends VRRP notification information, indicating that master is working properly and virtual
Quasi-router IP (group), BACKUP only receives VRRP data, does not send data, if not received in a certain time the MASTER's notification letter
, each BACKUP will declare itself master, send a notification message, and re-elect the master election status.

three. Preparatory work
Two virtual machines, such as:
192.168.0.121 and 192.168.0.122

four. Start the installation
1. Install the required libraries for Nginx:
Yum install-y gcc gcc-c++ zlib zlib-devel pcre pcre-devel OpenSSL openssl-devel
Analytical:
GCC:GCC compiler
Gcc-c++: C + + compiler for compiling Nginx HTTP module
Zlib, Zlib-devel: For gzip compression of the contents of the HTTP packet
Pcre, Pcre-devel:pcre Library, Nginx rewrite module required
OpenSSL, Openssl-devel: Used to support the transfer of HTTP on the SSL protocol

2, users and user groups:
Groupadd www #创建www用户组
Useradd-g www www-s/sbin/nologin #创建www用户归入www组, and prohibit www user login system
Mkdir-p/alidata/server/nginx #创建nginx安装目录

3. Start the installation:
Cd/alidata/server/source #统一搁置压缩包
wget http://nginx.org/download/nginx-1.9.5.tar.gz #获取nginx源码
TAR-ZXVF nginx-1.9.5.tar.gz #解压
CD nginx-1.9.5 #进入源码目录
./configure–user=www–group=www–prefix=/alidata/server/nginx–with-http_stub_status_module
–with-http_ssl_module–with-pcre
Use./configure–help to see which compilation options are available, nginx modules must be added at compile time and cannot be added dynamically

If this is the case, it will be successful, and the following command will be performed to complete the installation:

Make && make install #编译并安装
The installation is complete.

Note: Both of the above need to be installed.

Start the service without error
Start Ngin: (Make sure no other programs in Linux occupy Port 80)
Cd/alidata/server/nginx #进入安装目录
./sbin/nginx #启动

Five. Install keepalived:
1. Go to the drop-down package directory, as my Cd/alidata/resource
wget http://www.keepalived.org/software/keepalived-1.2.22.tar.gz Start Download
Create directory: Mkdir-p/alidata/server/keepalived
TAR-ZXVF keepalived-1.2.22.tar.gz
CD keepalived-1.2.22
./configure–prefix=/alidata/server/keepalived
Make && make install
CP keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
cp/alidata/server/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
Ln-s/alidata/server/keepalived/sbin/keepalived/usr/sbin/
./keepalived/etc/rc.d/init.d/keepalived Start #启动服务

Above for installation complete
Note: Both units need to be installed.

Six. Modify configuration file Options

! Configuration File forKeepalivedglobal_defs {# keepalived Email alerts are required to turn on the SendMail service. It is recommended to use standalone monitoring or third-party SMTP or to configure mail deliveryrouter_id192.168.0.121}vrrp_script Chk_nginx {Script"/alidata/server/keepalived/nginx_check.sh" #运行脚本, the script content below, is to play an nginx downtime, automatically open the serviceInterval2 #检测时间间隔Weight- - #如果条件成立的话, the weight is -20}# define virtual routes, vi_1 as identifiers for virtual routes, define their own namesVrrp_instance vi_1 {State MASTER#来决定主从Interface eno16777736# BIND Virtual IP network interface, fill in according to your own machinevirtual_router_id121 # Virtual Route ID number, two node settings must be the sameMcast_src_ip192.168.0.121 #填写本机ipPriority - # node priority, mainly higher than from node priorityNopreempt# High Priority setting Nopreempt resolve the problem of re-preemption after exception recoveryAdvert_int1 # Multicast information send interval, two node settings must be the same, default 1sAuthentication {Auth_typePASS Auth_pass1111}# Add Track_script block to instance configuration blockTrack_script {Chk_nginx#执行 Nginx-Monitored services} virtual_ipaddress {192.168.0.50 # Virtual IP, that is, how to solve the Write dead program IP can be switched IP, can also be extended, the use of a wide range. Can be configured with multiple. }}

The above configuration is the primary node configuration, similar to the node, which has been marked with a distinction. Here is the Monitoring service script content

#!/bin/bash-l`if$A-eq0 ];then    /alidata/server/nginx/sbin/nginx    2    if-l-eq0 ];then        killall keepalived    fifi

Read the script, define the variable, this variable is to check the Nginx process script, if the process is 0, start the Nginx service, re-check the Nginx service, if still not started, kill all the keepalived process.

Seven. Test High Availability
Open Browser, enter virtual IP address

I am respectively in two machines equipped with environment, IP for 192.168.0.121 and 192.168.0.122, when I stopped one of the Nginx services, the result


At this point, I stopped one of the keepalived services

Browser display

We open the cmd window to see

Virtual IP corresponds to the physical address of the corresponding real IP address of the physical address of the IP, in turn, to stop the service of another IP keepalived service, and then see the results of the test.

At this point, we have finished the header content. This allows you to extend highly available architectures such as FASTDFS cluster management, LVS load balancing management, and more. If you have any questions, please discuss them together. With time, the following updates the Redis high-availability, high-scale cluster scenario. Fastdfs cluster scheme, LVS Load balancer configuration scheme. There is a mistake in the place of manual writing. Sharing the same time, oneself is also in progress, we all want to become an architect, but that is just a title, let oneself progress a little bit every day is enough.

Keepalived + nginx for highly available cluster scenarios

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.