Nginx + Keepalived for High Availability of hot standby

Source: Internet
Author: User
Tags haproxy

Nginx + Keepalived for High Availability of hot standby

Preparation:

The two ip addresses of the machine are 192.168.100.128 192.168.100.129 (you can use a virtual machine for testing. The Network Mode of the virtual machine is NET and the static ip address is used)

Prepare a virtual ip address to provide external services, that is, access the web application through the ip address. (Keepalive binds the virtual ip address to two machines.) the core principle of keepalived is vrrp.

1. nginx Installation
Both machines need to be installed, you can refer to my previous blog Linux tar package install Nginx http://blog.csdn.net/caicongyang/article/details/46388845

2. Install keepalived (both machines must be installed and the following operations must be performed !)

Download the latest keepalived-1.2.17.tar.gz: http://www.keepalived.org/download.html

Upload the file to the/usr/local/directory.

# Tar-zxvf keepalived-1.2.17.tar.gz

Rename the decompressed folder

# Music keepalived-1.2.17 keeplived

Go to the folder, compile and install

# Cd keepalived
#./Configure
# Make & make install

Modify the configuration file/usr/local/keepalived/etc/keepalived. conf

The host configuration file is as follows:

Global_defs {
Notification_email {
[Email protected] # add multiple email addresses for receiving alerts
}
Notification_email_from [email protected] ### sender address
Smtp_server 127.0.0.1 ### mail sending server
Smtp_connect_timeout 30 ### timeout
Router_id LVS_DEVEL #### ID of the load balancer instance, used for email alerts
}
Vrrp_script chk_http_port {
Script "/opt/nginx_pid.sh" #### script path for detecting nginx status
Interval 2
Weight 2
}
Vrrp_instance VI_1 {
State MASTER ########### BACKUP for auxiliary machines
Interface eth0 #### HA monitoring network interface this item is eth0 by default, because I did a test under the virtual machine, so change it to eth2, you can use ifconfing
Virtual_router_id 51 # The virtual_router_id of the master and slave machines must be the same
Mcast_src_ip 192.168.100.128 ### local IP Address
Priority 100 ########### the weight is higher than the back value.
Advert_int 1 # number of seconds between active and standby notifications
Authentication {
Auth_type PASS ### verification when the master-slave switchover is configured by default
Auth_pass 1111
}
Track_script {
Chk_http_port ### service for monitoring execution
}
Virtual_ipaddress {
192.168.100.20.#### virtual ip address and vip address
}
}


Backup Machine configuration file

Global_defs {
Notification_email {
[Email protected] # add multiple email addresses for receiving alerts
}
Notification_email_from [email protected] ### sender address
Smtp_server 127.0.0.1 ### mail sending server
Smtp_connect_timeout 30 ### timeout
Router_id LVS_DEVEL #### ID of the load balancer instance, used for email alerts
}
Vrrp_script chk_http_port {
Script "/opt/nginx_pid.sh" #### script path for detecting nginx status
Interval 2
Weight 2
}
Vrrp_instance VI_1 {
State BACKUP ########### BACKUP for auxiliary machines
Interface eth2 #### HA Monitoring Network interface note: the default value of this item is eth0. Because I tested it on a virtual machine, I changed it to eth2. You can check your network port first.
Virtual_router_id 51 # The virtual_router_id of the master and slave machines must be the same
Mcast_src_ip 192.168.100.129 ### local IP Address
Priority 90 ########## the weight is higher than the back value.
Advert_int 1 # number of seconds between active and standby notifications
Authentication {
Auth_type PASS ### verification during Master/Slave switchover
Auth_pass 1111
}
Track_script {
Chk_http_port ### service for monitoring execution
}
Virtual_ipaddress {
192.168.100.20.#### virtual ip address and vip address
}
}

Install keepalived as a Linux System Service


# Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/rc. d/init. d/

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

# Mkdir/etc/keepalived

# Cp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/

# Cp/usr/local/keepalived/sbin/keepalived/usr/sbin/

Set startup Service

# Chkconfig -- level 2345 keepalived on

Write the verification script in/opt/,/opt/nginx_pid.sh (already configured in keepalived. conf)

Nginx_pid.sh: If nginx stops running, try to start it. If the keepalived process of the local machine cannot be killed, keepalied binds the virtual ip address to the standby machine.

#! /Bin/bash
A = 'ps-C nginx-no-header | wc-l'
If [$ A-eq 0]; then
/Usr/local/nginx/sbin/nginx # enter the path of your nginx startup command.
Sleep 3
If ['ps-C nginx -- no-header | wc-l'-eq 0]; then
Killall keepalived
Fi
Fi

Test:

Start nginx and keepalived on the two machines respectively

Use commands to check whether the virtual ip address is successfully bound

# Ip

The host result is

1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
Inet 127.0.0.1/8 scope host lo
Inet6: 1/128 scope host
Valid_lft forever preferred_lft forever
2: eth1: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
Link/ether 00: 0c: 29: ff: ca: 24 brd ff: ff
Inet 192.168.100.128/24 brd 192.168.100.255 scope global eth1
<Span style = "color: # ff6666;"> inet 192.168.100.htm/32 scope global eth1 </span>
Inet6 fe80: 20c: 29ff: feff: ca24/64 scope link
Valid_lft forever preferred_lft forever

We can see that the virtual IP address 192.168.100.htm has been bound to the 128 host.

In addition, you can use the nginx process that kills the primary service to access 192.168.100.htm to check whether the web page is still accessible. Then, use the # ip a command to check whether the virtual ip address of the standby machine is successfully bound!


Note: despite the high availability of the hot standby mode, one machine is in the state of being divided, causing a waste of resources. In the next article, I will configure the dual-host dual-master mode.

Haproxy + Keepalived + Apache configuration notes in CentOS 6.3

Haproxy + KeepAlived WEB Cluster on CentOS 6

Keepalived + Haproxy configure high-availability Load Balancing

Haproxy + Keepalived build high-availability Load Balancing

Configure LVS + Keepalived + ipvsadm on CentOS 7

Keepalived high-availability cluster Construction

For more information about Keepalived, click here.
Keepalived: click here

This article permanently updates the link address:

Related Article

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.