Basic knowledge of load balancing and a simple example of load balancing using nginx

Basic knowledge of load balancing and a simple example of load balancing using nginx

Nginx can generally be used for seven-layer load balancing. This article will introduce some basic knowledge of load balancing and a simple example of using nginx for load balancing.

Layer 4 load balancing vs Layer 7 load balancing

People often talk about seven-layer load balancing or four-layer load balancing. In fact, it is determined by the name of the layer of the ISO OSI network model. Because nginx uses the http protocol to perform load balancing operations at the application layer, it is called a seven-layer load balancing. Load balancing operations such as LVS at the TCP layer are called four-layer load balancing. Generally speaking, there are the following load balancing categories:

Common software support

Common load balancing algorithms

There are several common load balancing algorithms:

Load balancing demonstration example: normal polling

Next, we use nginx to demonstrate how to perform normal polling:

Preparation

Two services are started on ports 7001/7002 respectively to display different information. For the convenience of demonstration, a mirror is made using tornado. Different parameters are passed when the docker container is started to display the differences in services.

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7001"
ddba0abd24524d270a782c3fab907f6a35c0ce514eec3159357bded09022ee57
[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7002"
95deadd795e19f675891bfcd44e5ea622c95615a95655d1fd346351eca707951
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7001
Hello, Service :User Service 1: 7001
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7002
Hello, Service :User Service 1: 7002
[root@kong ~]#

Start nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-lb -d nginx 
9d53c7e9a45ef93e7848eb3f4e51c2652a49681e83bda6337c89a3cf2f379c74
[root@kong ~]# docker ps |grep nginx-lb
9d53c7e9a45e nginx "nginx -g 'daemon ..." 11 seconds ago Up 10 seconds 0.0.0.0:9080->80/tcp nginx-lb
[root@kong ~]#

nginx code snippet

Prepare the following nginx code snippet and add it to nginx's /etc/nginx/conf.d/default.conf

http {
upstream nginx_lb {
  server 192.168.163.117:7001;
  server 192.168.163.117:7002;
}
server {
  listen 80;
  server_name www.liumiao.cn 192.168.163.117;
  location / {
    proxy_pass http://nginx_lb;
  }
}

How to modify default.conf

This can be achieved by installing vim in the container, modifying it locally and then passing it in via docker cp, or modifying it directly with sed. If you install vim in the container, use the following method

[root@kong ~]# docker exec -it nginx-lb sh
# apt-get update
...omit #apt-get install vim
...omitted

Before modification

# cat default.conf
server {
  listen 80;
  server_name localhost;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }
  #error_page 404 /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  # proxy_pass http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #root html;
  # fastcgi_pass 127.0.0.1:9000;
  # fastcgi_index index.php;
  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  #include fastcgi_params;
  #}
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
}
#

After

# cat default.conf
upstream nginx_lb {
  server 192.168.163.117:7001;
  server 192.168.163.117:7002;
}
server {
  listen 80;
  server_name www.liumiao.cn 192.168.163.117;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  location / {
    #root /usr/share/nginx/html;
    #index index.html index.htm;
    proxy_pass http://nginx_lb;
  }
  #error_page 404 /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  # proxy_pass http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #root html;
  # fastcgi_pass 127.0.0.1:9000;
  # fastcgi_index index.php;
  # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  #include fastcgi_params;
  #}
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
}
#

Restart the nginx container

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#

Confirm the result

You can clearly see that polling is performed in order:

[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]#

Load Balancing Demo Example: Weighted Round Robin

On this basis, weighted polling only requires adding weight.

Modify default.conf

Modify default.conf as follows

# cp default.conf default.conf.org
# vi default.conf
# diff default.conf default.conf.org
2,3c2,3
< server 192.168.163.117:7001 weight=100;
< server 192.168.163.117:7002 weight=200;
---
> server 192.168.163.117:7001;
> server 192.168.163.117:7002;
#

Restart the nginx container

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#

Confirm the result

It can be seen that the polling results are carried out according to the proportion of 1/3 and 2/3:

[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]#

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to redirect to https through nginx load balancing
  • A brief discussion on Nginx seven-layer reverse proxy and load balancing
  • Detailed explanation of Nginx server load balancing strategy (6 types)
  • A brief analysis of the four modes of Nginx load balancing
  • How to build nginx load balancing under Linux
  • nginx realizes load balancing and dynamic and static separation
  • Nginx load balancing configuration simple configuration method
  • Three parameter settings for nginx load balancing
  • Detailed explanation of Nginx load balancing (architecture road)
  • Detailed steps to achieve load balancing with iis+nginx

<<:  js uses Canvas to merge multiple pictures into one implementation code

>>:  Solution to the problem that the configuration file my.cnf in MySQL cannot be started due to permission issues

Recommend

How to solve the problem of automatic package update in Debian system

I don't know when it started, but every time ...

Summary of the Differences between SQL and NoSQL

Main differences: 1. Type SQL databases are prima...

HTML table only displays the outer border of the table

I would like to ask a question. In Dreamweaver, I...

MySQL 5.7.17 installation and configuration graphic tutorial

The blogger said : I have been writing a series o...

react+antd.3x implements ip input box

This article shares the specific code of react+an...

The difference between Vue interpolation expression and v-text directive

Table of contents 1. Use plugin expressions 2. Us...

Detailed explanation of how to install PHP7 on Linux

How to install PHP7 on Linux? 1. Install dependen...

Native js custom right-click menu

This article example shares the specific code of ...

Talk about the understanding of CSS attribute margin

1.What is margin? Margin is used to control the sp...

Beginners learn some HTML tags (3)

Related articles: Beginners learn some HTML tags ...