Implementation process of nginx high availability cluster

Implementation process of nginx high availability cluster

This article mainly introduces the implementation process of nginx high-availability cluster. The example code in this article is very detailed and has a certain reference value for everyone's study or work. Friends in need can refer to it.

1. Configuration:

(1) Two nginx servers are required (2) Keepalived is required (3) A virtual IP is required

2. Preparation for high availability configuration

(1) Two servers are required: 192.168.180.113 and 192.168.180.112 (2) Install nginx on both servers (3) Install keepalived on both servers

3. Install keepalived on two servers

(1) Install using the yum command

(2) After installation, a directory called keepalived is created in etc, and a file called keepalived.conf is created.

[root@topcheer dev]# yum install keepalived -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * epel: ftp.riken.jp
 * extras: mirrors.cn99.com
 * updates: mirror.lzu.edu.cn
gitlab_gitlab-ce/x86_64/signature | 836 B 00:00:00
gitlab_gitlab-ce/x86_64/signature | 1.0 kB 00:00:00 !!!
gitlab_gitlab-ce-source/signature | 836 B 00:00:00
gitlab_gitlab-ce-source/signature | 951 B 00:00:00 !!!
Resolving dependencies--> Checking transactions---> Package keepalived.x86_64.0.1.3.5-16.el7 will be installed--> Processing dependency libnetsnmpmibs.so.31()(64bit), which is needed by package keepalived-1.3.5-16.el7.x86_64--> Processing dependency libnetsnmpagent.so.31()(64bit), which is needed by package keepalived-1.3.5-16.el7.x86_64--> Checking transactions---> Package net-snmp-agent-libs.x86_64.1.5.7.2-43.el7 will be installed--> Dependency resolution completed​
Dependency resolution
==========================================================================================================================================
 Package Architecture Version Source Size === ...
Installing:
 keepalived x86_64 1.3.5-16.el7 base 331 k
Install for dependencies:
 net-snmp-agent-libs x86_64 1:5.7.2-43.el7 base 706 k
​
Transaction Summary= ...
Install 1 package (+1 dependent package)
​
Total downloads: 1.0M
Installation size: 3.0 M
Downloading packages:
(1/2): net-snmp-agent-libs-5.7.2-43.el7.x86_64.rpm | 706 kB 00:00:00
(2/2): keepalived-1.3.5-16.el7.x86_64.rpm | 331 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------------
Total 1.8 MB/s | 1.0 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Installing: 1:net-snmp-agent-libs-5.7.2-43.el7.x86_64 1/2
 Installing: keepalived-1.3.5-16.el7.x86_64 2/2
 Verifying: keepalived-1.3.5-16.el7.x86_64 1/2
 Verifying: 1:net-snmp-agent-libs-5.7.2-43.el7.x86_64 2/2
​
Installed:
 keepalived.x86_64 0:1.3.5-16.el7
​
Installed as a dependency:
 net-snmp-agent-libs.x86_64 1:5.7.2-43.el7
​
complete!

4. Modify the configuration file

Both need to be replaced, one master and one slave

[root@topcheer keepalived]# cat keepalived.conf
global_defs {
  notification_email {
  [email protected]
  [email protected]
  [email protected]
  }
  notification_email_from [email protected]
  smtp_server 192.168.180.113
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_script chk_http_port {
  script "/usr/local/src/nginx_check.sh"
  interval 2 # (interval between detection script execution)
  weight 2
  }
  vrrp_instance VI_1 {
   state BACKUP # Change MASTER to BACKUP on the backup server
   interface ens33 //Network card virtual_router_id 51 # The virtual_router_id of the master and backup machines must be the same priority 40 # The master and backup machines have different priorities, the master has a larger value and the backup has a smaller value advert_int 1
  authentication
   auth_type PASS
   auth_pass 1111
  }
  virtual_ipaddress {
  192.168.180.114 // VRRP H virtual address}
}

script:

[root@topcheer src]# cat nginx_check.sh
#!/bin/bash
A=`ps -C nginx ▒Cno-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[root@topcheer src]#

Start nginx and keepalived on both servers

Start nginx: ./nginx Since I installed gitlab's own nginx on 112, I don't need to start it. Start keepalived: systemctl start keepalived.service

5. Testing

Turn off nginx and keepalived on 113 and find that it will switch to 112

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:
  • Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster
  • Nginx implements high availability cluster construction (Keepalived+Haproxy+Nginx)

<<:  How to use webSocket to update real-time weather in Vue

>>:  How to add, delete and modify columns in MySQL database

Recommend

Next.js Getting Started Tutorial

Table of contents Introduction Create a Next.js p...

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

URL Rewrite Module 2.1 URL Rewrite Module Rule Writing

Table of contents Prerequisites Setting up a test...

Implementation of master-slave replication in docker compose deployment

Table of contents Configuration parsing Service C...

Thirty HTML coding guidelines for beginners

1. Always close HTML tags In the source code of p...

CentOS installation mysql5.7 detailed tutorial

This article shares the detailed steps of install...

Solve the problem that the docker container cannot ping the external network

Today, when I was building a redis environment in...

MySQL Index Detailed Explanation

Table of contents 1. Index Basics 1.1 Introductio...

Vue2 implements provide inject to deliver responsiveness

1. Conventional writing in vue2 // The parent com...

MySQL multi-table join introductory tutorial

Connections can be used to query, update, and est...