20.四层负载均衡
四层负载均衡
什么是四层负载均衡
四层负载均衡是基于传输层协议包来封装的(如:TCP/IP),那我们前面使用到的七层是指的应用层,他的组装在四层的基础之上,无论四层还是七层都是指的OSI网络模型。
######### 4层负载均衡和7层区别
# 七层负载均衡:识别域名,是http层
# 四层负载均衡:不识别域名,是TCP层(相当于端口转发)
tcp://10.0.0.51:6379
# 在nginx1.9.0版本之前,没有四层负载均衡(只能做7层负载)
### 四层负载的作用
1.端口转发
2.7层负载的高可用
3.解决7层负载的端口限制
4.四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5.通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。
四层负载均衡总结
# 1、四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
2、四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
3、四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同事的使用)
4、四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5、通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。
ngx_stream_core_module
########### 四层负载均衡使用的模块
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 web {
server 172.16.1.7:8007; # 必须添加端口
server 172.16.1.8:8008;
}
server {
listen 80; # 不认识域名
proxy_pass web;
proxy_connect_timeout 3s;
proxy_timeout 3s;
}
}
###### 注意:strem只能配置在http层之外
events {
...
}
include /etc/nginx/stream_conf/*.conf;
http {
...
}
搭建四层负载均衡架构
1.在web上配置
#### 在web01上配置
# 1.编辑配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/aa.conf
server {
listen 8007;
server_name aaa.com;
root /code/aa;
index index.html;
}
# 2.创建站点目录及页面
[root@web01 ~]# mkdir -p /code/aa
[root@web01 ~]# vim /code/aa/index.html
aaaaaaaaaaaaaaaaaa
# 3.重新加载配置文件
[root@web01 ~]# nginx -s reload
#### 在web02上配置
# 1.编辑配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/aa.conf
server {
listen 8008;
server_name aaa.com;
root /code/aa;
index index.html;
}
# 2.创建站点目录及页面
[root@web01 ~]# mkdir -p /code/aa
[root@web01 ~]# vim /code/aa/index.html
aaaaaaaaaaaaaaaaaa
# 3.重新加载配置文件
[root@web01 ~]# nginx -s reload
# 4.域名解析
10.0.0.7 aaa.com
10.0.0.8 aaa.com
2.查看网页浏览是否正常
aaa.com:8007
aaa.com:8008
3.配置7层负载均衡(lb01和lb02操作一样)
# 1.查看nginx需要的模块
[root@web01 ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --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'
####### 源码安装nginx,在添加一个负载均衡健康检查模块
# 1.安装依赖包
[root@lb01 ~]# yum install -y openssl-devel patch
# 2.下载nginx源码包以及nginx_upstream_check模块第三方模块
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
# 3.解压nginx源码包以及第三方模块
[root@lb01 ~]# tar xf nginx-1.16.1.tar.gz
[root@lb01 ~]# unzip master.zip
# 4.进入nginx目录,打补丁(nginx的版本是1.16补丁就选择1.16的,p1代表在nginx目录,p0是不在nginx目录)
[root@lb01 ~]# cd nginx-1.16.1/
[root@lb01 ~/nginx-1.16.1]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.16.1+.patch
# 5.生成添和加模块
[root@lb01 ~]# ./configure --prefix=/app/nginx-1.16.1 --user=www --group=www --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
# 6.编译和安装
[root@lb01 ~]# make && make install
# 7.做软连接
[root@lb01 ~]# ln -s /app/nginx-1.16.1/ /app/nginx
# 8.设置环境变量并加载
[root@lb01 ~]# vim /etc/profile.d/nginx.sh
export PATH="/app/nginx/sbin:$PATH"
[root@lb01 ~]# source /etc/profile.d/nginx.sh
# 9.编辑配置文件
[root@lb01 ~]# vim /app/nginx/conf/nginx.conf
...
# 自己编辑的配置文件
include /app/nginx/conf.d/*.conf;
# proxy的优化
include /app/nginx/proxy_params;
}
# 9.创建配置文件目录和优化
[root@lb01 ~]# mkdir /app/nginx/conf.d
[root@lb01 ~]# vim /app/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# nginx代理与后端服务器连
proxy_connect_timeout 30;
# nginx代理等待后端服务器的响应时间
proxy_send_timeout 60;
# 后端服务器数据回传给nginx代理超时时间
proxy_read_timeout 60;
## 优化缓冲区
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
# 解决负载均衡常见典型故障
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
# 10.编辑配置文件
[root@lb01 ~]# vim /app/nginx/conf.d/lb7_aaa.conf
upstream aaa {
server 172.16.1.7:8007;
server 172.16.1.8:8008;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
}
server {
listen 80;
server_name aaa.com;
location / {
proxy_pass http://aaa;
include /app/nginx/proxy_params;
}
location /up {
check_status;
}
# 11.检查语法并且重新加载配置文件
[root@lb01 ~]# nginx -t
[root@lb01 ~]# nginx -s reload
7层的nginx端口也可以改
4.负载均衡查看网页
lb01
aaa.com
lb02
健康检查
5.配置4层负载均衡
# 1.安装nginx的官方版本就可以
### 更换源之后下载
[root@web03 ~]# yum install -y nginx
# 2.编辑配置文件(让他认识stream模块)
[root@web03 ~]# vim /etc/nginx/nginx.conf
...
events {
worker_connections 1024;
}
include /etc/nginx/stream/*.conf;
http {
...
# 2.创建目录
[root@web03 ~]# mkdir /etc/nginx/stream
# 3.编辑stream的配置文件
[root@web03 ~]# vim /etc/nginx/stream/lb_4.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;
server 172.16.1.6:80 weight=5;
}
server {
listen 80;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass lb;
}
}
# 4启动并开机自启
[root@web03 ~]# systemctl start nginx
[root@web03 ~]# systemctl enable nginx

浙公网安备 33010602011771号