Example of how to implement keepalived+nginx high availability

Example of how to implement keepalived+nginx high availability

1. Introduction to keepalived

Keepalived was originally designed for LVS load balancing software to manage and monitor the status of each service node in the LVS cluster system. Later, the VRRP function was added to achieve high availability. In addition to managing LVS software, keepalived can also support high availability solutions for other services.

keepalived achieves high availability through the VRRP protocol. VRRP (Virtual Router Redundancy Protocol) virtual routing redundancy protocol. The purpose of VRRP is to solve the single point failure problem of static routing. It can ensure that the entire network can run uninterruptedly when individual nodes fail.

2. Keepalived high availability failover principle

Failover between keepalived high-availability services is achieved through VRRP. When the keepalived service is working, the main Master node will continuously send (multicast) heartbeat messages to the backup node to tell the backup node that it is still alive.

When the primary node fails, it cannot send heartbeat messages to the standby node. If the standby node cannot continue to detect the heartbeat from the primary node. It will call its own takeover program to take over the IP resources and services of the main node. When the master node recovers, the standby node will release the IP resources and services that it took over when the master node failed, and return to its original standby role.

3. Install nginx

3.1. Master node (192.168.80.22)

3.1.1. Installing compilation tools and library files

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

3.1.2. Install pcre

#Enter the directory cd /usr/local/develop/anginx

#Upload the installation file and decompress it tar -zxvf pcre-8.38.tar.gz

#Enter the installation directory cd pcre-8.38

# Check configuration ./configure

#Compile and install make && make install

# View the pcre version pcre-config --version

3.1.3. Install nginx

