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

Example code for implementing anti-shake in Vue

Anti-shake: Prevent repeated clicks from triggeri...

MySQL uninstall and install graphic tutorial under Linux

This is my first time writing a blog. I have been...

Summary of pitfalls in virtualbox centos7 nat+host-only networking

Table of contents 1. Problem Background 2. What a...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

Detailed explanation of various loop speed tests in JS that you don’t know

Table of contents Preface 1. for loop 2. while lo...

MySQL multi-instance configuration application scenario

Table of contents MySQL multiple instances Multi-...

Play and save WeChat public account recording files (convert amr files to mp3)

Table of contents Audio transcoding tools princip...

Summary of some efficient magic operators in JS

JavaScript now releases a new version every year,...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

How to recover files accidentally deleted by rm in Linux environment

Table of contents Preface Is there any hope after...

How to prevent duplicate submission in jquery project

In new projects, axios can prevent duplicate subm...

Summary of pitfalls in importing ova files into vmware

Source of the problem As we all know, all network...

Node.js makes a simple crawler case tutorial

Preparation First, you need to download nodejs, w...

A method of hiding processes under Linux and the pitfalls encountered

Preface 1. The tools used in this article can be ...

How to query date and time in mysql

Preface: In project development, some business ta...