无 TLS 终止的 Ingress Gateway

    本任务中的 HTTPS 示例服务是一个简单的 NGINX 服务。在接下来的步骤中,您首先在 Kubernetes 集群中创建一个 NGINX 服务。接着,通过网关给这个服务配置一个域名是 的访问入口。

    Istio 打算在将 Kubernetes Gateway API 作为流量管理的默认 API。下面的说明允许您在网格中配置流量管理时选择使用 Gateway API 或 Istio 配置 API。根据您的偏好,按照 Gateway APIIstio classic 标签下的说明进行操作。

    注意,在大多数 Kubernetes 集群上,Kubernetes Gateway API CRD 并不是默认安装的,所以在使用 Gateway API 之前,请确保它们已经安装:

    本文使用 Kubernetes Gateway API 的。请确保在使用 Gateway API 之前安装实验性 CRD。

    1. $ kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd/experimental?ref=v0.5.1" | kubectl apply -f -

    按照部署 Istio。

    对于此任务,您可以使用自己喜欢的工具来生成证书和密钥。以下命令使用 :

      1. $ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout example.com.key -out example.com.crt
    1. nginx.example.com 创建证书和私钥:

      1. $ openssl req -out nginx.example.com.csr -newkey rsa:2048 -nodes -keyout nginx.example.com.key -subj "/CN=nginx.example.com/O=some organization"
      2. $ openssl x509 -req -sha256 -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in nginx.example.com.csr -out nginx.example.com.crt
    1. 创建一个 Kubernetes 的 资源来保存服务的证书:

      1. $ kubectl create secret tls nginx-server-certs --key nginx.example.com.key --cert nginx.example.com.crt
    2. 为 NGINX 服务创建一个配置文件:

    3. 创建一个 Kubernetes 的 ConfigMap 资源来保存 NGINX 服务的配置:

      1. $ kubectl create configmap nginx-configmap --from-file=nginx.conf=./nginx.conf
    4. 部署 NGINX 服务

      1. $ cat <<EOF | istioctl kube-inject -f - | kubectl apply -f -
      2. apiVersion: v1
      3. kind: Service
      4. metadata:
      5. name: my-nginx
      6. labels:
      7. run: my-nginx
      8. spec:
      9. ports:
      10. - port: 443
      11. protocol: TCP
      12. selector:
      13. run: my-nginx
      14. ---
      15. apiVersion: apps/v1
      16. kind: Deployment
      17. metadata:
      18. name: my-nginx
      19. spec:
      20. selector:
      21. matchLabels:
      22. run: my-nginx
      23. replicas: 1
      24. template:
      25. metadata:
      26. labels:
      27. run: my-nginx
      28. spec:
      29. - name: my-nginx
      30. image: nginx
      31. ports:
      32. - containerPort: 443
      33. volumeMounts:
      34. mountPath: /etc/nginx
      35. readOnly: true
      36. - name: nginx-server-certs
      37. mountPath: /etc/nginx-server-certs
      38. readOnly: true
      39. volumes:
      40. - name: nginx-config
      41. configMap:
      42. name: nginx-configmap
      43. - name: nginx-server-certs
      44. secret:
      45. secretName: nginx-server-certs
      46. EOF
    5. 要测试 NGINX 服务是否已成功部署,需要从其 Sidecar 代理发送请求,并忽略检查服务端的证书(使用 curl-k 选项)。确保正确打印服务端的证书,即 common name (CN) 等于 nginx.example.com

      1. $ kubectl exec "$(kubectl get pod -l run=my-nginx -o jsonpath={.items..metadata.name})" -c istio-proxy -- curl -sS -v -k --resolve nginx.example.com:443:127.0.0.1 https://nginx.example.com
      2. ...
      3. SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
      4. ALPN, server accepted to use http/1.1
      5. Server certificate:
      6. subject: CN=nginx.example.com; O=some organization
      7. start date: May 27 14:18:47 2020 GMT
      8. expire date: May 27 14:18:47 2021 GMT
      9. issuer: O=example Inc.; CN=example.com
      10. SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
      11. > GET / HTTP/1.1
      12. > User-Agent: curl/7.58.0
      13. > Host: nginx.example.com
      14. ...
      15. < HTTP/1.1 200 OK
      16. < Server: nginx/1.17.10
      17. ...
      18. <!DOCTYPE html>
      19. <html>
      20. <head>
      21. <title>Welcome to nginx!</title>
      22. ...
      1. apiVersion: networking.istio.io/v1alpha3
      2. kind: Gateway
      3. metadata:
      4. name: mygateway
      5. spec:
      6. selector:
      7. istio: ingressgateway # use istio default ingress gateway
      8. servers:
      9. - port:
      10. number: 443
      11. name: https
      12. protocol: HTTPS
      13. tls:
      14. mode: PASSTHROUGH
      15. hosts:
      16. - nginx.example.com
      17. EOF
    1. 为通过 Gateway 进入的流量配置路由:

    2. 根据确定 Ingress IP 和端口中的指令来定义环境变量 SECURE_INGRESS_PORTINGRESS_HOST

    3. 从集群外访问 NGINX 服务。注意,服务端返回了正确的证书,并且该证书已成功验证(输出了 SSL certificate verify ok)。

      1. $ curl -v --resolve "nginx.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" --cacert example.com.crt "https://nginx.example.com:$SECURE_INGRESS_PORT"
      2. Server certificate:
      3. subject: CN=nginx.example.com; O=some organization
      4. start date: Wed, 15 Aug 2018 07:29:07 GMT
      5. expire date: Sun, 25 Aug 2019 07:29:07 GMT
      6. issuer: O=example Inc.; CN=example.com
      7. SSL certificate verify ok.
      8. < HTTP/1.1 200 OK
      9. < Server: nginx/1.15.2
      10. ...
      11. <html>
      12. <head>
      13. <title>Welcome to nginx!</title>
    1. 删除已创建的 Kubernetes 资源:

      1. $ kubectl delete secret nginx-server-certs
      2. $ kubectl delete configmap nginx-configmap
      3. $ kubectl delete service my-nginx
      4. $ kubectl delete deployment my-nginx
      5. $ kubectl delete gateway mygateway
      6. $ kubectl delete virtualservice nginx
    2. 删除证书和密钥:

      1. $ rm example.com.crt example.com.key nginx.example.com.crt nginx.example.com.key nginx.example.com.csr
    3. 删除本示例中生成的配置文件:

      1. $ rm ./nginx.conf