Complete steps to achieve high availability with nginx combined with keepalived

Complete steps to achieve high availability with nginx combined with keepalived

Preface

In order to meet the high availability of the system, it is generally necessary to build a cluster. When the host crashes, our system can continue to provide services. The same is true when we use nginx as a reverse proxy and dynamic and static separation server. Achieving high availability of the system is the focus of our programmers. This article introduces how to use nginx and keepalived to build a high-availability cluster in master-slave mode.

Prerequisite Knowledge

This article does not introduce too much about nginx configuration, and assumes that readers already have some knowledge about nginx.

Introduction of keepalived

The function of Keepalived is to detect the status of the server. If a web server goes down or fails to work, Keepalived will detect it and remove the faulty server from the system, and use other servers to replace the server. When the server works normally, Keepalived automatically adds the server to the server group. All these tasks are completed automatically without human intervention. The only thing that needs to be done manually is to repair the faulty server.

System architecture diagram

Implementation steps

1. Environment Configuration

1.1 Install nginx

Install related dependencies

yum install pcre-devel zlib zlib-devel openssl openssl-devel

Upload and decompress the compressed package

tar zxvf nginx-1.12.2.tar.gz

Create a directory and test the configuration

mkdir -p /usr/local/nginx
./configure --prefix=/usr/local/nginx

Precompile and install

make && make install

Start and stop related commands

cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s start

1.2 Install keepalived

yum install -y keepalived

2. Host configuration

Modify the keepalived configuration file

vim /etc/keepalived/keepalived.conf

The modified contents are as follows

# Newly added configuration vrrp_script chk_http_port {
 script "/shell/nginx_check.sh" #script address interval 2 #check script execution interval weight 2 #weight}

vrrp_instance VI_1 {
 state MASTER #The master server is MASTER, the slave server is BACKUP
 interface eth0 #Network card virtual_router_id 51 #The virtual_router_id of the master and backup machines must be the same priority 100 #Different priorities for the master and backup machines. The master has a higher priority and the backup has a lower priority. The default is 100
 advert_int 1
 authentication
  auth_type PASS
  auth_pass 1111
 }
 #Virtual IP address virtual_ipaddress {
  192.168.126.88
 }
}

Create a script file

mkdir /shell/nginx_check.sh
vim /shell/nginx_check.sh

The script file contents are as follows

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
  /usr/local/nginx/sbin/nginx
  sleep 2
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
    killall keepalived
  fi
fi

3. Slave configuration

Modify the keepalived configuration file

vrrp_instance VI_1 {
  state BACKUP #The master server is MASTER, the slave server is BACKUP
  interface eth0 #Network card virtual_router_id 51 #The virtual_router_id of the master and backup machines must be the same priority 50 #Different priorities for the master and backup machines, the master has a higher priority and the backup has a lower value advert_int 1
  authentication
    auth_type PASS
    auth_pass 1111
  }
  #Virtual IP address virtual_ipaddress {
    192.168.126.88
  }
}

4. Start the service

start up

service keepalived start

View the virtual IP address

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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:4f:31:ce brd ff:ff:ff:ff:ff:ff
  inet 192.168.126.100/24 ​​brd 192.168.126.255 scope global eth0
  inet 192.168.126.88/32 scope global eth0
  inet6 fe80::20c:29ff:fe4f:31ce/64 scope link 
    valid_lft forever preferred_lft forever

5. Turn off the firewall test

Turn off firewall

service iptables stop
# Check whether the firewall starts automatically at boot time chkconfig --list | grep iptables
chkconfig iptables off

Log in to the virtual IP address to view

http://192.168.126.88/

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of nginx+keepalived high availability master-slave configuration
  • How to combine keepalived with nginx to achieve high availability of nginx
  • Example of how to implement keepalived+nginx high availability
  • Detailed explanation of Keepalived+Nginx to achieve high availability (HA)
  • Keepalived implements high availability of nginx
  • Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

<<:  Detailed explanation of the pitfalls of mixing npm and cnpm

>>:  MySQL 5.5.56 installation-free version configuration method

Recommend

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

1: Download from mysql official website https://d...

Implementation of Mysql User Rights Management

1. Introduction to MySQL permissions There are 4 ...

Using CSS3 to implement font color gradient

When using Animation.css, I found that the font o...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...

vue-element-admin global loading waiting

Recent requirements: Global loading, all interfac...

Detailed explanation of creating and calling MySQL stored procedures

Table of contents Preface Stored Procedure: 1. Cr...

CSS3 simple cutting carousel picture implementation code

Implementation ideas First, create a parent conta...