nginx four-layer tcp load balancing and active and standby, four-layer udp load balancing and active and standby, 7-layer http load balancing and active and standby configuration (wndows system active and standby, load balancing)

Preparation

Prepare two centos7 computers, virtual machines can also be used

Use vmware17 to build centos7 mini version server on windows-CSDN Blog

Set up a static IP address (because the network load balancing manager on the Windows server does not support dhcp servers)

Modify the network card IP address in ky10, centos7 and other linux systems, and set the network card to start automatically at boot_kylin configure ip-CSDN blog

Build keepalived

keepalived installation and configuration (server active and standby, load balancing)-CSDN Blog

Active and backup topology

?

centos7 system nginx compilation and installation

nginx, which supports four-layer load balancing, has version requirements: 1.19 or above is required. I chose 1.25.3 for testing here.

The program address is as follows:

https://gitcode.net/zengliguang/nginx_offline_package.git

Download via the following command

git clone https://gitcode.net/zengliguang/nginx_offline_package.git

You need to install git first

Enter the following command

yum install -y git

Wait for installation to complete

?

To download the nginx_offline_package script, you need to download the script to the /root root directory, so that the subsequent installation script can be used directly

?

Compile the installation script

cd /root/nginx_offline_package
source centos7_nginx_online_comp_install.sh

# Turn off the firewall
systemctl stop fire*
systemctl disable fire*

Wait for compilation to complete

?

Confirm whether the operation is successful

Enter the server IP in the browser

The following picture appears indicating that the deployment was successful.

?

Configuration file path after centos7_nginx_online_comp_install.sh script is installed: /usr/local/nginx/conf/nginx.conf

centos7 system offline installation

nginx, which supports four-layer load balancing, has version requirements: 1.19 or above is required. I chose 1.20.0 for testing here.

Download the offline installation package, you can choose the latest version 1.24.0

?

Index of /packages/rhel/7/x86_64/RPMS/

The program address is as follows:

https://gitcode.net/zengliguang/nginx_offline_package.git

Download via the following command

git clone https://gitcode.net/zengliguang/nginx_offline_package.git

To download the nginx_offline_package script, you need to download the script to the /root root directory, so that the subsequent installation script can be used directly

?

Offline installation script

# todo
cd /root/nginx_offline_package
source centos7_nginx_offline_install.sh

# Turn off the firewall
systemctl stop fire*
systemctl disable fire*

Configuration file path after centos7_nginx_offline_install.sh script is installed: /etc/nginx/nginx.conf, which references /etc/nginx/conf.d

If we modify it, we can directly modify it in the /etc/nginx/nginx.conf configuration file without referencing another file.

Download, install and run nginx on windows system

nginx, which supports four-layer load balancing, has version requirements: 1.19 or above is required. I chose 1.25.3 for testing here.

The program address is as follows:

https://gitcode.net/zengliguang/nginx-1.25.3.git

Download via the following command

git clone https://gitcode.net/zengliguang/nginx-1.25.3.git

?

Double-click start.bat to run nginx

Double-click stop.bat to stop nginx

Four-layer tcp, udp load balancing and active and backup configuration

The configuration file is as follows

stream {
  
    #tcp load balancing
    upstream agent{
    
      server 192.168.10.85:5000 max_fails=1 fail_timeout=10s;
      server 192.168.10.10:5000 max_fails=1 fail_timeout=10s backup;
    
    }
    server {
        listen 5000;
        proxy_connect_timeout 5s;
proxy_timeout 10m;
        ###proxy_timeout 24h;
        ###This parameter is very useful when using EMQ stress testing. The timeout period of the device is too long at the beginning, causing {shutdown, connack_timeout} to appear after a short period of time during the test.
        proxy_pass agent;
    }

     #udp load balancing, window system does not support it, it needs linux system to support it
     upstream dns_upstreams {
         server 192.168.59.8:30001;
         server 192.168.59.5:30001 backup;
     }

     server {
         listen 30001 udp reuseport; #windows not supported; linux supported
         proxy_pass dns_upstreams;
         proxy_timeout 1s;
         proxy_responses 1;
         error_log logs/dns.log;
     }


}

Four-layer tcp load balancing and active and backup configuration

Active and backup configuration

illustrate:

If the current servers are busy and have no time to respond to new requests, if a backup machine is configured, the backup machine will bear part of the pressure. If the pressure on the main server is reduced, user requests can be processed normally. The backup machine is in a waiting state. .

The configuration file is as follows

stream {
  
    #tcp load balancing
    upstream agent{

      # The maximum number of errors and error timeout need to be set larger. It will take some time to switch.
      server 192.168.10.85:5000 max_fails=60 fail_timeout=60s;
      server 192.168.10.10:5000 max_fails=60 fail_timeout=60s backup;
    
    }
    server {
        listen 5000;
        proxy_connect_timeout 5s;
proxy_timeout 10m;
        ###proxy_timeout 24h;
        ###This parameter is very useful when using EMQ stress testing. The timeout period of the device is too long at the beginning, causing {shutdown, connack_timeout} to appear after a short period of time during the test.
        proxy_pass agent;
    }

}

illustrate:

The listening server is the server where the nginx service is located

Reverse proxy to 192.168.10.85:5000 (primary server) and 192.168.10.10:5000 (standby server)

Four-layer UDP load balancing and active and backup configuration

Active and backup configuration

illustrate:

If the current servers are busy and have no time to respond to new requests, if a backup machine is configured, the backup machine will bear part of the pressure. If the pressure on the main server is reduced, user requests can be processed normally. The backup machine is in a waiting state. .

The configuration file is as follows

stream {
    #udp load balancing, window system does not support it, it needs linux system to support it
    upstream dns_upstreams {
        server 192.168.59.8:30001;
        server 192.168.59.5:30001 backup;
    }

    server {
        listen 30001 udp reuseport; #windows not supported; linux supported
        proxy_pass dns_upstreams;
        proxy_timeout 1s;
        proxy_responses 1;
        error_log logs/dns.log;
    }

}

illustrate:

The listening server is the server where the nginx service is located

Reverse proxy to 192.168.59.8:30001 (primary server) and 192.168.59.5:30001 (standby server)

7-layer http load balancing and active and backup configuration

Active and backup configuration

illustrate:

If the current servers are busy and have no time to respond to new requests, if a backup machine is configured, the backup machine will bear part of the pressure. If the pressure on the main server is reduced, user requests can be processed normally. The backup machine is in a waiting state. .

The configuration file is as follows

http {
    include mime.types;
    default_type application/octet-stream;

    sendfile on;

    keepalive_timeout 65;

    upstream backend {
        server 192.168.10.85:8082 backup;
        server 192.168.10.10:8082;
    
    }


    server {
        listen 8082;
        server_name localhost;

    
        location/{
            proxy_pass http://backend;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            roothtml;
        }
     
    }

illustrate:

The listening server is the server where the nginx service is located

Reverse proxy to 192.168.10.85:8082 (standby server) and 192.168.10.10:8082 (main server)

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Network Skill TreeHomepageOverview 42442 people are learning the system