[toc]

nginx的七层负载均衡概述

负载均衡简介

为什么使用负载均衡

  1. 解决web服务器的单点故障
  2. 让web服务器构成一个集群
  3. 将请求平均下发给后端的web服务器

负载均衡的叫法

  1. load balance(LB)
  2. server load balance(SLB)

image-20230616204006170

公有云负载均衡

  • 阿里云 SLB 阿里云负载均衡(Server Load Balancer,简称SLB)
  • 腾讯云 CLB
  • 青云 LB
  • AWS ELB

负载均衡的软件和硬件

  • 软件:
    • nginx
    • HAproxy
    • LVS
  • 硬件:
    • F5

image-20230616204218360

七层和四层的区别

  • 一个七层 传输层,一个是七层,应用层
  • 四层比七层快
  • 四层无法识别域名,七层可以识别域名

nginx七层负载实现场景

  • nginx要实现负载均衡依赖一个proxy_pass代理模块
  • Nginx负载均衡和Nginx代理不同的地方:
    • nginx的代理location层仅只能代理一台服务器
    • nginx的负载均衡可以代理多台服务器

负载均衡的语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Syntax: upstream name { ... }
Default: -
Context: http

#upstream例
upstream backend {
模块名 后端的主机 名字 (根据网站域名来起名)
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}

upstream blog.xxx.com {
server 172.16.1.7:80;
server 172.16.1.8:888;
serber 172.16.1.9:999;
}

server {
location {
proxy_pass http://blog.xxx.com
}
}

配置负载均衡

环境准备

主机 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 负载均衡 nginx
web01 10.0.0.7 172.16.1.7 web网站 nginx php
web02 10.0.0.8 172.16.1.8 web网站 nginx php
web03 10.0.0.9 172.16.1.9 web网站 nginx php

编辑nginx的配置文件(web01 web02 web03)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 在web服务器上分别编写配置文件
vim lb.xxx.conf
server {
listen 9999;
server_name lb.xxx.com;
root /code/lb;
index index.html;
}

# 创建站点目录
mkdir -p /code/lb

# 创建index.html
echo 'web01' > /code/lb/index.html
echo 'web02' > /code/lb/index.html
echo 'web03' > /code/lb/index.html

# 启动服务
systemctl start nginx && systemctl enable nginx

# 检测语法
nginx -t

# 本地域名解析
10.0.0.7 lb.xxx.com
#10.0.0.8 lb.xxx.com

image-20221226191145724

image-20221226191159456

负载均衡配置(lb01)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 配置负载均衡
vim /etc/nginx/conf.d/lb.conf
upstream lb.xxx.com{
server 172.16.1.7:9999;
server 172.16.1.8:9999;
}

server {
listen 80;
server_name lb.xxx.com;

location / {
proxy_pass http://lb.xxx.com;
include proxy_params;
}
}

# 优化内容
vim /etc/nginx/proxy_params
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

负载均衡模式

轮循

image-20221226191607096

加权轮循

image-20221226191622685

IP hash

image-20221226191708787

url hash

image-20221226191741820

