Kubernetes Sidecar 注入

该插件将注入Kong数据平面节点并在Kubernetes之上形成服务网格

介绍

Kong 0.15.0 / 1.0.0增加了代理和路由原始tcptls流的能力,并使用服务网格Sidecar模式和Kong节点之间的相互tls来部署Kong。本教程将引导您使用我们的 Kubernetes Sidecar 注入插件在Kubernetes上设置Kong服务网格。

准备条件

您需要在Kubernetes上运行Kong 1.0.0或更高版本,包括存储为可用于Kong控制平面的机密的SSL证书。来自Kong Kubernetes Repository的Make任务run_cassandrarun_postgres将完全配置必备数据存储,Kong控制平面,Kong数据平面和SSL机密。

或者,按照Kong Kubernetes Install Instructions页面中的任何设置说明进行操作,然后设置SSL证书/密码:

  1. cd $(mktemp -d)
  2. ### Create a key+certificate for the control plane
  3. cat <<EOF | kubectl create -f -
  4. apiVersion: certificates.k8s.io/v1beta1
  5. kind: CertificateSigningRequest
  6. metadata:
  7. name: kong-control-plane.kong.svc
  8. spec:
  9. request: $(openssl req -new -nodes -batch -keyout privkey.pem -subj /CN=kong-control-plane.kong.svc | base64 | tr -d '\n')
  10. usages:
  11. - digital signature
  12. - key encipherment
  13. - server auth
  14. EOF
  15. kubectl certificate approve kong-control-plane.kong.svc
  16. kubectl -n kong create secret tls kong-control-plane.kong.svc --key=privkey.pem --cert=<(kubectl get csr kong-control-plane.kong.svc -o jsonpath='{.status.certificate}' | base64 --decode)
  17. kubectl delete csr kong-control-plane.kong.svc
  18. rm privkey.pem

或使用以下简便脚本:

  1. curl -fsSL https://raw.githubusercontent.com/Kong/kong-dist-kubernetes/master/setup_certificate.sh | bash

安装步骤

导出一些变量以访问Kong Admin API和Proxy:

  1. $ export HOST=$(kubectl get nodes --namespace default -o jsonpath='{.items[0].status.addresses[0].address}')
  2. $ export ADMIN_PORT=$(kubectl get svc --namespace kong kong-control-plane -o jsonpath='{.spec.ports[0].nodePort}')

通过Kong Admin API启用Sidecar Injector插件:

  1. curl $HOST:$ADMIN_PORT/plugins -d name=kubernetes-sidecar-injector -d config.image=kong

打开Kubernets Sidecar Injection:

  1. cat <<EOF | kubectl create -f -
  2. apiVersion: admissionregistration.k8s.io/v1beta1
  3. kind: MutatingWebhookConfiguration
  4. metadata:
  5. name: kong-sidecar-injector
  6. webhooks:
  7. - name: kong.sidecar.injector
  8. rules:
  9. - apiGroups: [""]
  10. apiVersions: ["v1"]
  11. resources: ["pods"]
  12. operations: [ "CREATE" ]
  13. failurePolicy: Fail
  14. namespaceSelector:
  15. matchExpressions:
  16. - key: kong-sidecar-injection
  17. operator: NotIn
  18. values:
  19. - disabled
  20. clientConfig:
  21. service:
  22. namespace: kong
  23. name: kong-control-plane
  24. path: /kubernetes-sidecar-injector
  25. caBundle: $(kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}')
  26. EOF

或使用以下简便脚本:

  1. curl -fsSL https://raw.githubusercontent.com/Kong/kong-dist-kubernetes/master/setup_sidecar_injector.sh | bash

使用

接下来,任何开始使用Kong Sidecar的pods都会自动注入,而来自该pods容器的所有数据都将通过Kong Sidecar。 例如,如果我们使用Istio中的bookinfo.yaml示例:

  1. kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.1/samples/bookinfo/platform/kube/bookinfo.yaml

我们看到所有pods都收到了Kong Sidecar:

  1. kubectl get all
  2. NAME READY STATUS
  3. pod/details-v1 2/2 Running
  4. pod/productpage 2/2 Running
  5. pod/ratings-v1 2/2 Running
  6. pod/reviews-v1 2/2 Running
  7. pod/reviews-v2 2/2 Running
  8. pod/reviews-v3 2/2 Running

继续配置服务。