Nginx combat - load balancing

1. Load balancing

Learn about Nginx load balancing today. Due to the limitations of traditional software construction and the limited processing capacity of a server, it is difficult to meet our requirements in today's high-concurrency and complex business scenarios. But if many such servers are combined in a certain way, and all requests are evenly distributed to these machines for processing, then this "system" can handle as many requests as possible, which is the original design of load balancing Thought.

Of course, the technologies that can be used include software and hardware. This time we will focus on Nginx's load balancing. The figure below is a four-layer + seven-layer load balancing. Adopt the topology of lvs+nginx.

2. The concept of load balancing

1. What is load balancing

Load balancing, referred to as LB (Load Balancing), is a computer technology. Used to distribute load among multiple computers (computer clusters), network connections, CPUs, disk drives, or other resources to optimize resource usage, maximize throughput, minimize response time, and avoid overloading. Macroscopically, it means to balance and distribute the load (work tasks, access requests) to multiple operating units (servers, components) for execution to solve high performance, single point of failure (high availability), and scalability (horizontal scaling) A common problem at high flow rates.

2. Classification of load balancing

Since the load balancing technology is to control the packets in the network transmission and involves the seven-layer model of the network, the load balancing classification can be classified according to the network layer, such as the second, third, fourth, and seventh layer load balancing. The most common of these are Layer 4 and Layer 7 load balancing. In addition, there are other aspects of classification, including soft balance, hardware load balance, etc. For example, the Eureka component in the SpringCloud family bucket, and the middleware of various sub-databases and sub-tables all belong to the category of load balancing. Layer 2, Layer 3, Layer 4, and Layer 7 load balancing all work in the OSI model, and a simple explanation will be given next. There are already many open source load balancing tools in the industry, most of which work on the fourth and seventh layers. Representative open source tools include Nginx, LVS, and Haproxy. LVS is mainly used for four-layer load balancing. Nginx and Haproxy mainly do seven-layer load balancing, but they both support four-layer load balancing. For example, the stream module in Nginx not only supports four-layer reverse proxy function, but also Support four-layer load balancing function.

2.1 Layer 2 load balancing

The load balancing server provides a VIP (virtual IP) to the outside world. Different machines in the cluster use the same IP address, but the MAC addresses of the machines are different. After the load balancing server receives the request, it forwards the request to the target machine by rewriting the target MAC address of the message to achieve load balancing. Layer 2 load balancing occurs at the data link layer, so it is also called "data link layer load balancing" and it belongs to the OSI model.

2.2 Layer 3 load balancing

The load balancing server provides a VIP (virtual IP), but different machines in the cluster use different IP addresses. When the load balancing server receives the request, it forwards the request to different real servers through IP according to different load balancing algorithms. The three-layer load balancing occurs at the network layer, so it is also called "network layer load balancing", and it also belongs to the OSI model.

2.3 Layer 4 load balancing

Four-layer load balancing works at the transport layer of the OSI model. Since there is only TCP/UDP protocol at the transport layer, these two protocols include source port number and destination port number in addition to source IP and destination IP. After receiving the client request, the four-layer load balancing server forwards the traffic to the application server by modifying the address information (IP+port number) of the data packet.

2.4 Layer-7 load balancing

Seven-layer load balancing works in the application layer of the OSI model, and there are many application layer protocols, such as http and dns are commonly used. Layer-7 loads can be loaded based on these protocols. These application layer protocols will contain a lot of meaningful content. For example, in the load balancing of the same web server, in addition to the load based on IP plus port, it can also decide whether to perform load balancing based on the seven-layer URL, browser type, and language.

3. Load balancing algorithm

Load balancing algorithms can be divided into two categories, one is static load balancing algorithms, common ones include polling, weighting, etc.; the other is dynamic load balancing algorithms, common ones are least connection, fastest response, service type , service quality, etc. The following focuses on recording several commonly used load balancing algorithms.

3.1 Polling (default)

Each request from the network is distributed to the internal servers in turn, from 1 to N and then restarts. This load balancing algorithm is suitable for the situation that the servers in the server group all have the same configuration and the average service request is relatively balanced.

3.2 Weighted polling (weight)

根据服务器的不同处理能力,给每个服务器分配不同的权值,使其能够接受相应权值数的服务请求。例如:服务器 A 的权值被设计成 1,B 的权值是 3,C 的权值是6,则服务器 A、B、C 将分别接受到 10%、30%、60% 的服务请求。此种均衡算法能确保高性能的服务器得到更多的使用率,避免低性能的服务器负载过重。

3.2 ip-hash(ip_hash)

我们都知道,每个请求的客户端都有相应的 ip 地址,该均衡策略中,nginx 将会根据相应的 hash 函数,对每个请求的 ip 作为关键字,得到的 hash 值将会决定将请求分发给相应 Server 进行处理。iphash算法的情况下,每一个IP在不变的情况下都会路由到首次请求的服务上。

