Kong 是面向现代架构(混合云,混合组织)的下一代 API 网关平台,具备云原生、高性能,易用、可扩展等特性。nginx
适用于 Api Gateway, Kubernetes Ingress, Service Mesh Sidecar 等场景。sql
主要特性有:数据库
目前咱们须要解决的问题api
Kong 能够完美的解决以上问题,解决方案以下:缓存
易用性,扩展性: 提供了Restful操做方式,而且有dashboard管理工具bash
# 建立一个名称 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 配合,代码提交代码库后,自动打包,运行测试用例,蓝绿部署。 app
当前使用 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
修改 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
复制代码
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
}
复制代码
配置部署
插件文件在
/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云,阿里云等复杂的架构中任意翱翔。