bobenut / me

1 stars 0 forks source link

Kong Ingress Controller实践Part2,为路由附加插件 #77

Open bobenut opened 1 year ago

bobenut commented 1 year ago

目标

为Ingress编排的路由,附加KongPlug,实现能够修改响应头的效果,实践KongPlug的基本附加方法。

准备

按照上一篇Kong Ingress Controller实践Part1,编排路由,准备好上游服务,调通编排的API。

为路由加上KongPlugin,修改响应头

  1. 执行并创建一个KongPlugin,插件使用response-transformer为响应头增加内容。
    echo '
    apiVersion: configuration.konghq.com/v1
    kind: KongPlugin
    metadata:
    name: add-response-header
    config:
    add:
    headers:
    - "demo: injected-by-kong"
    plugin: response-transformer
    ' | sudo kubectl apply -f -
    kongplugin.configuration.konghq.com/add-response-header created
  2. KongPlugin应用到Ingress上,应用Plugin是通过在Ingress上的注解中增加要应用的插件名称来实现。
    sudo kubectl patch ingress demo -p '{"metadata":{"annotations":{"konghq.com/plugins":"add-response-header"}}}'
ingress.networking.k8s.io/demo patched
  1. 访问/foo,观察响应中追加了一行内容。
    GET http://172.16.30.251:32736/foo/status/200 HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: close
Server: gunicorn/19.9.0
Date: Sun, 01 Jan 2023 16:18:51 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
demo: injected-by-kong
X-Kong-Upstream-Latency: 3
X-Kong-Proxy-Latency: 1
Via: kong/3.0.1.0-enterprise-edition

参考

Using KongPlugin resource