kylin-四层负载LVS,用Nginx做假四层

四层负载

面试题:LVS的几种工作模式

1.DR模式
2.NAT模式
3.FULL NAT模式
4.TUN隧道模式

面试题:Nginx支持四层负载吗?

支持,但是是假的,不是真四层,只是模拟的四层代理。
可以用LVS来做四层转发。

配置方法

1.新添加一台LB02服务器,并配置

主机名 公网IP 私网IP 部署服务
lb02 10.0.0.6 172.16.1.6 nginx
#同步配置
[root@lb02 ~]# rsync -avz --delete 10.0.0.5:/etc/nginx/ /etc/nginx/
#必要文件添加
[root@lb02 ~]# groupadd -g666 dezyan
[root@lb02 ~]# useradd -u666 -g666 -M -s /sbin/nologin dezyan
#删除配置文件中,本机未安装的模块
[root@lb02 ~]# cd /etc/nginx/conf.d/
[root@lb02 conf.d]# vim lb.conf 
#启动
[root@lb02 conf.d]# systemctl start nginx
[root@lb02 conf.d]# systemctl enable nginx

2.添加四层负载服务器,并配置

1.配置官网仓库
[root@lb ~]# scp 10.0.0.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
2.安装nginx服务
[root@lb ~]# yum -y install nginx
3.删除应用层配置
[root@lb ~]# cd /etc/nginx/
[root@lb conf.d]# rm -rf default.conf	
4.配置四层负载
[root@lb nginx]# cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

### 四层配置
stream {
upstream web {
        server 10.0.0.5:80;
	server 10.0.0.6:80;
}
     server {
     listen 80;
     proxy_pass web;
     }

}
###########
http {
...


5.启动nginx服务
[root@lb nginx]# systemctl start nginx
windows将hosts解析到10.0.0.4
注意浏览器的缓存 
可以使用curl命令测试

3.配置路由转发

配置nginx四层负载均衡实现tcp的转发
配置访问10.0.0.4的2222端口则转发给后端web01的22端口
vim nginx.conf
stream {
..
upstream web01 {
        server 172.16.1.7:22;
        }

     server {
     listen 2222;
     proxy_pass web01;
        }
...
}
[root@lb nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb nginx]# systemctl restart nginx

测试
ssh 10.0.0.4 2222 实际连接的172.16.1.7的22端口
posted @ 2025-03-21 08:46  丁志岩  阅读(21)  评论(0)    收藏  举报