- 说明
- KongPlugin的用法
- KongIngress的用法
- 参考
说明
这是API网关Kong的学习笔记中的一篇,使用过程中遇到的问题和解决方法记录在API网关Kong的使用过程中遇到的问题以及解决方法。
API网关Kong学习笔记(二):Kong与Kubernetes集成的方法中介绍过Kong Ingress Controller定义的CustomResourceDefinitions
,那时候了解不多,没详细记录用法。把Kong Ingress Controller的代码读了以后,基本上摸清了它的工作过程,这里详细记录一下KongPlugin
、KongConsumer
、KongIngress
和KongCredential
的用法。
KongPlugin的用法
KongPlugin的格式如下:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:name:
Kong实现了很多插件,每个插件的配置都不相同,这些不同的配置都体现在config
中。
rate-limiting
插件的一个实例可能是这个样子的:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:name: http-svc-consumer-ratelimiting
consumerRef: consumer-team-x
config:hour: 1000limit_by: ipsecond: 100
plugin: rate-limiting
而ip-restriction
插件的一个实例可能是下面这个样子:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:name: echo-ip-restrictionnamespace: demo-echo
disabled: false # optional
plugin: ip-restriction
config:
# whitelist: #用“,”间隔的一组IP或者CIDR,要么白名单、要么黑名单blacklist: 192.168.33.12,172.16.129.1
它们的config
字段的结构是不同的,每个插件的config字段中可以有哪些配置,在每个插件插件的文档中可以找到:rate-limiting,ip-restriction。
KongIngress的用法
KongIngress
中是一些增强配置
的,这一点要特别注意。kong ingress controller会将kubernetes中原生定义的ingress转换成kong中的配置,但是kong的配置是要多于标准ingress中的内容的,多出的这些配置在KongIngress中设置。
KongIngress的结构如下,由upstream
、proxy
和route
三部分组成:
apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:name: configuration-demo
upstream:hash_on: nonehash_fallback: nonehealthchecks:active:concurrency: 10healthy:http_statuses:- 200- 302interval: 0successes: 0http_path: "/"timeout: 1unhealthy:http_failures: 0http_statuses:- 429interval: 0tcp_failures: 0timeouts: 0passive:healthy:http_statuses:- 200successes: 0unhealthy:http_failures: 0http_statuses:- 429- 503tcp_failures: 0timeouts: 0slots: 10
proxy:protocol: httppath: /connect_timeout: 10000retries: 10read_timeout: 10000write_timeout: 10000
route:methods:- POST- GETregex_priority: 0strip_path: falsepreserve_host: trueprotocols:- http- https
upstream
中配置的负载均衡算法、健康检查方法等,proxy
中设置的kong与backend server通信的时超时时间、重试次数等,route
中设置的是路由匹配的协议、方法以及路由的优先级。