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

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

How to use jsx syntax correctly in vue

Table of contents Preface Virtual DOM What is Vir...

MySQL date functions and date conversion and formatting functions

MySQL is a free relational database with a huge u...

Install multiple versions of PHP for Nginx on Linux

When we install and configure the server LNPM env...

Ubuntu 20.04 CUDA & cuDNN Installation Method (Graphical Tutorial)

CUDA installation download cuda Enter the nvidia-...

Apache Bench stress testing tool implementation principle and usage analysis

1: Throughput (Requests per second) A quantitativ...

Detailed explanation of nmcli usage in CentOS8

Common nmcli commands based on RHEL8/CentOS8 # Vi...

Use ab tool to perform API stress test on the server

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

How to use JSZip compression in CocosCreator

CocosCreator version: 2.4.2 Practical project app...

40 web page designs with super large fonts

Today's web designs tend to display very larg...

Using vsftp to build an FTP server under Linux (with parameter description)

introduce This chapter mainly introduces the proc...

Add a floating prompt for the header icon in the ElementUI table

This article mainly introduces how to add floatin...

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...