Nginx + keepalived Dual machine hot standby

Source: Internet
Author: User

Read Catalogue

    • Order
    • Nginx
    • Keepalived
    • Test
    • Problem: After killing the keepalived process, the VIP offset can be achieved, but the original machine's VIP cannot be deleted automatically
Back to the top of the sequence

Dual-machine hot standby means that two machines are in operation, but not both machines are serving at the same time.
When one of the service providers fails, the other one will automatically take over and provide the service immediately, and the switch time is very short.

Keepalived works by vrrp--Virtual Routing Redundancy protocol.

The test environment is as follows:

Ip

Vip

Master

192.168.174.135

192.168.174.140

Backup

192.168.174.137

192.168.174.140

Back to TopNginx

Installation

Install Nginx 

Find profile Location

Find/-name nginx.conf/etc/nginx/nginx.conf

Modifying a configuration file (nginx.conf)

User www-Data;worker_processes4;p id/run/nginx.pid;events {worker_connections1024; Types_hash_max_size 2048mime.types; Default_type application/octet-stream; Access_log/var/log/nginx/access.log; Error_log/var/log/nginx/error.log; server {listen 80< Span style= "color: #000000;" > Default_server; server_name test; CharSet Utf-8; location/  {root HTML; index index.html index.htm; Proxy_set_header x real_ip $remote _addr; client_max_body_size 100m;} }}

File/usr/share/nginx/html/index.html

Add

Add

Start

sudo service nginx start  

Shut down

sudo service nginx stop
Back to Topkeepalived

Installation

Download keepalived-1.2.19.tar.gz

TAR–ZXVF keepalived-1.2.  tar. GZCD keepalived-1.2.  /configure--prefix=/usr/local/keepalived makeinstall       

Problems may occur during:

!!! OpenSSL is not a properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!

Solve

Install Libssl.dev

Create a soft link

Ln-s/usr/local/keepalived/sbin/keepalived/sbin/ln-s/usr/local/keepalived/etc/rc.d/init.d/keepalived/ etc/init.d/ln-s/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/ 

Start

sudo keepalived-d-f/usr/local/keepalived/etc/keepalived/keepalived.conf

Shut down

Killall keepalived

Configuration (keepalived.conf):

global_defs {    router_id nodea}vrrp_instance vi_1 {State    MASTER     interface eth0        #监测网络接口     50 The   #主 must be the same as                #优先级: Master and prepare    11111192.168.  174.140/ #VRRP ha virtual address}}         

Configuration of alternate nodes

global_defs {   router_id nodeb}vrrp_instance vi_1 {State    BACKUP    interface eth0        90      11111192.168.  174.140/ }}         
Back to top of the test

Double-click Hot Spare

Both devices start Nginx and keepalived, the browser each access

Browser access: http://192.168.174.140/, which shows the master page.

The same IP appr can be used to verify:

135 Machine:

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN Group Default

Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00

inet 127.0.0.1/8 Scope host Lo

Valid_lft Forever Preferred_lft Forever

INET6:: 1/128 Scope Host

Valid_lft Forever Preferred_lft Forever

2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up group default Qlen 1000

Link/ether 00:0c:29:39:d4:88 BRD FF:FF:FF:FF:FF:FF

inet 192.168.174.135/24 BRD 192.168.174.255 Scope Global eth0

Valid_lft Forever Preferred_lft Forever

inet 192.168.174.140/24 scope Global secondary eth0

Valid_lft Forever Preferred_lft Forever

Inet6 FE80::20C:29FF:FE39:D488/64 Scope link

Valid_lft Forever Preferred_lft Forever

137 Machine:

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN Group Default

Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00

inet 127.0.0.1/8 Scope host Lo

Valid_lft Forever Preferred_lft Forever

INET6:: 1/128 Scope Host

Valid_lft Forever Preferred_lft Forever

2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU qdisc pfifo_fast State UNKNOWN Group default Qlen 1000

Link/ether 00:0c:29:cf:23:62 BRD FF:FF:FF:FF:FF:FF

inet 192.168.174.137/24 BRD 192.168.174.255 Scope Global eth0

Valid_lft Forever Preferred_lft Forever

Inet6 FE80::20C:29FF:FECF:2362/64 Scope link

Valid_lft Forever Preferred_lft Forever

Now turn off the keepalived of the 135 machine.

But when Nginx is down or the entire machine goes down, this is not the case-access to 192.168.174.140 through the browser does not access resources.

Nginx Down/Machine down hot standby

In order to solve the previous problem, the script can be used to automatically shut down the keepalived process when the nginx process is detected, thus implementing a hot backup.

Configuration of the master node

global_defs {router_id nodea}vrrp_script chk_http_port {script"/home/jimite/keepalived/chk_nginx_pid.sh " interval 2 weight 2< Span style= "color: #000000;" >}vrrp_instance vi_1 {State MASTER interface eth0 virtual_router_id  priority 100 Advert_int 1 authentication {Auth_type PASS auth_pass 1111192.168.174.140/24< Span style= "color: #000000;" > }} 

Configuration of alternate nodes

global_defs {router_id nodeb}vrrp_script chk_http_port {script"/home/jihite/keepalived/chk_nginx_pid.sh " interval 2 weight 2< Span style= "color: #000000;" >}vrrp_instance vi_1 {State BACKUP interface eth0 virtual_router_id 50 priority 90 Advert_int 1 authentication {Auth_type PASS auth_pass 1111192.168.174.140/24< Span style= "color: #000000;" > }} 

where/home/jimite/keepalived/chk_nginx_pid.sh is

#!/bin/basha= 'ps-c nginx--no-header |  WC-l '0 ]'nginx server is diedkillall keepalivedfi     
Back to the top: after killing the keepalived process, you can achieve the VIP offset, but the original machine's VIP cannot automatically delete the reason: The VRRP protocol principle is: only master sends the message externally. Each backup accepts a message, and the new master is selected in the remaining backup machine when the message is not received. Before using kill-9 pid or killall pid to kill the keepalived process, causing the installation keepalived Unable to send information, backup received information upgrade to master, but because the process was killed "abnormal shutdown", resulting in keepalived no ability to delete itself VIP. Solution: Turn off keepalived with the command service keepalived stop or kill-15 pid (Note: Delete only the first process number) there is a problem: abnormal shutdown keepalived. Use kill-9 or Killall to kill keepalived.

Nginx + keepalived Dual machine hot standby

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.