四层负载均衡是基于传输层协议包来封装的(如:TCP/IP),那咱们前面使用到的七层是指的应用层,他的组装在四层 的基础之上,不管四层仍是七层都是指的OSI网络模型。css
一、四层+七层来作负载均衡,四层能够保证七层的负载均衡的高可用性;如:nginx就没法保证本身的服务高可用,需 要依赖LVS或者keepalive。 二、如:tcp协议的负载均衡,有些请求是TCP协议的(mysql、ssh),或者说这些请求只须要使用四层进行端口的转发 就能够了,因此使用四层负载均衡。
四层+七层构建大规模集群架构使用场景
mysql
四层负载均衡仅能转发TCP/IP协议、UDP协议、一般用来转发端口,如:tcp/2二、udp/53;nginx
四层负载均衡能够用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)web
四层负载均衡能够解决七层负载均衡高可用问题;(多台后端七层负载均衡能同事的使用)sql
四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;vim
一般大并发场景一般会选择使用在七层负载前面增长四层负载均衡。后端
Nginx如何配置四层负载均衡
centos
一、经过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务; 二、经过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。
先配置两台lb负载均衡
浏览器
[root@lb02 ~]# cat /etc/yum.repos.d/nginx.repo [nginx‐stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key #在lb02上安装nginx [root@lb02 yum.repos.d]# yum install ‐y nginx #在lb02上同步lb01的全部nginx相关配置 [root@lb02 ~]# scp ‐r root@172.16.1.5:/etc/nginx /etc/ #启动nginx [root@lb02 conf.d]# nginx ‐t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb02 conf.d]# systemctl enable nginx Created symlink from /etc/systemd/system/multi‐user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@lb02 conf.d]# systemctl start nginx
①建立存放四层负载均衡配置文件的目录网络
[root@lb02 ~]# vim /etc/nginx/nginx.conf events { .... } include /etc/nginx/conf.c/*.conf; http { ..... } [root@lb02 ~]# mkdir /etc/nginx/conf.c
②配置四层负载均衡
[root@web03 conf.c]# cat lb_domain.conf stream { 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; } } [root@web03 conf.c]# nginx ‐t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web03 conf.c]# systemctl reload nginx #配置本机hosts解析后浏览器访问并查看nginx日志
③四层负载均衡开启日志
#四层负载均衡是没有access的日志的,由于在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置是在http之外的; #若是须要日志则须要配置在stream下面 [root@web03 conf.c]# cat lb_domain.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 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; } }
①使用nginx四层负载均衡实现tcp的转发
请求负载均衡 5555 ‐‐‐> 172.16.1.7:22; 请求负载均衡 6666 ‐‐‐> 172.16.1.51:3306;
②配置nginx四层负载均衡实现tcp的转发
[root@lb4‐01 ~]# cat /etc/nginx/conf.c/lb_domain.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; #定义转发ssh的22端口 upstream ssh_7 { server 10.0.0.7:22; } #定义转发mysql的3306端口 upstream mysql_51 { server 10.0.0.51:3306; } server { listen 5555; proxy_connect_timeout 3s; proxy_timeout 300s; proxy_pass ssh_7; } server { listen 6666; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass mysql_51; } }