nginx负载均衡后端状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# down
## 暂时不参与负载均衡
upstream lb.xxx.com{
server 172.16.1.7:9999 down; (#web01暂时不参与负载均衡,用户访问不到web01)
server 172.16.1.8:9999;
server 172.16.1.9:9999;
}

# backup
## 备份,当其他没有backup状态的负载均衡机器都down机的情况下,才会下发到备份的机器上
upstream lb.xxx.com{
server 172.16.1.7:9999 backup;
server 172.16.1.8:9999;
server 172.16.1.9:9999;
}

# max_fails 最大连接失败次数
upstream lb.xxx.com{
server 172.16.1.7:9999 backup;
server 172.16.1.8:9999 max_fails=3;
server 172.16.1.9:9999 ;
}

# fail_timeout 结合max_fails使用,服务暂停时间,
upstream lb.xxx.com{
server 172.16.1.7:9999 backup;
server 172.16.1.8:9999 max_fails=3 fail_timeout=60s;# 10.0.0.8:9999 最多失败三次之后暂停1分钟
server 172.16.1.9:9999 ;
}

# max_conns 限制最大的接收连接数
upstream lb.xxx.com{
server 172.16.1.7:9999 backup;
server 172.16.1.8:9999 max_fails=3 fail_timeout=60s;
server 172.16.1.9:9999 max_conns=1;
}

nginx负载均衡健康检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 安装依赖
yum install -y pcre-devel openssl-devel

# 项目地址
https://github.com/yaoweibin/nginx_upstream_check_module

# 停止nginx
systemctl stop nginx

# 下载源码包
wget https://nginx.org/download/nginx-1.22.1.tar.gz

# 下载第三方模块
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# 解压
tar xf nginx-1.22.1.tar.gz && unzip master.zip

# 打补丁
cd ~/nginx-1.22.1/

# 安装patch
yum install -y patch

# 打补丁
patch -p1 < /root/nginx_upstream_check_module-master/check_1.20.1+.patch

# 创建目录
mkdir /app

# 生成
./configure --prefix=/app/nginx-1.22.0 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/root/nginx_upstream_check_module-master
## ./configure 'nginx -V' --add-module=/root/nginx_upstream_check_module-master

# 编译
make && make install

# 编辑配置文件
vim /app/nginx-1.22.0/conf/nginx.conf

user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include 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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on
include /app/nginx-1.22.0/conf/conf.d/*.conf;

# 创建目录
mkdir conf.d
vim /app/nginx-1.22.0/conf/conf.d/lb.xxx.com.conf

upstream lb.xxx.com{
server 172.16.1.7:9999;
server 172.16.1.8:9999;
server 172.16.1.9:9999;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval 检测间隔时间,单位为毫秒
#rise 表示请求2次正常,标记此后端的状态为up
#fall 表示请求3次失败,标记此后端的状态为down
#type 类型为tcp
#timeout 超时时间,单位为毫秒
}
server {
listen 80;
server_name lb.xxx.com;

location / {
proxy_pass http://lb.xxx.com;
include proxy_params;
}
location /check_health {
check_status;
}
}

# 创建proxy_params文件
vim /app/nginx-1.22.0/conf/proxy_params
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

# 启动nginx
/app/nginx-1.22.0/sbin/nginx

# 配置本地域名解析
10.0.0.5 lb.xxx.com

# 访问健康检查网址
lb.xxx.com/check_health

image-20221227210119366

SLB负载均衡

概述

随着互联网和数据中心的快速发展,大量的数据和信息需要在网络中传输。在这样的情况下,负载均衡(SLB)变得越来越重要。负载均衡可以提高系统的可用性、性能和可伸缩性,保证系统的稳定运行。在本篇文章中,我们将探讨负载均衡SLB的原理以及工作场景。

负载均衡SLB的概念

  • 负载均衡(Load Balancing)是指对多台服务器或者网络设备进行分布式处理,实现请求分发和流量管理的技术。其核心思想是将请求分发到多个服务器上,以实现服务器的负载均衡,提高系统的性能和可用性。
  • 负载均衡6(SLB)是一种特殊的负载均衡技术,它可以对网络流量进行更加细粒度的控制。具体来说,SLB可以根据客户端访问请求的特征,将请求转发到不同的服务器处理。SLB可以在内容层、网络层或者传输层进行操作,从而实现灵活且高效的负载均衡功能。

负载均衡SLB的工作原理

负载均衡SLB可以通过三种不同的方式进行实现:内容路由、硬件负载均衡器和软件负载均衡器。此外,也可以使用DNS负载均衡实现负载均衡的功能。

阿里云负载均衡SLB支持以下类型的负载均衡:

  • 应用型负载均衡ALB(Application Load Balancer):
    • 专门面向七层,提供超强的业务处理性能,例如HTTPS卸载能力。
    • 单实例每秒查询数QPS(Query Per Second)可达100万次。
    • 同时ALB提供基于内容的高级路由特性,例如基于HTTP报头、Cookie和查询字符串进行转发、重定向和重写等,是阿里云官方云原生Ingress网关。
  • 网络型负载均衡NLB(Network Load Balancer):
    • 面向万物互联时代推出的新一代四层负载均衡,支持超高性能和自动弹性能力,单实例可以达到1亿并发连接,用于应对高并发业务。
    • NLB有TCPSSL卸载、新建连接限速、多端口监听等高级特性,可用于物联网MQTTS加密卸载、抗洪峰上联等场景。
  • 传统型负载均衡CLB(Classic Load Balancer):
    • 支持TCP、UDP、HTTP和HTTPS协议,具备良好的四层处理能力,以及基础的七层处理能力。