四层负载均衡 -01
  uM1yvXrce2fo 2023年11月02日 46 0


一、四层负载均衡

1.什么是四层负载均衡
所谓四层就是基于IP+端口的负载均衡;七层就是基于URL等应用层信息的负载均衡;同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡。 
换句换说,二层负载均衡会通过一个虚拟MAC地址接收请求,然后再分配到真实的MAC地址;三层负载均衡会通过一个虚拟IP地址接收请求,然后再分配到真实的IP地址;四层通过虚拟IP+端口接收请求,然后再分配到真实的服务器;七层通过虚拟的URL或主机名接收请求,然后再分配到真实的服务器。。

四层负载均衡 -01_nginx


四层负载均衡 -01_nginx_02

2.应用场景
1.四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;
2.负载均衡可以做端口转发 #vpn 跳板机 端口转发
3.数据库读写分离
3.四层负载均衡特点
1.四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
2.四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
3.四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同时的使用)
4.四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5.通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。

二、四层负载均衡实践

1.环境准备
 主机           IP                        主机角色              安装软件    

lb4 10.10.0.4,172.16.1.4 四层负载均衡 源码包 nginx

lb01 10.0.0.5, 172.16.1.5 七层负载均衡

lb02 10.0.0.6, 172.16.1.6 七层负载均衡 源码包 nginx
##2.测试lb01
 lb01负载均衡确认没有问题
3.lb4和lb02搭建nginx
1.配置yum源
2.安装
3.配置nginx
4.创建用户
5.启动

#2.测试lb01
lb01负载均衡确认没有问题
3.lb4和lb02搭建nginx
1.配置yum源
2.安装
3.配置nginx
4.创建用户
5.启动
4.将lb01配置同步到lb02
[root@lb01 ~]# scp /etc/nginx/conf.d/* 172.16.1.5:/etc/nginx/conf.d/
[root@lb01 ~]# scp /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/
5.测试lb02的负载均衡
[root@lb02 ~]# 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 ~]# systemctl restart nginx
配置hosts测试
10.0.0.6 linux12.wp.com
6.配置四层负载均衡
1、四层负载均衡语法
Syntax: stream { ... }
Default: —
Context: main


#示例:四层负载均衡stream模块跟http模块在同一级别,不能配置在http里面

stream {
upstream backend {
server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
}

server {
listen80;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
}
2、配置nginx主配置文件
[root@lb4 ~]# vim /etc/nginx/nginx.conf
#注释http层所有内容
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
include /etc/nginx/conf.c/*.conf; #添加一个包含文件
#http {
# include /etc/nginx/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 /var/log/nginx/access.log main;
# sendfile on;
# #tcp_nopush on;
# keepalive_timeout 65;
# #gzip on;
# include /etc/nginx/conf.d/*.conf;
#}
3、配置四层负载均衡
#创建目录
[root@lb4 ~]# mkdir /etc/nginx/conf.c

#配置
[root@lb4 ~]# vim /etc/nginx/conf.c/linux12.lb4.com.conf
stream {
upstream lbserver {
server 10.0.0.5:80;
server 10.0.0.6:80;
}

server {
listen 80;
proxy_pass lbserver;
proxy_connect_timeout 1s;
proxy_timeout 3s;
}
}
4、启动服务
[root@lb4 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb4 ~]# systemctl start nginx
5、配置hosts访问
10.0.0.3 linux12.wp.com linux12.lb.com

#访问
http://linux12.wp.com
6、四层负载均衡配置日志
#四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置是在http以外的;

#如果需要日志则需要配置在stream下面
[root@lb4 ~]# vim /etc/nginx/conf.c/linux12.lb4.com.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 lbserver {
server 10.0.0.5:80;
server 10.0.0.6:80;

}

server {
listen 80;
proxy_pass lbserver;
proxy_connect_timeout 1s;
proxy_timeout 3s;
}
}

#查看所有web服务器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log

三、四层负载端口转发

1.请求负载均衡的125端口,跳转到web01的22端口
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf

#简单配置
stream {
server {
listen 125;
proxy_pass 172.16.1.7:22;
}
}

#一般配置
stream {
upstream ssh_7 {
server 10.0.0.7:22;
}

server {
listen 125;
proxy_pass ssh_7;
}
}
2.请求负载均衡的66端口,跳转至172.16.1.31:1123
stream {
upstream db_31 {
server 172.16.1.31:1123;
}

server {
listen 66;
proxy_pass db_31;
}
}
3.数据库从库的负载均衡
stream {
upstream dbserver {
server 172.16.1.31:1123;
server 172.16.1.32:1123;
server 172.16.1.33:1123;
server 172.16.1.34:1123;
server 172.16.1.35:1123;
}

server {
listen 125;
proxy_pass dbserver;
}
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   2024年05月17日   34   0   0 linuxsvn
  ehrZuhofWJiC   2024年05月17日   38   0   0 KVMlinux
  ehrZuhofWJiC   2024年05月17日   36   0   0 服务器linux
uM1yvXrce2fo