Nginx + Keepalived dual-master configuration

Source: Internet
Author: User
1. in the Keepalived + Nginx High-reliability load balancing architecture, keepalived is responsible for implementing the High-availability (HA) function to control the VIP (virtual network address) of the front-end machine ), when a device fails, the hot standby server can automatically switch the VIP address in an instant. in actual operation, there is only two seconds to switch. the DNS service can be responsible for the load balancing of the front-end VIP address. 2. Topology

I. Architecture

In the Keepalived + Nginx High-reliability load balancing architecture, keepalived is responsible for implementing the High-availability (HA) function to control the VIP address (virtual network address) of the front-end machine. when a device fails, the hot standby server can automatically switch the VIP address in an instant. in actual operation, the experience is only two seconds. the DNS service can be responsible for load balancing of the front-end VIP address.

2. Topology

 

III. simple principle

Nginx-Keepalived-M (nginx master server) and Nginx-Keepalived-S (backup server) both use the keepalived software to bind the eth0 Nic to a virtual IP address (VIP1: 172.16.1.20, VIP1 is bound to the eth0 that carries the service. when Nginx-Keepalived-M fails, nginx-Keepalived-S will pass through/etc/keepalived. the heartbeat time set in the conf file advert_int 1 is checked, and the Nginx-Keepalived-M status cannot be obtained. then, the system switches to Nginx-Keepalived-S instantly to achieve hot dual-machine load balancing, when Nginx-Keepalived-M is restored, keepalived rebinds the VIP 1 address 172.16.1.20 to the eth0 Nic of Nginx-Keepalived-M through the priority parameter. Similarly, the virtual IP address (VIP2) 172.16.1.21 regards the previous Nginx-Keepalived-S as the master server and Nginx-Keepalived-M as the slave server, to achieve load balancing between hot and standby servers.

IV. environment

1. Centos6.3 Mini x64 (Nginx-Keepalived-M) IP: 172.16.1.22 Vip: 172.16.1.20

2. Centos6.3 Mini x64 (Nginx-Keepalived-S) IP: 172.16.1.23 Vip: 172.16.1.21

V. installation and configuration

Install Nginx and Keepalived in Nginx-Keepalived-M and nginx-keepalived-S, respectively. the installation process is skipped.

1. software installation path

/Usr/local/nginx

/Usr/local/keepalived

# Cp/usr/local/keepalived/etc/keepalived

2. keepalived configuration of the nginx master server

# Vi/etc/keepalived. conf

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

! Configuration File for keepalived
# Written byglobal_defs {
Router_id Nginx_Id_1
}

Vrrp_script chk_nginx {
Script "/usr/local/keepalived/etc/check_http.sh" # define the detection script
Interval 2 # detection interval
Weight 2
}
Vrrp_instance Nginx1 {# define an instance
State MASTER # defined as master
Interface eth0
Virtual_router_id 51 #0-255 must be consistent across the vrrp in the same instance
Priority 150 # priority. the master with the highest priority
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {# Check the script
Chk_nginx
}
Virtual_ipaddress {# floating IP address of this instance
172.16.1.20
}
}
Vrrp_instance Nginx2 {
State BACKUP
Interface eth0
Virtual_router_id 52
Priority110
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
172.16.1.21
}
}

Save and exit

---------------------?

Check script:

# Vi/usr/local/keepalived/etc/check_http.sh

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

#! /Bin/bash
If ["$ (ps-ef | grep" nginx: master process "| grep-v grep)" = ""]

Then
Killall-9 keepalived

Fi

Save and exit

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

3. keepalived configuration of the nginx backup server

Vi/etc/keepalived. conf

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

! Configuration File for keepalived
# Written byglobal_defs {
Router_id Nginx_Id_2
}
Vrrp_script chk_nginx {
Script "/usr/local/keepalived/etc/check_http.sh"
Interval 2
Weight 2
}
Vrrp_instance Nginx1 {
State BACKUP
Interface eth0
Virtual_router_id 51
Priority110
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
172.16.1.20
}
}
Vrrp_instance Nginx2 {
State MASTER
Interface eth0
Virtual_router_id 52
Priority150
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Chk_nginx
}

Virtual_ipaddress {
172.16.1.21
}
}

 

VI. Summary

Nginx + keepalived dual-host mutual backup ends.

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.