代理传输层 (L4) 流量
默认情况下,APISIX 作为应用层 (L7) 代理运行。APISIX 还支持处理传输层 (L4) TCP 和 UDP 流量,既可以单独处理,也可以在处理应用层 (L7) 流量的基础上处理。
本指南将向你展示如何配置 APISIX 以代理传输层 (L4) 流量,并配置 流路由 以建立与 MySQL 服务器的连接。
前置条件
- 安装 Docker。
- 安装 cURL 以向服务发送请求进行验证。
- 按照 快速入门教程 在 Docker 或 Kubernetes 中启动一个新的 APISIX 实例。
- 安装 MySQL Shell 以启动与 MySQL 服务器的连接。
启动 MySQL 服务器
- Docker
- Kubernetes
启动一个 MySQL 实例作为示例上游服务,并将 root 密码配置为 my-secret-pw:
docker run -d \
--name mysql \
--network=apisix-quickstart-net \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
mysql:9.4
为示例上游服务创建一个 Kubernetes 清单文件,root 密码为 my-secret-pw:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: ingress-apisix
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:9.4
env:
- name: MYSQL_ROOT_PASSWORD
value: "my-secret-pw"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: ingress-apisix
spec:
selector:
app: mysql
ports:
- name: mysql
port: 3306
targetPort: 3306
将配置应用到你的集群:
kubectl apply -f mysql.yaml
启用传输层 (L4) 代理
默认情况下,APISIX 仅启用应用层 (L7) 代理。要同时代理传输层 (L4) 流量,请配置 proxy_mode 和 stream_proxy。
- Docker
- Kubernetes
更新 config.yaml 配置文件 如下:
docker exec apisix-quickstart /bin/sh -c "echo '
apisix:
enable_control: true
control:
ip: 0.0.0.0
port: 9092
// Annotate 1
proxy_mode: http&stream
// Annotate 2
stream_proxy:
tcp:
- 9100
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key_required: false
allow_admin:
- 0.0.0.0/0
plugin_attr:
prometheus:
export_addr:
ip: 0.0.0.0
port: 9091
' > /usr/local/apisix/conf/config.yaml"
❶ proxy_mode:同时接受传输层 (L4) 和应用层 (L7) 流量。
❷ stream_proxy:配置传输层 (L4) 代理的接口。
重新加载 APISIX 以使配置更改生效:
docker exec apisix-quickstart apisix reload
如果你使用 快速入门 在 Docker 中启动 APISIX,端口 9100 已经映射 (-p 9100:9100)。
升级你的网关以启用流模式并设置 TCP 端口 9100:
helm upgrade -n ingress-apisix apisix apisix/apisix \
--set ... \ # add other parameters
--set "service.stream.enabled=true" \
--set "service.stream.tcp[0]=9100"
创建流路由
创建一个 流路由 并配置 MySQL 服务器作为上游服务。
- Admin API
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/stream_routes" -X PUT -d '
{
"id": "stream-route-mysql",
"server_port": 9100,
"upstream": {
"nodes": {
"mysql:3306": 1
},
"type": "roundrobin"
}
}'
- Gateway API
- APISIX CRD
首先,按如下方式更新你的 Gateway 清单:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: ingress-apisix
name: apisix
spec:
gatewayClassName: apisix
listeners:
- name: http
protocol: HTTP
port: 80
- name: tcp
protocol: TCP
port: 9100
allowedRoutes:
kinds:
- kind: TCPRoute
infrastructure:
parametersRef:
group: apisix.apache.org
kind: GatewayProxy
name: apisix-config
为 TCPRoute 创建一个 Kubernetes 清单:
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TCPRoute
metadata:
name: stream-route-mysql
namespace: ingress-apisix
spec:
parentRefs:
- name: apisix
sectionName: tcp
rules:
- backendRefs:
- name: mysql
port: 3306
将配置应用到你的集群:
kubectl apply -f gateway.yaml -f tcp-route.yaml
为流路由创建一个 Kubernetes 清单文件:
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: stream-route-mysql
namespace: ingress-apisix
spec:
ingressClassName: apisix
stream:
- name: stream-route-mysql
protocol: TCP
match:
ingressPort: 9100
backend:
serviceName: mysql
servicePort: 3306
将配置应用到你的集群:
kubectl apply -f tcp-route.yaml
验证
- Docker
- Kubernetes
为服务转发端口 9100:
kubectl port-forward svc/apisix-gateway 9100:9100 &
以 root 身份连接 MySQL 服务器,并在提示时输入密码 my-secret-pw:
mysql --host=127.0.0.1 --port=9100 -u root -p
如果成功,你应该看到类似以下的欢迎文本:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 9.4.0 MySQL Community Server - GPL
Copyright (c) 2000, 2025, Oracle and/or its affiliates.
下一步
当接受来自下游客户端的请求或代理到上游服务时,APISIX 还支持 TCP 连接上的 TLS 作为传输层 (L4) 代理。请参阅配置 TCP 上的 TLS 操作指南以了解更多信息(即将推出)。