Nginx四层负载

什么是四层负载均衡

OSI七层模型中,四层是传输层,传输层使用端口到端口的通信方式

四层负载均衡,就是在传输层做端口的转发(端口映射)

四层负载应用场景

  • 四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;如:nginx无法保证自己的服务高可用,需要依赖LVS或者keepalive。
  • 如:TCP协议的负载均衡,有些请求是TCP协议的(mysql、ssh),或者说这些请求只需要使用四层进行端口的转发就可以,所以使用四层负载均衡。

nginx四层负载

负载均衡的软件:

  • nginx

    • 四层负载(nginx1.9版本以上会有stream模块,只有这个模块才可以做四层负载)
      • stream
    • 七层负载
      • upstream
  • LVS

    • 四层负载
  • HAproxy

    • 四层负载

      七层负载

四层负载均衡配置

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 七层负载 nginx
lb02 10.0.0.6 172.16.1.6 四层负载 nginx
web01 10.0.0.7 172.16.1.7 web网站 nginx、php
web02 10.0.0.8 172.16.1.8 web网站 nginx、php
db01 10.0.0.51 172.16.1.51 数据库 MariaDB

部署四层负载

## 1.添加nginx官方源
[root@lb02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

## 2.安装nginx
[root@lb02 ~]# yum install -y nginx

## 3.配置四层负载
[root@lb02 ~]# vim /etc/nginx/nginx.conf
stream {
    upstream backend {
        server 172.16.1.5:80;
    }
    server {
            listen 90; 
            proxy_pass backend;
    }
}

使用stream做端口转发

实现22端口转发

## 在10.0.0.6机器上开8001端口,映射到10.0.0.8的22端口

## 1.创建include配置文件目录
[root@lb02 ~]# mkdir /etc/nginx/stream.d

## 2.修改主配置文件,添加include
[root@lb02 ~]# vim /etc/nginx/nginx.conf
include /etc/nginx/stream.d/*.conf;

## 3.实现456映射22端口需求
[root@lb02 ~]# vim /etc/nginx/stream.d/8001_22.conf
stream {
        upstream web02_ssh {
                server 172.16.1.8:22;
        }
        server {
                listen 8001;
                proxy_pass web02_ssh;
        }

[root@web01 ~]# ssh 172.16.1.6 -p 8001
root@172.16.1.6's password: 
Last login: Sun Jun 19 14:18:26 2022 from 10.0.0.1
[root@web02 ~]# 

映射数据库端口

[root@lb02 ~]# vim /etc/nginx/stream.d/8001_22.conf
stream {
        upstream web02_ssh {
                server 172.16.1.8:22;
        }
        server {
                listen 8001;
                proxy_pass web02_ssh;
        }

        upstream db01_mysql {
                server 172.16.1.51:3306;
        }
	server {
		listen 7777;
		proxy_pass db01_mysql;
	}	
}

[root@web01 ~]# mysql -uwp_user -p -h172.16.1.6 -P 7777
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

映射redis端口

[root@lb02 ~]# vim /etc/nginx/stream.d/8001_22.conf 
stream {
        upstream web02_ssh {
                server 172.16.1.8:22;
        }
        server {
                listen 8001;
                proxy_pass web02_ssh;
        }

        upstream db01_mysql {
                server 172.16.1.51:3306;
        }
	server {
		listen 7777;
		proxy_pass db01_mysql;
	}
	upstream db01_redis {
		server 172.16.1.51:6379;
	}
	server {
		listen 2022;
		proxy_pass db01_redis;
	}
}

127.0.0.1:6379> keys *
1) "wsh"
2) "username"
3) "chinakey"

172.16.1.6:2022> keys *
1) "wsh"
2) "username"
3) "chinakey"