Catalog
Overview of Nginx Four-Layer Load Balancing
What is Load Balancing
Four-tier load balancing is encapsulated based on transport layer protocol packages (such as TCP/IP). The seven-tier we used before refers to the application layer, which is assembled on the basis of the four-tier, regardless of whether the four-tier or the seven-tier refers to the OSI network model.
Load balancing application scenarios
1. Four layers + seven layers to do load balancing, four layers can ensure the high availability of seven layers of load balancing; for example, nginx can not guarantee high availability of its own services, it needs to rely on LVS or keep alive.
2. For example: TCP protocol load balancing, some requests are TCP protocol (mysql, ssh), or these requests only need to use four layers for port forwarding, so use four layers load balancing.
Four-tier and Seven-tier Cluster Architecture
Summary of Four-Layer Load Balancing
1. Four-tier load balancing can only forward TCP/IP protocol, UDP protocol, usually used for forwarding ports, such as tcp/22, udp/53;
2. Four-tier load balancing can be used to solve the seven-tier load balancing port restriction problem; (Six-tier load balancing uses a maximum of 65535 port numbers)
3. Four-tier load balancing can solve the problem of high availability of seven-tier load balancing; (multiple back-end seven-tier load balancing can be used by colleagues)
4. The forwarding efficiency of the fourth layer is much higher than that of the seventh layer, but it only supports the tcp/ip protocol and does not support the http and https protocols.
5. Usually large concurrency scenarios usually choose to add four-tier load balancing before the seven-tier load.
How to Configure Four-Layer Load Balancing in Nginx
1. By accessing 5555 ports of load balancing, it is actually the 22 ports of the back-end web01 that provide services.
2. By accessing the 6666 port of load balancing, the 3306 port of mysql in the back end is actually providing services.
Configure two lb load balancers first
[root@lb02 ~]# cat /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key #Install nginx on lb02 [root@lb02 yum.repos.d]# yum install -y nginx #Synchronize all nginx-related configurations of lb01 on lb02 [root@lb02 ~]# scp -r root@172.16.1.5:/etc/nginx /etc/ #start nginx [root@lb02 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb02 conf.d]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@lb02 conf.d]# nginx
1. Create a directory for four-tier load balancing profiles
[root@lb02 ~]# vim /etc/nginx/nginx.conf events { .... } include /etc/nginx/conf.c/*.conf; http { ..... } [root@lb02 ~]# mkdir /etc/nginx/conf.c
2. Configuring four-tier load balancing
[root@lb02 conf.c]# cat lb_domain.conf stream { upstream lb { server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s; server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s; } server { listen 80; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass lb; } } [root@web03 conf.c]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web03 conf.c]# nginx -s reload #Configure the browser to access and view the nginx log after the native hosts are parsed
3. Four Layer Load Balancing Open Log
#Four-tier load balancing has no access log, because in the configuration of nginx.conf, access log format is configured under http, while the four-tier complex balancing configuration is outside http. #If you need logs, you need to configure them under stream [root@lb01 conf.c]# cat lb_domain.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 lb { server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s; server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s; } server { listen 80; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass lb; } }
nginx four-tier load balancing port forwarding
1. Transfer of tcp using nginx four-tier load balancing
Request load balancing 5555 - > 172.16.1.7:22; Request load balancing 6666 - > 172.16.1.51:3306;
2. Configuring nginx four-tier load balancing to realize tcp transfer
[root@lb4-01 ~]# cat /etc/nginx/conf.c/lb_domain.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; #Define 22 ports for forwarding ssh upstream ssh_7 { server 10.0.0.7:22; } #Define port 3306 for forwarding mysql upstream mysql_51 { server 10.0.0.51:3306; } server { listen 5555; proxy_connect_timeout 3s; proxy_timeout 300s; proxy_pass ssh_7; } server { listen 6666; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass mysql_51; } }