Detailed explanation of how to use Nginx + consul + upsync to achieve dynamic load balancing

Detailed explanation of how to use Nginx + consul + upsync to achieve dynamic load balancing

Prerequisites

DNS domain name resolution process

  • Enter the domain name in the browser and visit
  • Check the browser cache for the corresponding IP and port. If there is a direct access to the corresponding IP and port
  • If there is no corresponding one in the browser cache, check the local host file to see if there is one.
  • If there is no local host file, search on the DSN server.

External network mapping

As the name implies, it maps the local IP address to a public IP address, which can be accessed by all hosts (computers connected to the Internet)

  • Question: Why do I need to use external network mapping?

For example, when you make a third-party interface callback for WeChat payment, the interface that WeChat calls back to you must be a public IP address, otherwise it will not be able to call you back at all, resulting in your inability to test locally. At this time, you need an external network mapping to map your IP address to a public network (a network accessible to the host)

  • Question: How to achieve external network mapping?

Use third-party tools to complete external network mapping, such as natapp, ngrok. For specific instructions, visit the official website.
ngrok official website.
natapp official website.(recommended)

nginx core knowledge

What is nginx

nginx is a lightweight web server/reverse proxy server that is small and supports very high concurrency.

Application Scenario

http server: used as static server and image server. Virtual host configuration: split a server into multiple website deployments. (You can complete the virtual host configuration by configuring different domain name mappings or different ports)
Reverse proxy: Use reverse proxy to hide the real IP address <br /> Reverse proxy application scenarios:
You have three servers providing different services. At this time, you can use nginx to configure domain name mapping to complete access to the three different servers, or through ports. The real IP addresses of the three servers can be hidden. Nginx forwards the packets to different servers internally, and the packet capture tool can only obtain the IP address of the Nginx server (without the base point).
Load balancing <br /> Security configuration uses Nginx to build an API interface gateway to solve the cross-domain problem of the website and separate the static and dynamic resources of the website to prevent DDOS (traffic attacks)

Glossary

Layer 4 load balancing

Runs at the transport layer, often using the TCP protocol

Layer 7 load balancing

Runs at the application layer, often using the Http protocol

Load Balancing

Function: It can reduce the pressure of a single server, failover (retry mechanism) If a server goes down, directly poll the next server, health check, use upstream to configure the upstream server in nginx,

Failover

If you access one of the servers and find that the service is down or there is a delay, you can configure failover to switch directly to another server.

### Time to connect to the upstream server proxy_connect_timeout 1s;
		### Send request timeout proxy_send_timeout 1s;
		### Accept the corresponding timeout proxy_read_timeout 1s;

Dynamic load balancing

The configuration in the upstream is no longer static, but is dynamically pulled from the registration center to achieve dynamic load balancing.

Nginx + consul + upsync completes dynamic load balancing

1. Install the basic environment of centos7

# Basic commands # Install the netstat command yum install netstat
 # View all TCP port usage netstat -ntlp
 # View the current service process ps -ef Optional: [ | grep nginx]
 # Force kill the program corresponding to the port kill -9 pid process number # Configure yum source yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
# Install the basic environment yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
yum install wget
yum install unzip

2. Install consul (service registration and discovery)

# Download its installation package, the -c in wget -c means breakpoint continuation wget -c https://releases.hashicorp.com/consul/1.8.5/consul_1.8.5_linux_amd64.zip
# Unzip the compressed package upzip unzip consul_1.8.5_linux_amd64.zip
# Execute the ./consul command. If the line Usage: consul [--version] [--help] <command> [<args>] and the following parameters appear, it means that there is no problem with this consul./consul
# Turn off the firewall systemctl stop firewalld
# Start consul, fill in the IP address of your computer, if it is a virtual machine, fill in the IP address of the virtual machine, centos can use ip addr to view the IP address, window to view the IP address with ipconfig
./consul agent -dev -ui -node=consul-dev -client=192.168.254.134
# Of course, you can also use the background running method, so that the output log returns to the nohup.out file at the same level nohup ./consul agent -dev -ui -node=consul-dev -client=192.168.254.134 &
# Access consul's web page 192.168.254.134:8500

# Check and close the background process of consul. The 19854 I wrote here is the process pid queried by jobs -l
jobs -l
kill -9 19854