3.2 最少连接数(least_conn)

最少连接,也就是说 nginx 会判断后端集群服务器中哪个 Server 当前的 Active Connection 数是最少的,那么对于每个新进来的请求,nginx 将该其分发给对应的 Server。

4. Nginx中的负载均衡配置

Nginx 的 stream 模块和 http 模块分别支持四层和七层模块的负载均衡。其用法和支持的负载均衡策略大致相同。首先使用 upstream 指令块 和 server 指令指定上游的服务,upstream 指令的用法如下

upstream name { ... }

官网示例如下:下面定义了4台服务器,分别是域名、IP+端口、socket形式指定地址,默认情况下upstream指令块中采用的是加权轮训的方式轮询配置得服务,当然我们可以指定一些服务的静态配置,比如注定服务的权重、server的最大并发连接max_conns,(max_fails 和 fail_timeout)失败几次之后将服务标记为故障服务,在设置的时间内不在路由到标记为故障的服务。

upstream backend {
    server backend1.example.com weight=5;
    server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;
    server backup1.example.com  backup;
}

三、 负载均衡实践

基础知识梳理完毕,接下来进行实践来感受一下相关的概念,俗话说的:好纸上得来终觉浅,绝知此事要躬行!希望可以通过这些实验让咱们更加巩固这些技术点。

1、Nginx中负载均衡

环境准备:mac,我这边使用的是docker安装的nginx,安装教程可以根据我的这篇文章:传送门:Nginx教程,两给SpringBoot服务。首先咱们搭建环境。因为我们之前学习过docker挂载,所以这次修改Nginx文件就方便很多,需要的同学可以参考:docker 挂载、修改文件,如果没有进行挂载想要修改文件的可以参考这篇文章。废话不多说直接上case.

1.1 两个微服务

首先提供两个微服务模拟一份代码两台机器。两个服务都有一个路径相同的对外接口。

@RestController
@Slf4j
public class Demo1LBController {

    @GetMapping("/lb/start")
    public String demo1() {
        String str = "demo1 lb start ...";
        log.info(str);
        System.out.println(str);
        return str;
    }
}

@RestController
@Slf4j
public class Demo2LBController {

    @GetMapping("/lb/start")
    public String demo2() {
        String str = "demo2 lb start ...";
        log.info(str);
        System.out.println(str);
        return str;
    }
}

1.2 Nginx配置修改

upstream 块在http块中,用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡。咱们定义负载均衡的服务叫做:myserver,然后在 server 块里的 location 配置代理:proxy_pass http://myserver;。

nginx.config里的配置
    # ######## 负载均衡开始 ########
    upstream myserver {
       server 10.33.148.23:8081;
       server 10.33.148.23:8082;

       # server backend1.example.com weight=5;
       # server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
       # server unix:/tmp/backend3;
       # server backup1.example.com  backup;
   }
    # ######## 负载均衡结束 ########
    
default.config里的配置
   location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://myserver;

    }

1.2 验证

咱们启动NG之后,在浏览器输入:http://localhost:8010/lb/start,即可得到请求到不同服务的返回,默认走的轮询所以都是规律性的返回。

2、轮询

轮询的配置就是不设置任何静态参数,采用 Nginx 默认的负载均衡策略,上面的验证就是,我这边就做赘述。

3、加权轮询

顾名思义就是根据服务器的不同处理能力,给每个服务器分配不同的权值。具体配置如下,8081的服务权重占4/5,大家可以在浏览器操作感受一下。

4、ip_hash

每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。配置上ip hash之后即使有权重也不会重新请求其他的服务上。大家可以操作感受。

ip_hash;

5、参数说明

weight=number 设定服务器的权重,默认是1,权重越大被访问机会越大,可以根据机器的配置情况来配置。

max_fails=number 设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就认为服务器不 可用。在下一个fail_timeout时间段,服务器不会再被尝试。 失败的尝试次数默认是1。

默认配置时,http_404状态不被认为是失败的尝试。 可以通过指令proxy_next_upstream 和memcached_next_upstream来配置什么是失败的尝试。

fail_timeout=time
统计失败尝试次数的时间段。在这段时间中,服务器失败次数达到指定的尝试次数,服务器就被认为不可用。默认情况下,该超时时间是10秒。 

backup
标记为备用服务器。当主服务器不可用以后,请求会被传给这些服务器,配置这个指令可以实现故障转移。

down 标记服务器永久不可用,可以跟ip_hash指令一起使用。

负载均衡的相关知识就梳理完毕啦,本次对 Nginx 中的负载均衡的相关配置指令,实战 Nginx 的负载均衡功能,同时测试多种负载均衡算法的学习和记录希望可以帮助大家更好的里解Nginx。

Guess you like

Origin blog.csdn.net/lly576403061/article/details/129699081