Kong 是面向现代架构(混合云,混合组织)的下一代 API 网关平台,具有云原生、高性能,易用、可扩展等特性。
适用于 Api Gateway, Kubernetes Ingress, Service Mesh Sidecar 等场景。
主要特性有:
目前我们需要解决的问题
Kong 可以完美的解决以上问题,解决方案如下:
易用性,扩展性: 提供了Restful操作方式,并且有dashboard管理工具
# 创建一个名称 hello 的 upstream
curl -X POST http://localhost:8001/upstreams --data "name=hello"
# 为 hello 添加两个负载均衡节点
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=localhost:8080" --data "weight=100"
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=localhost:8081" --data "weight=100"
# 配置一个service
curl -X POST http://localhost:8001/services --data "name=hello" --data "host=hello"
# 为service 配置路由
curl -X POST http://localhost:8001/routes --data "paths[]=/hello" --data "service.id={$service.id 配置上面service返回的}"
dashboard工具截图
持续集成发布: 与 GitLab CI/CD 配合,代码提交代码库后,自动打包,运行测试用例,蓝绿部署。
当前使用 Kong 架构图
所有节点连接到数据中心
所有节点将周期性执行任务,同步数据最终一致
节点有本地缓存,可以设置缓存过期时间
支持动态扩容,参考上方架构图,Kong集群在Lvs后面
支持 gRPC
# /etc/kong/kong.conf
proxy_listen = 0.0.0.0:8000, 0.0.0.0:8443 ssl, 0.0.0.0:9080 http2
服务监控
注意事项
kong/templates/nginx_kong.lua 模板文件统一 对于kong日志要做好切割,我们使用 logrotate
不同的插件有不同的参数,需要进行设定,设定完成后就会启用
例如:这是内置的 key-auth 插件,作用是进行api认证,设定 key 之后只有认证通过的才能访问
自定义开发和部署,根据业务需要开发插件
基本流程
下载模板
base简单版
simple-plugin
├── handler.lua
└── schema.lua
advanced高级版
complete-plugin
├── api.lua
├── daos.lua
├── handler.lua
├── migrations
│ ├── cassandra.lua
│ └── postgres.lua
└── schema.lua
必要文件就是 handler.lua 和 schema.lua
2. 修改 handler.lua 文件,有很多函数,自定义逻辑就是在这些函数中写kong会在一些特定阶段调用对应的函数
local BasePlugin = require "kong.plugins.base_plugin"
-- The actual logic is implemented in those modules
local access = require "kong.plugins.my-custom-plugin.access"
local body_filter = require "kong.plugins.my-custom-plugin.body_filter"
local CustomHandler = BasePlugin:extend()
function CustomHandler:new()
CustomHandler.super.new(self, "my-custom-plugin")
end
function CustomHandler:access(config)
CustomHandler.super.access(self)
-- Execute any function from the module loaded in `access`,
-- for example, `execute()` and passing it the plugin's configuration.
access.execute(config)
end
function CustomHandler:body_filter(config)
CustomHandler.super.body_filter(self)
-- Execute any function from the module loaded in `body_filter`,
-- for example, `execute()` and passing it the plugin's configuration.
body_filter.execute(config)
end
return CustomHandler
3.修改 schema.lua 文件,主要是配置一些参数,以及参数检查
return {
no_consumer = true, -- this plugin will only be applied to Services or Routes,
fields = {
-- Describe your plugin's configuration's schema here.
},
self_check = function(schema, plugin_t, dao, is_updating)
-- perform any custom verification
return true
end
}
4. 配置部署
插件文件在
/data/kong/plugins/simple-plugin/
则配置kong.conf
lua_package_path = /data/?.lua;($default);
plugins = bundled,simple-plugin
然后重新reload kong 如果lua插件没有错误,就可以在后台看到加载出来了
返回的内容太大,需要加大 buffer(upstream response cache error) 修改配置
nginx_proxy_proxy_buffer_size=128k
nginx_proxy_proxy_buffers=4 256k
nginx_proxy_proxy_busy_buffers_size=256k
需要注意配置属性
如果设置为 true, paths 设置有值,那么请求将会被替换掉
{ "paths": ["/service"], "strip_path": true, "service": { "id": "..." }}
请求:GET /service/path/to/resource HTTP/1.1Host: …
Proxy: GET /path/to/resource HTTP/1.1
{ "paths": ["/version/\d+/service"], "strip_path": true, "service": { "id": "..." }}
请求:GET /version/1/service/path/to/resource HTTP/1.1
Proxy: GET /path/to/resource HTTP/1.1
如果设置为 true,代理后仍然保留 header 请求 host
请求:GET / HTTP/1.1Host: service.com
Proxy: GET / HTTP/1.1Host: service.com
设置为false,将不保留header host
GET / HTTP/1.1Host: service.com
GET / HTTP/1.1Host: <my-service-host.com>
Kong 是行业内较为成熟的网关开源产品,无论是性能,易用,扩展方面都表现不错。
Kong 就是服务治理的翅膀,可以更优雅,更便捷,更智能的实现服务降级,熔断,流量调度等工作。
让我们在自建 Kunernetes 私有云,Hulk云,阿里云等复杂的架构中任意翱翔。