3. Install nginx and add upsync module to it

# Download the upsync module and unzip it wget -c https://github.com/weibocom/nginx-upsync-module/archive/master.zip
unzip nginx-upsync-module-master.zip
# Download nginx
wget -c http://nginx.org/download/nginx-1.9.9.tar.gz
# Unzip to the current directory tar -zxvf nginx-1.9.9.tar.gz
# Configure an nginx user and user group. -s /sbin/nologin nginx means that the user cannot log in to the host. groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
# These two folders will be specified when compiling nginx mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
# Enter the directory of the unzipped folder of nginx cd nginx-1.9.9 
# Compile nginx, --prefix represents the directory where nginx is installed. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
make && make install
## Enter the directory where nginx was just installed, that is, the /usr/local/nginx directory, and enter the conf directory to edit the file contents of the conf directory upstream myserver {
 server 127.0.0.1:11111;# This is fixed, ignore it# springbootserver: key value, upsync_timeout timeout 3 seconds, upsync_interval interval, upsync_type type consul, strong_dependency enhanced dependency upsync 192.168.254.134:8500/v1/kv/upstreams/springbootserver upsync_timeout=3000ms upsync_interval=500ms upsync_type=consul strong_dependency=off;
 #Put the pulled configuration file in the following configured directory upsync_dump_path /usr/local/nginx-1.9.9/conf/upsync_dump.conf;

 }
 #Specify the location in server as the upstream server just created
 location / {
 proxy_pass http://myserver;
 index index.html index.htm;
 }
# Enter the sbin directory and start nginx
./nginx

4. Conduct testing

  • The machine starts two services, 8080 and 8081, with the IP address of 192.168.0.116
  • Use the Linux command to specify 2 key values ​​for consul. 192.168.254.134 is the IP address of consul.
# Use curl request, it must be a put request curl -X PUT http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:8080

curl -X PUT http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:8081

#Or use postman to call the API provided by consul to add key and value
http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:8081

# You can even use the graphical interface to manually add key values. When adding manually, please note that if you are creating a folder, you need to add a forward slash at the end: /

5. Add the value corresponding to the key in the consul graphical page to specify the load balancing algorithm

{"weight":1, "max_fails":2, "fail_timeout":10, "down":0} 

insert image description here

Then, because the wget download speed is sometimes slow, the file is uploaded here

File Links

This is the end of this article about Nginx + consul + upsync to achieve dynamic load balancing. For more related Nginx + consul + upsync dynamic load balancing content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The principle and configuration of Nginx load balancing and dynamic and static separation
  • Nginx Layer 4 Load Balancing Configuration Guide
  • How to configure Nginx load balancing
  • Analysis of the principle of Nginx+Tomcat to achieve load balancing and dynamic and static separation
  • What is Nginx load balancing and how to configure it
  • Implementation method of Nginx+tomcat load balancing cluster
  • Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations
  • Nginx configuration to achieve multiple server load balancing

<<:  JavaScript to implement checkbox selection or cancellation

>>:  MySQL subqueries and grouped queries

Recommend

JS realizes the front-end paging effect

This article example shares the specific code of ...

Simple example of using Docker container

Table of contents 1. Pull the image 2. Run the im...

Two ways to start Linux boot service

Table of contents rc.local method chkconfig metho...

CSS to achieve the image hovering mouse folding effect

CSS to achieve the image hovering mouse folding e...

mysql5.7.14 decompressed version installation graphic tutorial

MySQL is divided into Community Edition (Communit...

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...

How to set default value for datetime type in MySQL

I encountered a problem when modifying the defaul...

Vue implements a simple shopping cart example

This article shares the specific code of Vue to i...

Source code reveals why Vue2 this can directly obtain data and methods

Table of contents 1. Example: this can directly g...

Summary of mysqladmin daily management commands under MySQL (must read)

The usage format of the mysqladmin tool is: mysql...

Example code for Html layered box-shadow effect

First, let’s take a look at the picture: Today we...

Docker installation and configuration steps for Redis image

Table of contents Preface environment Install Cre...

Detailed tutorial on distributed operation of jmeter in docker environment

1. Build the basic image of jmeter The Dockerfile...