#Enter the directory cd /usr/local/develop/anginx
​
#Upload the installation file and decompress it tar -zxvf nginx-1.8.1.tar.gz
​
#Enter the installation directory cd nginx-1.8.1
​
# Check configuration ./configure --prefix=/usr/local/develop/anginx/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/develop/anginx/pcre-8.38
​
#Compile and install make && make install
​
# View the nginx version /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
--------------------------------------------------------
[root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.8.1
​
#Configure nginx (check)
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -t
​
#nginx management command /usr/local/develop/anginx/webserver/nginx/sbin/nginx # Start Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop # Stop Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload # Reload the configuration file /usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen # Restart Nginx

3.1.4.nginx basic configuration

vi nginx.conf

#user nobody;
worker_processes 1;
​
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
​
pid logs/nginx.pid;
​
​
events {
  worker_connections 1024;
}
​
​
http {
  include mime.types;
  default_type application/octet-stream;
​
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';
​
  access_log logs/access.log main;
​
  sendfile on;
  #tcp_nopush on;
​
  #keepalive_timeout 0;
  keepalive_timeout 65;
​
  #gzip on;
  
  #Add tomcat list, the real application servers are placed here upstream tomcat_pool{
    #server tomcat address: port number weight indicates weight, the larger the weight, the greater the probability of being assigned;
    server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
    server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s;
    
  }
​
  server {
    listen 80;
    server_name tomcat_pool;
​
    #charset koi8-r;
​
    #access_log logs/host.access.log main;
​
    location / {
      #root html;
      #index index.html index.htm;
      proxy_pass http://tomcat_pool; #direct to tomcat to process proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
​
    #error_page 404 /404.html;
​
    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }
​
​
}

3.2. Standby node (192.168.80.21)

Note: The installation method is the same as that of the nginx master node.

4. Install keepalived

4.1. Master node (192.168.80.22)

#Install keepalived
yum install keepalived -y
​
#Start the keepalived service /etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root 15723 1 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15724 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15725 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived
[root@hadoop02 anginx]#
​
#Set the system to start automatically echo "/etc/init.d/keepalived start" >>/etc/rc.local
​
#Shut down the keepalived service /etc/init.d/keepalived stop
​
#Edit the keepalived configuration file vi /etc/keepalived/keepalived.conf
​
-----------------------------------------------------------
! Configuration File for keepalived
​
global_defs {
  notification_email {
   [email protected]
   [email protected]
   [email protected]
  }
  notification_email_from [email protected]
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id lb01
}
​
vrrp_instance VI_1 {
  state MASTER
  interface eth1
  virtual_router_id 55
  priority 150
  advert_int 1
  authentication
    auth_type PASS
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
............................................................

About configuration instructions:

  • [router_id] is the router identifier, which should be unique within a LAN.
    • 【vrrp_instance VI_1】{...}This is a VRRP instance, which defines the master/slave status, interface, priority, authentication, and IP information of keepalived
    • [state] defines the role of VRRP
    • [interface] defines the interface to be used. Here, the network card used by my server is eth1.
    • [virtual_router_id] is the virtual router ID. The master and backup in a group of keepalived configurations are set to the same
    • [priority] is the priority. The larger the number, the higher the priority.
    • 【auth_type】is the authentication method
    • 【auth_pass】is the authentication password
  • 【virtual_ipaddress】 {...} Defines a virtual IP address. You can configure multiple IP addresses. Here I define it as 192.168.80.100, which is bound to the network interface eth1, virtual interface eth1:1

4.2. Standby node (192.168.80.21)

#Install keepalived
yum install keepalived -y
​
#Start the keepalived service /etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root 15723 1 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15724 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15725 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived
[root@hadoop02 anginx]#
​
#Set the system to start automatically echo "/etc/init.d/keepalived start" >>/etc/rc.local
​
#Shut down the keepalived service /etc/init.d/keepalived stop
​
#Edit the keepalived configuration file vi /etc/keepalived/keepalived.conf
​
-----------------------------------------------------------------
! Configuration File for keepalived
​
global_defs {
  notification_email {
   [email protected]
   [email protected]
   [email protected]
  }
  notification_email_from [email protected]
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id lb02
}
​
vrrp_instance VI_1 {
  state BACKUP
  interface eth1
  virtual_router_id 55
  priority 100
  advert_int 1
  authentication
    auth_type PASS
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
.............................................................
​
​

5. Testing

5.1. Start the keepalived service of the master and standby nodes

#Execute on node 1 (192.168.80.22)
/etc/init.d/keepalived start
-------------------------------------
[root@hadoop02 anginx]# ps -ef |grep keepalived
root 15788 1 0 01:09 ? 00:00:00 /usr/sbin/keepalived -D
root 15790 15788 0 01:09 ? 00:00:00 /usr/sbin/keepalived -D
root 15791 15788 0 01:09 ? 00:00:00 /usr/sbin/keepalived -D
root 15807 15622 0 01:33 pts/1 00:00:00 grep keepalived
[root@hadoop02 anginx]#
​
​
#Execute on node 2 (192.168.80.21)
/etc/init.d/keepalived start
---------------------------------------
[root@hadoop01 ~]# ps -ef |grep keepalived
root 11542 1 0 01:30 ? 00:00:00 /usr/sbin/keepalived -D
root 11544 11542 0 01:30 ? 00:00:00 /usr/sbin/keepalived -D
root 11545 11542 0 01:30 ? 00:00:00 /usr/sbin/keepalived -D
root 11550 11512 0 01:33 pts/1 00:00:00 grep keepalived
[root@hadoop01 ~]#

5.2. Access services through virtual IP

http://192.168.80.100/session-redis-demo/

5.3. Stop the keepalived service on the master node

#Execute on node 1 (192.168.80.22)
/etc/init.d/keepalived stop
​
#Observe the changes in the standby node ip addr
-------------------------------------------
[root@hadoop01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 ~]#

5.4. Continue to access the service through the virtual IP

http://192.168.80.100/session-redis-demo/

6.Keepalived+nginx integration

Description: Write an nginx daemon script. If the nginx service fails, stop the keepalived service of the current node. Automatically switch to the standby node.

6.1. Write nginx daemon script

vi nginx_check.sh
​
--------------------------------------
#!/bin/bash
while true
do
if [ $(netstat -tlnp | grep nginx | ​​wc -l) -ne 1 ]
then
  /etc/init.d/keepalived stop
fi
sleep 2
done
​
#Authorize the script chmod u+x nginx_check.sh
​
#Execute script nohup /usr/local/develop/anginx/shell/nginx_check.sh &

6.2. Stop the master node nginx service

#Stop the master node nginx service /usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop
​
#Find process [root@hadoop02 ~]# ps -ef |grep nginx
root 15915 1 0 01:51 ? 00:00:00 /bin/bash /usr/local/develop/anginx/shell/nginx_check.sh
root 16516 15753 0 01:54 pts/5 00:00:00 grep nginx
[root@hadoop02 ~]#
​
#Observe the changes of standby nodes [Service is normal]
ip addr
--------------------------------------
[root@hadoop01 shell]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 shell]#
​
#Restart the master node nginx and keepalived service again /usr/local/develop/anginx/webserver/nginx/sbin/nginx
​
/etc/init.d/keepalived start

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • HAProxy+Keepalived to achieve high availability load balancing (theoretical part)
  • How to refresh data of keepalive page in vue cache
  • Implementation of KeepAlive interface in jetcd, the official repository of java client Etcd

<<:  How to install MySQL and enable remote connection on cloud server Ubuntu_Server_16.04.1

>>:  How to implement JavaScript's new operator yourself

Recommend

How to uninstall Linux's native openjdk and install sun jdk

See: https://www.jb51.net/article/112612.htm Chec...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...

Detailed steps for smooth transition from MySQL to MariaDB

1. Introduction to MariaDB and MySQL 1. Introduct...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...

js realizes the image cutting function

This article example shares the specific code of ...

How to assign a public IP address to an instance in Linux

describe When calling this interface, you need to...

Develop calculator example code using native javascript

The main function of a calculator is to perform n...

Summary of various forms of applying CSS styles in web pages

1. Inline style, placed in <body></body&g...

Use ab tool to perform API stress test on the server

Table of contents 1 A brief introduction to syste...

Several reasons for not compressing HTML

The reason is simple: In HTML documents, multiple ...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...