Nginx TCP 反向代理方式
环境准备
注意:使用 Nginx 实现 Apache Doris 数据库的负载均衡,前提是要搭建 Apache Doris 的环境,Apache Doris FE 的 IP 和端口分别如下所示,这里我是用一个 FE 来做演示的,多个 FE 只需要在配置里添加多个 FE 的 IP 地址和端口即可
通过 Nginx 访问 MySQL 的 Apache Doris 和端口如下所示。
IP: 172.31.7.119
端口: 9030
安装依赖
sudo apt-get install build-essential
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
安装 Nginx
sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
sudo tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
sudo ./configure --prefix=/usr/local/nginx --with-stream --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
sudo make && make install
配置反向代理
这里是新建了一个配置文件
vim /usr/local/nginx/conf/default.conf
然后在里面加上下面的内容
events {
worker_connections 1024;
}
stream {
upstream mysqld {
hash $remote_addr consistent;
server 172.31.7.119:9030 weight=1 max_fails=2 fail_timeout=60s;
##注意这里如果是多个FE,加载这里就行了
}
###这里是配置代理的端口,超时时间等
server {
listen 6030;
proxy_connect_timeout 300s;
proxy_timeout 300s;
proxy_pass mysqld;
}
}
启动 Nginx
指定配置文件启动
cd /usr/local/nginx
/usr/local/nginx/sbin/nginx -c conf.d/default.conf
验证
mysql -uroot -P6030 -h172.31.7.119
参数解释:
- -u 指定 Doris 用户名
- -p 指定 Doris 密码,我这里密码是空,所以没有
- -h 指定 Nginx 代理服务器 IP
- -P 指定端口
mysql -uroot -P6030 -h172.31.7.119
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.0 Doris version 0.15.1-rc09-Unknown
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
IP 透传
自 2.1.1 版本开始,Doris 支持 Proxy Protocol 协议。利用这个协议,可以是实现负载均衡的 IP 透传,从而在经过负载均衡后,Doris 依然可以获取客户端的真实 IP,实现白名单等权限控制。
注:
- 仅支持 Proxy Protocol V1。
- 仅支持并做用于 MySQL 协议端口,不支持和影响 HTTP、ADBC 等其他协议端口。
- 开启后,必须使用 Proxy Protocol 协议进行连接,否则连接失败。
下面以 Nginx 为例,介绍如何实现 IP 透传。
-
Doris 开启 Proxy Protocol
在 FE 的 fe.conf 中添加:
enable_proxy_protocol = true
-
Nginx 开启 Proxy Protocol
events { worker_connections 1024; } stream { upstream mysqld { hash $remote_addr consistent; server 172.31.7.119:9030 weight=1 max_fails=2 fail_timeout=60s; } server { listen 6030; proxy_connect_timeout 300s; proxy_timeout 300s; proxy_pass mysqld; # Enable Proxy Protocol to the upstream server proxy_protocol on; } }
-
通过代理连接 Doris
mysql -uroot -P6030 -h172.31.7.119
-
验证
mysql> show processlist; +------------------+------+------+-------------------+---------------------+----------+------+---------+------+-------+-----------------------------------+------------------+ | CurrentConnected | Id | User | Host | LoginTime | Catalog | Db | Command | Time | State | QueryId | Info | +------------------+------+------+-------------------+---------------------+----------+------+---------+------+-------+-----------------------------------+------------------+ | Yes | 1 | root | 172.21.0.32:34390 | 2024-03-17 16:32:22 | internal | | Query | 0 | OK | 82edc460d93f4e28-8bbed058a068e259 | show processlist | +------------------+------+------+-------------------+---------------------+----------+------+---------+------+-------+-----------------------------------+------------------+ 1 row in set (0.00 sec)
如果在 Host 列看到的真实的客户端 IP,则说明验证成功。否则,只能看到代理服务的 IP 地址。
同时,在 fe.audit.log 中也会记录真实的客户端 IP。