概述
KONG的核心对象为:upstream、target、service、route
- upstream: 是对上游服务器的抽象;
- target: 代表了一个物理服务,是 ip + port 的抽象;
- service: 是抽象层面的服务,他可以直接映射到一个物理服务(host 指向 ip + port),也可以指向一个upstream 来做到负载均衡;
- route: 是路由的抽象,他负责将实际的 request 映射到 service。
KONG监听的端口为:8000、8001、8443、8444、8100
- 8444: 带ssl的管理端口
- 8000/8443: 分别是用来监听来自客户端的Http 和 Https请求,等价于 Nginx 默认的 80 端口
- 8001: 端口便是默认的管理端口,可以通过 HTTP Restful API 来动态管理 Kong 的配置
- 8100: 为prometheus获取metrics指标端口
案例
Nginx Conf01:
upstream helloUpstream {
server localhost:3000 weight=100;
}
server {
listen 80;
location /hello {
proxy_pass http://helloUpstream;
}
}如上简单的 Nginx 配置,可以转换为如下的 Http 请求。
Kong实现
1)配置 upstream
# curl -X POST http://10.0.0.207:8001/upstreams --data "name=testUpstream"2)配置 target
# curl -X POST http://10.0.0.207:8001/upstreams/testUpstream/targets --data "target=localhost:3000" --data "weight=100"3)配置 service
# curl -X POST http://10.0.0.207:8001/services --data "name=hello" --data "host=testUpstream"
输出:"id":"f3ed44ca-908f-4595-b26e-1a543740e642"4)配置 route
# curl -X POST http://10.0.0.207:8001/routes --data "paths[]=/hello" --data "service.id=f3ed44ca-908f-4595-b26e-1a543740e642"
# curl -X POST http://10.0.0.207:8001/routes --data "paths[]=/hello" --data "name=routehello" --data "service.name=hello"这一切都是动态的,无需手动 reload nginx.conf 为 Kong 新增路由信息时涉及到了 upstream,target,service,route 等概念,便是 Kong 最核心的四个对象。
为 hello 服务添加50次/秒的限流:
# curl -X POST http://localhost:8001/services/hello/plugins \
--data "name=rate-limiting" \
--data "config.second=50"为 hello 服务添加 jwt 插件:
# curl -X POST http://localhost:8001/services/login/plugins \
--data "name=jwt"插件也可以安装在 route 之上
# curl -X POST http://localhost:8001/routes/{routeId}/plugins \
--data "name=rate-limiting" \
--data "config.second=50"
# curl -X POST http://localhost:8001/routes/{routeId}/plugins \
--data "name=jwt"Web管理界面
这里不赘述
