四层负载均衡 -01

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

在这里插入图片描述
在这里插入图片描述

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FikL-09-19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值