www.cnblogs.com/wunaozai/p/…
The foreword 0.
For a simple configuration of Nginx load balancing, I have previously configured http-based load balancing on my blog. This time the load balancing is a little different, it is based on TCP load balancing. Http-based load balancing is supported by the default Nginx release and is simple to configure, but tcp-based load balancing is a bit more cumbersome to configure.
1. Download and install
Since we will be using four TCP layer load balancers, we will compile Nginx ourselves, download the latest version here nginx.org/download/ng… Git Clone github.com/openresty/e…
Then compile
1 ./configure --prefix=/root/workspace/emq/nginx --with-stream - add - the module = / root/workspace/emq/nginx - 1.12.2 / echo - nginx - module - with - http_geoip_moduleCopy the code
Libgeoip-dev apt-get install libgeoip-dev apt-get install libgeoip-dev apt-get install libgeoip-dev If there is need can be downloaded here packet dev.maxmind.com/geoip/legac… www.cnblogs.com/kevingrace/…
If the preceding information is displayed, the compilation is successful
1 make && make install
Copy the code
2. Add a virtual NIC
One is temporary addition, one is configured to boot
1 ifconfig eth1:1 172.16.23.111 netmask 255.255.255.0 up
2 ifconfig eth1:1 down
Copy the code
Vim /etc/network/interfaces Is added and restarts the NIC
1 auto lo
2 iface lo inet loopback
3
4 auto eth1
5 iface eth1 inet static
6 address 172.16.23.204
7 netmask 255.255.255.0
8 gateway 172.16.23.1
9
10 #auto eth1:1
11 #iface eth1:1 inet static
12 # address 172.16.23.104
13 # netmask 255.255.255.0
14 # gateway 172.16.23.1
Copy the code
Conf configuration file
1 user root; 2 3 worker_processes auto; 4 5 events { 6 worker_connections 1024; 7} 8 9 stream {10 11 upstream Backend {12 server 172.16.20.217:60000; 13 server 172.16.20.217:60001; 10} 15 16 upstream backbind {17 server 172.16.23.203:54321; 18} 19 20 map $msec $proxy_bind_ip {21 ~[0-5]$172.16.23.111; 22 ~ [5-9] $172.16.23.204; 23 } 24 25 server { 26 listen 12345; 27 proxy_pass backend; 28 proxy_bind $proxy_bind_ip; 29 } 30 } 31 32 http { 33 geo $LB { 34 default 1; 35 #include conf/geo. Conf 36 172.16.20.217 0; 37 172.16.20.218 1; 38} 39 map $LB $proxy_bind_ip {40 0 172.16.23.111; 1 172.16.23.204 41; 42} 43 map $msec $proxy_bind_ip_2 {44 ~[0-5]$172.16.23.111; 45 ~ [5-9] $172.16.23.204; 46 } 47 server { 48 listen 8888; 49 location /test { 50 echo $proxy_bind_ip; 51 echo $proxy_bind_ip_2; 52 echo $connection; 53 echo "hostname" $hostname; 54 echo "msec" $msec; 55 echo "pid" $pid; 56 echo "proxy_protocol" $proxy_protocol_addr ":" $proxy_protocol_port; 57 echo "remote:" $remote_addr ":" $remote_port; 58 echo "server:" $server_addr ":" $server_port; 59 echo "status:" $status; 60 echo "time:" $time_iso8601; 61 echo "time:" $time_local; 62} 63} 64}Copy the code
Add nginx echo module, Geo module, stream module
Start debugging, using HTTP {} for debugging, because http://127.0.0.1:8888/test can adjustment of the variables, think that don’t know the grammar of the map, so can only bit by bit to debug.
Upstream {} Back-end load balancer
Map {} variable mapping
Geo {} IP address filtering
Server {} Server configuration
More functions do not describe here, online information more complete.
4. Test
As you can see from the two figures above, create two service listening applications
172.16.23.217:60000 172.16.23.217:60001 These two simulated back-end business applications (MQTT cluster).
TCP Client creates 5 connections to simulate different clients (MQTT devices)
172.16.23.204:12345 172.16.23.111:12345 These two ports are monitored by the load balancer Nginx.
Five clients are connected to an Nginx load balancer, which randomly loads two back-end applications, 60000 and 60001, and also randomly uses virtual network cards 204 or 111 of Nginx