Nginx四层负载均衡详解

Nginx四层负载均衡详解

2023年7月19日发(作者:)

Nginx四层负载均衡详解Nginx四层负载均衡什么是四层负载均衡四层负载均衡基于传输层协议包来封装的(如:TCP/IP),那我们前⾯使⽤的七层指的是应⽤层,它的组装在四层基础之上,⽆论是四层还是七层都是这是OSI⽹络模型。四层负载均衡应⽤场景1. 四层+七层来做负载均衡,四层可以保证七层的负载均衡的⾼可⽤性。如nginx就⽆法保证⾃⼰的服务⾼可以⽤,需要依赖lvs或者keepalive来做。2. 如 : TCP协议的负载均衡,有些请求是TCP协议的(mysql,ssh),或者说这些请求只需要使⽤四层进⾏端⼝的转发就可以了,所以使⽤四层负载均衡。⽐如:mysql读的负载均衡(轮询);⽐如:端⼝映射。四层负载均衡总结1. 四层负载均衡仅能转发TCP/IP协议、UDP协议,通常⽤来转发端⼝,如 : tcp/3306、tcp/22、udp/53。2. 四层负载均衡可以⽤来解决七层负载均衡的端⼝限制问题(七层负载均衡最⼤使⽤65535个端⼝号)。3. 可以⽤来解决七层负载均衡的⾼可⽤问题(多台后端七层负载聚会能同时的使⽤)。4. 四层的转发效率⽐七层的效率⾼的多,但是仅⽀持tcp/ip协议,不⽀持http协议或者https协议。实验环境lb03-4层负载 192.168.15.3 172.16.1.3lb01-7层负载 192.168.15.5 172.16.1.5

lb02-7层负载 192.168.15.6 172.16.1.6nfs 192.168.15.31 172.16.1.31mysql/redis 192.168.15.51 172.16.1.51

web01 192.168.15.7 172.16.1.7web02 192.168.15.8 172.16.1.8我们这⼀次的实验,是接着Nginx七层负载均衡搭建完成后继续搭建的。注意 : 我们lb01服务器⾥⾯有⼀个nginx的健康检查模块,本次实验是不需要的。同步lb01服务器七层负载均衡⾄lb02服务器1. 在lb02上⾯配置nginx的yum源vim /etc/.d/me=nginx stable repobaseurl=/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=/keys/nginx_ule_hotfixes=truename=nginx mainline repobaseurl=/packages/mainline/centos/$releasever/$basearch/gpgcheck=1enabled=0gpgkey=/keys/nginx_ule_hotfixes=true2. 在lb02上⾯安装nginx服务yum -y install nginx3. 将lb01上⾯的配置⽂件拷贝到lb02服务器上scp -rp root@172.16.1.5:/etc/nginx /etc/ #

在lb02上⾯执⾏4. 编译从lb01服务器上拷贝到lb02服务器的多余的配置⽂件gzip proxy_ip 5. 查看nginx配置⽂件指定⽬录下的⽂件名ls /etc/nginx/conf.d# proxy_ proxy_ proxy_6. 启动nginxnginx -tsystemctl restart nginxsystemctl enable nginx我们输⼊会出现502报错,原因是因为WeCenter软件默认开启了session缓存。我们⽬前搭建的三个服务,只有phpmyadmin需要将session写⼊radis,wordpress和WeCenter都是默认将session写⼊数据库的。我们需要关闭web01服务器和web02服务器的session缓存。1. 关闭web01服务器session缓存vim /etc/_start = 0 #

第1294⾏2. 重启web01服务器的nginx服务和php服务systemctl restart nginxsystemctl restart php-fpm3. 关闭web02服务器的session缓存vim /etc/_start = 0 #

第1294⾏4. 重启web02服务器的nginx服务和php服务systemctl restart nginxsystemctl restart php-fpm测试1. 在本机添加域名解析C:WindowsSystem32driversetc在hosts⽂件添加域名解析# 192.168.15.5 192.168.15.6 - 测试我们先打开本机的cmd,输⼊ping 。查看⼀下域名解析的ip地址是不是lb02的ip地址(192.168.15.6)。我们打开浏览器输⼊ ; ; 。如果都可以正常访问到⽹页的页⾯,那么就表⽰我们lb02七层负载均衡搭建成功了。配置lb03服务器Nginx四层负载均衡配置四层负载均衡,需要⽤到stream模块,我们可以先通过nginx -V命令查看当前安装的nginx版本有没有--with-stream模块。注意 : 四层负载均衡和http层是没有任何关系的,不能把四层负载均衡配置在http层⾥⾯! ! !- lb03配置nginx四层负载均衡1. 配置nginx的yum源vim /etc/.d/me=nginx stable repobaseurl=/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=/keys/nginx_ule_hotfixes=truename=nginx mainline repobaseurl=/packages/mainline/centos/$releasever/$basearch/gpgcheck=1enabled=0gpgkey=/keys/nginx_ule_hotfixes=true2. 下载nginx软件包yum -y install nginx3. 修改配置⽂件3.1 在主配置⽂件中添加四层负载均衡配置的⽂件路径vim /etc/nginx/ents { ....}include /etc/nginx/conf.c/*.conf;#

