使用 Kong Ingress 控制器插件

Kong Ingress 控制器 (KIC) 在您的 minikube 服务器上运行。

  1. 启动 minikube

    minikube start
    

    配置所有资源需要几分钟时间。

    kubectl get nodes
    

部署 Kong Ingress 控制器

通过 minikube 命令启用 Kong Ingress 控制器。

$ minikube addons enable kong

🌟  The 'kong' addon is enabled

注意:首次执行此过程可能需要五分钟。

设置环境变量

接下来,我们将使用 Kong 可访问的 IP 地址设置一个环境变量。我们可以使用它向 Kubernetes 集群发送请求。

$ export PROXY_IP=$(minikube service -n kong kong-proxy --url | head -1)
$ echo $PROXY_IP
http://192.168.99.100:32728

或者,您可以使用 minikube tunnel 命令。


# open another terminal window and run
minikube tunnel

# you may need to enter an admin password because minikube need to use ports 80 and 443 

让我们测试一下 KIC 是否正在运行。

$ curl -v localhost

*   Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.86.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Date: Wed, 03 May 2023 01:34:31 GMT
< Content-Type: application/json; charset=utf-8
< Connection: keep-alive
< Content-Length: 48
< X-Kong-Response-Latency: 0
< Server: kong/3.2.2
<
* Connection #0 to host localhost left intact
{"message":"no Route matched with those values"}%

创建 Ingress 对象

要代理请求,您需要一个上游应用程序来代理。部署此 echo 服务器提供了一个简单的应用程序,该应用程序返回有关其正在运行的 Pod 的信息

echo "
apiVersion: v1
kind: Service
metadata:
  labels:
    app: echo
  name: echo
spec:
  ports:
  - port: 1025
    name: tcp
    protocol: TCP
    targetPort: 1025
  - port: 1026
    name: udp
    protocol: TCP
    targetPort: 1026
  - port: 1027
    name: http
    protocol: TCP
    targetPort: 1027
  selector:
    app: echo
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: echo
  name: echo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: echo
    spec:
      containers:
      - image: kong/go-echo:latest
        name: echo
        ports:
        - containerPort: 1027
        env:
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
        resources: {}
" | kubectl apply -f -

接下来,我们将创建路由配置,以将 /echo 请求代理到 echo 服务器

echo "
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: echo
  annotations:
    konghq.com/strip-path: 'true'
spec:
  ingressClassName: kong
  rules:
  - host: kong.example
    http:
      paths:
      - path: /echo
        pathType: ImplementationSpecific
        backend:
          service:
            name: echo
            port:
              number: 1027
" | kubectl apply -f -

让我们测试一下我们的 ingress 对象。

$ curl -i localhost/echo -H "Host: kong.example"

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 133
Connection: keep-alive
Date: Wed, 03 May 2023 01:59:25 GMT
X-Kong-Upstream-Latency: 1
X-Kong-Proxy-Latency: 1
Via: kong/3.2.2

Welcome, you are connected to node minikube.
Running on Pod echo-f4fdf987c-qdv7s.
In namespace default.
With IP address 10.244.0.6.

下一步

注意: 在官方文档中阅读有关 KIC 和不同用例的更多信息。


上次修改时间:2024 年 5 月 10 日:修复指向 Kong 文档的损坏链接 (95e82224c)