Four layer load balancing

1, Four layer load balancing

1. What is four layer load balancing

The so-called four layer load balancing is to determine the final internal server through the target address and port in the message and the server selection mode set by the load balancing equipment.
Taking the common TCP as an example, when receiving the first SYN request from the client, the load balancing device selects the best server, modifies the target IP address in the message (changed to the back-end server IP) and forwards it directly to the server. TCP connection establishment, that is, three handshakes, is established directly between the client and the server. The load balancing device only plays a forwarding action similar to that of the router. In some deployment cases, in order to ensure that the packets returned by the server can be correctly returned to the load balancing device, the original source address of the message may be modified while forwarding the message.

2. Application scenarios

1. Four layers + seven layers are used for load balancing. Four layers can ensure the high availability of load balancing of seven layers;
2. Load balancing can be used for port forwarding
3. Database read / write separation

4 + 7 application scenarios

3. Four layer load balancing features

1. Layer 4 load balancing can only forward TCP/IP protocol and UDP protocol. It is usually used to forward ports, such as tcp/22 and udp/53;
2. Layer 4 load balancing can be used to solve the problem of port restriction of layer 7 load balancing; (layer 7 load balancing uses 65535 port numbers at most)
3. Layer 4 load balancing can solve the problem of high availability of layer 7 load balancing; (multiple backend seven layer load balancing can be used at the same time)
4. The forwarding efficiency of layer 4 is much higher than that of layer 7, but only supports tcp/ip protocol, not http and https protocols;
5. In general, for large concurrency scenarios, four layers of load balancing are added in front of the seven layers of load.

2, Four layer load balancing practice

1. Environmental preparation

host ip identity
lb04 172.16.1.3 Four layer load balancing
lb01 172.16.1.5 Seven layer load balancing
lb02 172.16.1.6 Seven layer load balancing

Prepare nginx and configuration files on lb01 and 02, and the load balancing is OK, and the configuration should be the same

2. Configure four layers of load balancing

1) Four layer load balancing syntax

Syntax: stream { ... }
Default:    —
Context:    main
 
#Example: the four layer load balancing stream module is at the same level as the HTTP module and cannot be configured in http
stream {
    upstream backend {
        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
    }
 
    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}

2) Configure nginx master profile

[root@lb4 ~]# vim /etc/nginx/nginx.conf
#Comment all content of http layer
user  www;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
#Add an include file
include /etc/nginx/conf.c/*.conf;
#http {
#    include       /etc/nginx/mime.types;
#    default_type  application/octet-stream;
#    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                      '$status $body_bytes_sent "$http_referer" '
#                      '"$http_user_agent" "$http_x_forwarded_for"';
#    access_log  /var/log/nginx/access.log  main;
#    sendfile        on;
#    #tcp_nopush     on;
#    keepalive_timeout  65;
#    #gzip  on;
#    include /etc/nginx/conf.d/*.conf;
#}

3) Configure four layer load balancing

#Create directory
[root@lb4 ~]# mkdir /etc/nginx/conf.c
 
#to configure
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }
 
    server {
        listen 80;
        proxy_pass lbserver;
    }
}

4) Start service

[root@lb4 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb4 ~]# systemctl start nginx

5) Configure hosts access

10.0.0.3 linux.wp.com linux.lb.com
 
#visit
http://linux.wp.com/

6) Layer 4 load balancing configuration log

#Layer 4 load balancing does not have access logs, because in nginx In the conf configuration, the log format of access is configured under http, while the four layer load balancing configuration is outside http;
 
#If logs are required, they need to be configured under stream
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
    log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                  '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
    access_log /var/log/nginx/proxy.log proxy;
 
    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }
 
    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}
 
#View all web server logs
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log

3, Layer 4 load port forwarding

1. Request 5555 port for load balancing and jump to port 22 of web01

#Simple configuration
stream {
    server {
        listen 5555;
        proxy_pass 172.16.1.7:22;
    }
}
 
#General configuration
stream {
    upstream ssh_7 {
        server 10.0.0.7:22;
    }
 
    server {
        listen 5555;
        proxy_pass ssh_7;
    }
}

2. Request 6666 port for load balancing and jump to 172.16.1.51:3306

stream {
    upstream db_51 {
        server 172.16.1.51:3306;
    }
 
    server {
        listen 6666;
        proxy_pass db_51;
    }
}

3. Load balancing of database slave database

stream {
    upstream dbserver {
        server 172.16.1.51:3306;
        server 172.16.1.52:3306;
        server 172.16.1.53:3306;
        server 172.16.1.54:3306;
        server 172.16.1.55:3306;
        server 172.16.1.56:3306;
    }
 
    server {
        listen 5555;
        proxy_pass dbserver;
    }
}

Added by charliez on Mon, 10 Jan 2022 14:01:54 +0200