apisix Admin API配置TCP路由转发
apisix Admin API配置TCP路由转发
一、开启和配置stream代理
1、修改/usr/local/apisix/conf/config.yaml文件
新增以下配置
apisix: proxy_mode: http&stream stream_proxy: tcp: - 9100 - "192.168.1.110:9101" udp: - 9200
proxy_mode: 配置开启stream,默认是http,这里修改为http&stream
stream_proxy: 配置tcp和udp信息。
2、重启apisix服务
systemctl restart apisix
二、配置上游服务
curl http://127.0.0.1:9180/apisix/admin/upstreams \ -H "X-API-KEY: $admin_key" -i -X PUT -d ' { "id": "test-upstream-mysql", "name": "mytest-upstream-mysql", "desc": "测试upstream-mysql", "scheme": "tcp", "keepalive_pool": { "idle_timeout": 60, "requests": 1000, "size": 320 }, "nodes": [ { "host": "192.168.1.110", "priority": 0, "port": 3306, "weight": 1 } ], "timeout": { "connect": 6, "send": 6, "read": 6 }, "type": "roundrobin", "checks": { "active": { "http_path": "/", "type": "tcp", "concurrency": 10, "timeout": 3, "healthy": { "http_statuses": [ 200, 302 ], "interval": 3, "successes": 1 }, "unhealthy": { "http_statuses": [ 429, 404, 500, 501, 502, 503, 504, 505 ], "interval": 3, "http_failures": 2, "tcp_failures": 2, "timeouts": 2 } } } }'
三、配置路由
1、配置通用路由,则9100、9101端口都可以转发tcp,9200可以转发udp
curl http://127.0.0.1:9180/apisix/admin/stream_routes -H "X-API-KEY: $admin_key" -X PUT -d ' { "id": "test-stream", "upstream_id": "test-upstream-mysql" }'
2、配置匹配路由,则只有匹配到IP、端口或者IP+端口的情况才做转发。
curl http://127.0.0.1:9180/apisix/admin/stream_routes -H "X-API-KEY: $admin_key" -X PUT -d ' { "id": "test-stream", "server_addr": "192.168.1.110", "server_port": 9101, "upstream_id": "test-upstream-mysql" }'
四、验证匹配路由
[root@archive ~]# mysql -h 192.168.1.110 -P 9100 -uroot -p Enter password: ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
[root@localhost ~]# mysql -h 192.168.1.110 -P 9101 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 ... mysql>