注意:

不能把四层负载均衡配置⽂件写在http层⾥⾯#

不可以把配置⽂件的路径写成/etc/nginx/conf.d,因为http层⾥⾯包含了/etc/nginx/conf.d⽂件路径,会发⽣冲突。http { ....}3.2 创建四层负载均衡的配置⽂件mkdir /etc/nginx/ /etc/nginx/ lb_ream{ upstream lb { #

创建虚拟服务池,定义虚拟服务池的名称为lb server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s; #

将服务器lb01添加⾄定义好的虚拟服务池lb server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s; #

将服务器lb02添加⾄定义好的虚拟服务池lb } server { listen 80; #

四层负载均衡监听的在80端⼝ proxy_connect_timeout 3s; #

连接超时时间 proxy_timeout 3s; #

响应超时时间 proxy_pass lb; #

指定资源池名称,注意四层负载均衡不能写http名称 }}4. 删除lb03服务器中http层监听端⼝的配置⽂件rm -rf /etc/nginx/conf.d/#

四层负载均衡默认监听的80端⼝和七层负载均衡默认监听的80端⼝发⽣冲突,⽆法启动服务# lb03服务器我们只是⽤来做四层负载均衡,不需要http层来监听端⼝,直接删除⽂件即可。5. 重启nginx服务nginx -tsystemctl start nginxsystemctl enable nginx- 测试在本机添加域名解析C:WindowsSystem32driversetc在hosts⽂件添加域名解析(注释掉其他的域名解析)192.168.15.3 打开cmd,输⼊ping ,解析的ip地址为192.168.15.3。我们打开浏览器,输⼊,可以正常访问到页⾯,这就代表客户端传输的数据包是由四层负载进⾏转发的。四层负载均衡记录⽇志Nginx软件的⽇志是在主配置⽂件中通过log_format来定义的。我们打开/etc/nginx/主配置⽂件,发现只有http层有⼀个log_format。http层属于七层负载均衡的协议,跟四层负载均衡是没有任何关系的,所以我们在四层负载均衡中是看不到⽇志⽂件的内容的。我们需要在四层负载均衡stream下重新定义⽇志的参数。1. 编辑四层负载均衡的配置⽂件vim /etc/nginx/conf.c/lb_ream{

log_format proxy '$remote_addr $remote_port - $msec - [$time_local] $status $protocol' #

远程客户端地址

远程客户端随机端⼝

毫秒

时间

状态

协议 '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"'; #

虚拟链接池的主机地址

⼤⼩

连接的时间

access_log /var/log/nginx/ proxy; #

⽇志的路径

使⽤的变量为proxy upstream lb { server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s; server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s; }

server { listen 80; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass lb; }

}

2. 重启nginx服务systemctl restart nginx- 测试在本机添加域名解析C:WindowsSystem32driversetc在hosts⽂件添加域名解析(注释掉其他的域名解析)192.168.15.3 我们打开浏览器输⼊:,在lb03服务器四层负载均衡服务器中输⼊tail -f /var/log/nginx/不断刷新浏览器,我们会发现lb03服务器中⽇志中Tcp连接的ip地址是在lb01服务器和lb02服务器中来回切换的。使⽤Nginx负载均衡代理tcp协议,实现端⼝映射请求负载均衡 : 端⼝ 5555 ===> 172.16.1.7 : 22请求负载均衡 : 端⼝ 6666 ===> 172.16.1.51 : 33061. 修改Nginx四层负载的配置⽂件vim /etc/nginx/conf.c/lb_ream{log_format proxy '$remote_addr $remote_port - $msec - [$time_local] $status $protocol' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"'; access_log /var/log/nginx/ proxy;#

转发ssh的22端⼝ upstream ssh_7 { server 172.16.1.7:22; }#

转发mysql的3306端⼝ upstream mysql_51 { server 172.16.1.51:3306; } server { listen 5555; proxy_connect_timeout 60s; proxy_timeout 60s; proxy_pass ssh_7; } server { listen 6666; proxy_connect_timeout 60s; proxy_timeout 60s; proxy_pass mysql_51; }}2. 重启nginx服务systemctl restart nginx3. 查看端⼝号netstat -lntp# tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN 17681/nginx: master# tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 17681/nginx: master

- 测试我们打开Xsehll软件,输⼊ssh root@192.168.15.3:5555。可以直接登陆web01(172.16.1.7)服务器。我们打开Navicat软件,输⼊ip:192.168.15.3;输⼊port:6666。可以直接连接上db01(172.16.1.51)的数据库。代理与转发之间的差别

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689741514a282614.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信