TCP/UDP 动态代理

众多的闻名的应用和服务,像 LDAP、 MYSQL 和 RTMP ,选择 TCP 作为通信协议。 但是像 DNS、 syslog 和 RADIUS 这类非事务性的应用,他们选择了 UDP 协议。

APISIX 可以对 TCP/UDP 协议进行代理并实现动态负载均衡。 在 nginx 世界,称 TCP/UDP 代理为 stream 代理,在 APISIX 这里我们也遵循了这个声明。

如何开启 Stream 代理

conf/config.yaml 配置文件设置 stream_proxy 选项, 指定一组需要进行动态代理的 IP 地址。默认情况不开启 stream 代理。

  1. apisix:
  2. stream_proxy: # TCP/UDP proxy
  3. tcp: # TCP proxy address list
  4. - 9100
  5. - "127.0.0.1:9101"
  6. udp: # UDP proxy address list
  7. - 9200
  8. - "127.0.0.1:9211"

如果 apisix.enable_admin 为 true,上面的配置会同时启用 HTTP 和 stream 代理。

如果你设置 enable_admin 为 false,且需要同时启用 HTTP 和 stream 代理,设置 only 为 false:

  1. apisix:
  2. enable_admin: false
  3. stream_proxy: # TCP/UDP proxy
  4. only: false
  5. tcp: # TCP proxy address list
  6. - 9100

如何设置 route

简例如下:

  1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "remote_addr": "127.0.0.1",
  4. "upstream": {
  5. "nodes": {
  6. "127.0.0.1:1995": 1
  7. },
  8. "type": "roundrobin"
  9. }
  10. }'

例子中 APISIX 对客户端 IP 为 127.0.0.1 的请求代理转发到上游主机 127.0.0.1:1995。 更多用例,请参照 test case

更多 route 匹配选项

我们可以添加更多的选项来匹配 route。目前 Stream Route 配置支持 3 个字段进行过滤:

  • server_addr: 接受 Stream Route 连接的 APISIX 服务器的地址。
  • server_port: 接受 Stream Route 连接的 APISIX 服务器的端口。
  • remote_addr: 发出请求的客户端地址。

例如

  1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "server_addr": "127.0.0.1",
  4. "server_port": 2000,
  5. "upstream": {
  6. "nodes": {
  7. "127.0.0.1:1995": 1
  8. },
  9. "type": "roundrobin"
  10. }
  11. }'

例子中 APISIX 会把服务器地址为 127.0.0.1, 端口为 2000 代理到上游地址 127.0.0.1:1995

让我们再举一个实际场景的例子:

  1. 将此配置放在 config.yaml

    1. apisix:
    2. stream_proxy: # TCP/UDP proxy
    3. tcp: # TCP proxy address list
    4. - 9100 # by default uses 0.0.0.0
    5. - "127.0.0.10:9101"
  2. 现在运行一个 mysql docker 容器并将端口 3306 暴露给主机

    1. $ docker run --name mysql -e MYSQL_ROOT_PASSWORD=toor -p 3306:3306 -d mysql
    2. # check it using a mysql client that it works
    3. $ mysql --host=127.0.0.1 --port=3306 -u root -p
    4. Enter password:
    5. Welcome to the MySQL monitor. Commands end with ; or \g.
    6. Your MySQL connection id is 25
    7. ...
    8. mysql>
  3. 现在我们将创建一个带有服务器过滤的 stream 路由:

    1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "server_addr": "127.0.0.10",
    4. "server_port": 9101,
    5. "upstream": {
    6. "nodes": {
    7. "127.0.0.1:3306": 1
    8. },
    9. "type": "roundrobin"
    10. }
    11. }'

    每当 APISIX 服务器 127.0.0.10 和端口 9101 收到连接时,它只会将请求转发到 mysql 上游。让我们测试一下:

  4. 9100 发出请求(在 config.yaml 中启用 stream 代理端口),过滤器匹配失败。

    1. $ mysql --host=127.0.0.1 --port=9100 -u root -p
    2. Enter password:
    3. ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2

    下面的请求匹配到了 stream 路由,所以它可以正常代理到 mysql。

    1. mysql --host=127.0.0.10 --port=9101 -u root -p
    2. Enter password:
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 26
    5. ...
    6. mysql>

完整的匹配选项列表参见 Admin API 的 Stream Route

接收 TLS over TCP 连接

APISIX 支持接收 TLS over TCP 连接。

首先,我们需要给对应的 TCP 地址启用 TLS:

  1. apisix:
  2. stream_proxy: # TCP/UDP proxy
  3. tcp: # TCP proxy address list
  4. - addr: 9100
  5. tls: true

接着,我们需要为给定的 SNI 配置证书。 具体步骤参考 Admin API 的 SSL。 mTLS 也是支持的,参考 保护路由

然后,我们需要配置一个 route,匹配连接并代理到上游:

  1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "upstream": {
  4. "nodes": {
  5. "127.0.0.1:1995": 1
  6. },
  7. "type": "roundrobin"
  8. }
  9. }'

当连接为 TLS over TCP 时,我们可以通过 SNI 来匹配路由,比如:

  1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "sni": "a.test.com",
  4. "upstream": {
  5. "nodes": {
  6. "127.0.0.1:5991": 1
  7. },
  8. "type": "roundrobin"
  9. }
  10. }'

在这里,握手时发送 SNI a.test.com 的连接会被代理到 127.0.0.1:5991

代理到 TLS over TCP 上游

APISIX 还支持代理到 TLS over TCP 上游。

  1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "upstream": {
  4. "scheme": "tls",
  5. "nodes": {
  6. "127.0.0.1:1995": 1
  7. },
  8. "type": "roundrobin"
  9. }
  10. }'

通过设置 scheme 为 tls,APISIX 将与上游进行 TLS 握手。

当客户端也使用 TLS over TCP,客户端发送的 SNI 将传递给上游。否则,将使用一个假的 SNI “apisix_backend”。