具有 TLS 發起的出口閘道

出口流量的 TLS 來源 範例展示了如何設定 Istio 以針對外部服務的流量執行 TLS 來源設定出口閘道 範例展示了如何設定 Istio 以將出口流量導向專用的出口閘道服務。此範例結合了前兩個範例,描述如何設定出口閘道,以針對外部服務的流量執行 TLS 來源。

開始之前

  • 按照 安裝指南 中的說明設定 Istio。

  • 啟動 curl 範例,該範例將用作外部呼叫的測試來源。

    如果您已啟用 自動 Sidecar 注入,請執行

    Zip
    $ kubectl apply -f @samples/curl/curl.yaml@
    

    否則,您必須在部署 curl 應用程式之前手動注入 Sidecar

    Zip
    $ kubectl apply -f <(istioctl kube-inject -f @samples/curl/curl.yaml@)
    

    請注意,任何您可以 execcurl 的 Pod 都可以。

  • 建立一個 Shell 變數,以保存將請求傳送到外部服務的來源 Pod 的名稱。如果您使用 curl 範例,請執行

    $ export SOURCE_POD=$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})
    
  • 對於 macOS 使用者,請驗證您使用的是 openssl 1.1 或更高版本

    $ openssl version -a | grep OpenSSL
    OpenSSL 1.1.1g  21 Apr 2020
    

    如果先前的命令輸出 1.1 或更高版本,如所示,您的 openssl 命令應該可以正確執行此任務中的說明。否則,請升級您的 openssl 或嘗試不同的 openssl 實作,例如在 Linux 機器上。

  • 如果尚未啟用,請 啟用 Envoy 的存取記錄。例如,使用 istioctl

    $ istioctl install <flags-you-used-to-install-Istio> --set meshConfig.accessLogFile=/dev/stdout
    
  • 如果您未使用 Gateway API 說明,請務必 部署 Istio 出口閘道

使用出口閘道執行 TLS 來源

本節說明如何執行與 出口流量的 TLS 來源 範例中相同的 TLS 來源,只不過這次使用的是出口閘道。請注意,在這種情況下,TLS 來源將由出口閘道完成,而不是由上一個範例中的 Sidecar 完成。

  1. edition.cnn.com 定義一個 ServiceEntry

    $ kubectl apply -f - <<EOF
    apiVersion: networking.istio.io/v1
    kind: ServiceEntry
    metadata:
      name: cnn
    spec:
      hosts:
      - edition.cnn.com
      ports:
      - number: 80
        name: http
        protocol: HTTP
      - number: 443
        name: https
        protocol: HTTPS
      resolution: DNS
    EOF
    
  2. 透過向 http://edition.cnn.com/politics 發送請求來驗證您的 ServiceEntry 是否已正確應用。

    $ kubectl exec "${SOURCE_POD}" -c curl -- curl -sSL -o /dev/null -D - http://edition.cnn.com/politics
    HTTP/1.1 301 Moved Permanently
    ...
    location: https://edition.cnn.com/politics
    ...
    

    如果您的輸出中看到 301 Moved Permanently,則表示您的 ServiceEntry 已正確設定。

  3. edition.cnn.com、連接埠 80 建立一個出口 Gateway,並為將定向到出口閘道的 Sidecar 請求建立一個目標規則。

$ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
  name: istio-egressgateway
spec:
  selector:
    istio: egressgateway
  servers:
  - port:
      number: 80
      name: https-port-for-tls-origination
      protocol: HTTPS
    hosts:
    - edition.cnn.com
    tls:
      mode: ISTIO_MUTUAL
---
apiVersion: networking.istio.io/v1
kind: DestinationRule
metadata:
  name: egressgateway-for-cnn
spec:
  host: istio-egressgateway.istio-system.svc.cluster.local
  subsets:
  - name: cnn
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN
      portLevelSettings:
      - port:
          number: 80
        tls:
          mode: ISTIO_MUTUAL
          sni: edition.cnn.com
EOF
  1. 設定路由規則,以將流量導向出口閘道
$ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: direct-cnn-through-egress-gateway
spec:
  hosts:
  - edition.cnn.com
  gateways:
  - istio-egressgateway
  - mesh
  http:
  - match:
    - gateways:
      - mesh
      port: 80
    route:
    - destination:
        host: istio-egressgateway.istio-system.svc.cluster.local
        subset: cnn
        port:
          number: 80
      weight: 100
  - match:
    - gateways:
      - istio-egressgateway
      port: 80
    route:
    - destination:
        host: edition.cnn.com
        port:
          number: 443
      weight: 100
EOF
  1. 定義一個 DestinationRule,以針對對 edition.cnn.com 的請求執行 TLS 來源。

    $ kubectl apply -f - <<EOF
    apiVersion: networking.istio.io/v1
    kind: DestinationRule
    metadata:
      name: originate-tls-for-edition-cnn-com
    spec:
      host: edition.cnn.com
      trafficPolicy:
        loadBalancer:
          simple: ROUND_ROBIN
        portLevelSettings:
        - port:
            number: 443
          tls:
            mode: SIMPLE # initiates HTTPS for connections to edition.cnn.com
    EOF
    
  2. http://edition.cnn.com/politics 發送 HTTP 請求。

    $ kubectl exec "${SOURCE_POD}" -c curl -- curl -sSL -o /dev/null -D - http://edition.cnn.com/politics
    HTTP/1.1 200 OK
    ...
    

    輸出應該與 出口流量的 TLS 來源 範例中的相同,使用 TLS 來源:沒有 301 Moved Permanently 訊息。

  3. 檢查出口閘道 Proxy 的記錄。

如果 Istio 部署在 istio-system 命名空間中,則用於列印記錄的命令為

$ kubectl logs -l istio=egressgateway -c istio-proxy -n istio-system | tail

您應該會看到類似下列的行

[2020-06-30T16:17:56.763Z] "GET /politics HTTP/2" 200 - "-" "-" 0 1295938 529 89 "10.244.0.171" "curl/7.64.0" "cf76518d-3209-9ab7-a1d0-e6002728ef5b" "edition.cnn.com" "151.101.129.67:443" outbound|443||edition.cnn.com 10.244.0.170:54280 10.244.0.170:8080 10.244.0.171:35628 - -

清除 TLS 來源範例

移除您建立的 Istio 設定項目

$ kubectl delete gw istio-egressgateway
$ kubectl delete serviceentry cnn
$ kubectl delete virtualservice direct-cnn-through-egress-gateway
$ kubectl delete destinationrule originate-tls-for-edition-cnn-com
$ kubectl delete destinationrule egressgateway-for-cnn

使用出口閘道執行相互 TLS 來源

與上一節類似,本節說明如何設定出口閘道以針對外部服務執行 TLS 來源,只不過這次使用需要相互 TLS 的服務。

此範例相當複雜,因為您需要先

  1. 產生用戶端和伺服器憑證
  2. 部署支援相互 TLS 協定的外部服務
  3. 使用所需的相互 TLS 憑證重新部署出口閘道

然後,您才能設定外部流量通過將執行 TLS 來源的出口閘道。

產生用戶端和伺服器憑證及金鑰

對於此任務,您可以使用您最喜歡的工具來產生憑證和金鑰。以下命令使用 openssl

  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
    
  2. my-nginx.mesh-external.svc.cluster.local 建立憑證和私密金鑰

    $ openssl req -out my-nginx.mesh-external.svc.cluster.local.csr -newkey rsa:2048 -nodes -keyout my-nginx.mesh-external.svc.cluster.local.key -subj "/CN=my-nginx.mesh-external.svc.cluster.local/O=some organization"
    $ openssl x509 -req -sha256 -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in my-nginx.mesh-external.svc.cluster.local.csr -out my-nginx.mesh-external.svc.cluster.local.crt
    

    或者,如果您想為目的地啟用 SAN 驗證,您可以將 SubjectAltNames 新增至憑證。例如

    $ cat > san.conf <<EOF
    [req]
    distinguished_name = req_distinguished_name
    req_extensions = v3_req
    x509_extensions = v3_req
    prompt = no
    [req_distinguished_name]
    countryName = US
    [v3_req]
    keyUsage = critical, digitalSignature, keyEncipherment
    extendedKeyUsage = serverAuth, clientAuth
    basicConstraints = critical, CA:FALSE
    subjectAltName = critical, @alt_names
    [alt_names]
    DNS = my-nginx.mesh-external.svc.cluster.local
    EOF
    $
    $ openssl req -out my-nginx.mesh-external.svc.cluster.local.csr -newkey rsa:4096 -nodes -keyout my-nginx.mesh-external.svc.cluster.local.key -subj "/CN=my-nginx.mesh-external.svc.cluster.local/O=some organization" -config san.conf
    $ openssl x509 -req -sha256 -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in my-nginx.mesh-external.svc.cluster.local.csr -out my-nginx.mesh-external.svc.cluster.local.crt -extfile san.conf -extensions v3_req
    
  3. 產生用戶端憑證和私密金鑰

    $ openssl req -out client.example.com.csr -newkey rsa:2048 -nodes -keyout client.example.com.key -subj "/CN=client.example.com/O=client organization"
    $ openssl x509 -req -sha256 -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 1 -in client.example.com.csr -out client.example.com.crt
    

部署相互 TLS 伺服器

為了模擬支援相互 TLS 協定的實際外部服務,請在您的 Kubernetes 叢集中部署一個 NGINX 伺服器,但請在 Istio 服務網格之外執行,也就是在未啟用 Istio Sidecar Proxy 注入的命名空間中執行。

  1. 建立一個命名空間來表示 Istio 網格之外的服務,也就是 mesh-external。請注意,由於自動 Sidecar 注入未在其上 啟用,因此 Sidecar Proxy 不會自動注入到此命名空間中的 Pod 中。

    $ kubectl create namespace mesh-external
    
  2. 建立 Kubernetes Secrets 以保存伺服器和 CA 憑證。

    $ kubectl create -n mesh-external secret tls nginx-server-certs --key my-nginx.mesh-external.svc.cluster.local.key --cert my-nginx.mesh-external.svc.cluster.local.crt
    $ kubectl create -n mesh-external secret generic nginx-ca-certs --from-file=example.com.crt
    
  3. 為 NGINX 伺服器建立一個組態檔

    $ cat <<\EOF > ./nginx.conf
    events {
    }
    
    http {
      log_format main '$remote_addr - $remote_user [$time_local]  $status '
      '"$request" $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';
      access_log /var/log/nginx/access.log main;
      error_log  /var/log/nginx/error.log;
    
      server {
        listen 443 ssl;
    
        root /usr/share/nginx/html;
        index index.html;
    
        server_name my-nginx.mesh-external.svc.cluster.local;
        ssl_certificate /etc/nginx-server-certs/tls.crt;
        ssl_certificate_key /etc/nginx-server-certs/tls.key;
        ssl_client_certificate /etc/nginx-ca-certs/example.com.crt;
        ssl_verify_client on;
      }
    }
    EOF
    
  4. 建立一個 Kubernetes ConfigMap 以保存 NGINX 伺服器的組態

    $ kubectl create configmap nginx-configmap -n mesh-external --from-file=nginx.conf=./nginx.conf
    
  5. 部署 NGINX 伺服器

    $ kubectl apply -f - <<EOF
    apiVersion: v1
    kind: Service
    metadata:
      name: my-nginx
      namespace: mesh-external
      labels:
        run: my-nginx
    spec:
      ports:
      - port: 443
        protocol: TCP
      selector:
        run: my-nginx
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx
      namespace: mesh-external
    spec:
      selector:
        matchLabels:
          run: my-nginx
      replicas: 1
      template:
        metadata:
          labels:
            run: my-nginx
        spec:
          containers:
          - name: my-nginx
            image: nginx
            ports:
            - containerPort: 443
            volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx
              readOnly: true
            - name: nginx-server-certs
              mountPath: /etc/nginx-server-certs
              readOnly: true
            - name: nginx-ca-certs
              mountPath: /etc/nginx-ca-certs
              readOnly: true
          volumes:
          - name: nginx-config
            configMap:
              name: nginx-configmap
          - name: nginx-server-certs
            secret:
              secretName: nginx-server-certs
          - name: nginx-ca-certs
            secret:
              secretName: nginx-ca-certs
    EOF
    

設定出口流量的相互 TLS 來源

  1. 在與出口閘道部署在相同命名空間中,建立一個 Kubernetes Secret 以保存用戶端的憑證
$ kubectl create secret -n istio-system generic client-credential --from-file=tls.key=client.example.com.key \
  --from-file=tls.crt=client.example.com.crt --from-file=ca.crt=example.com.crt

為了支援與各種工具整合,Istio 支援幾種不同的 Secret 格式。在此範例中,使用具有金鑰 tls.keytls.crtca.crt 的單個通用 Secret。

  1. my-nginx.mesh-external.svc.cluster.local、連接埠 443 建立一個出口 Gateway,並為將定向到出口閘道的 Sidecar 請求建立一個目標規則。
$ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
  name: istio-egressgateway
spec:
  selector:
    istio: egressgateway
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - my-nginx.mesh-external.svc.cluster.local
    tls:
      mode: ISTIO_MUTUAL
---
apiVersion: networking.istio.io/v1
kind: DestinationRule
metadata:
  name: egressgateway-for-nginx
spec:
  host: istio-egressgateway.istio-system.svc.cluster.local
  subsets:
  - name: nginx
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN
      portLevelSettings:
      - port:
          number: 443
        tls:
          mode: ISTIO_MUTUAL
          sni: my-nginx.mesh-external.svc.cluster.local
EOF
  1. 設定路由規則,以將流量導向出口閘道
$ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: direct-nginx-through-egress-gateway
spec:
  hosts:
  - my-nginx.mesh-external.svc.cluster.local
  gateways:
  - istio-egressgateway
  - mesh
  http:
  - match:
    - gateways:
      - mesh
      port: 80
    route:
    - destination:
        host: istio-egressgateway.istio-system.svc.cluster.local
        subset: nginx
        port:
          number: 443
      weight: 100
  - match:
    - gateways:
      - istio-egressgateway
      port: 443
    route:
    - destination:
        host: my-nginx.mesh-external.svc.cluster.local
        port:
          number: 443
      weight: 100
EOF
  1. 新增一個 DestinationRule 以執行相互 TLS 來源
$ kubectl apply -n istio-system -f - <<EOF
apiVersion: networking.istio.io/v1
kind: DestinationRule
metadata:
  name: originate-mtls-for-nginx
spec:
  host: my-nginx.mesh-external.svc.cluster.local
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN
    portLevelSettings:
    - port:
        number: 443
      tls:
        mode: MUTUAL
        credentialName: client-credential # this must match the secret created earlier to hold client certs
        sni: my-nginx.mesh-external.svc.cluster.local
        # subjectAltNames: # can be enabled if the certificate was generated with SAN as specified in previous section
        # - my-nginx.mesh-external.svc.cluster.local
EOF
  1. 驗證憑證是否已提供給出口閘道且處於作用中狀態
$ istioctl -n istio-system proxy-config secret deploy/istio-egressgateway | grep client-credential
kubernetes://client-credential            Cert Chain     ACTIVE     true           1                                          2024-06-04T12:46:28Z     2023-06-05T12:46:28Z
kubernetes://client-credential-cacert     Cert Chain     ACTIVE     true           16491643791048004260                       2024-06-04T12:46:28Z     2023-06-05T12:46:28Z
  1. http://my-nginx.mesh-external.svc.cluster.local 發送 HTTP 請求

    $ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl -sS http://my-nginx.mesh-external.svc.cluster.local
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    ...
    
  2. 檢查出口閘道 Proxy 的記錄

如果 Istio 部署在 istio-system 命名空間中,則用於列印記錄的命令為

$ kubectl logs -l istio=egressgateway -n istio-system | grep 'my-nginx.mesh-external.svc.cluster.local' | grep HTTP

您應該會看到類似下列的行

[2018-08-19T18:20:40.096Z] "GET / HTTP/1.1" 200 - 0 612 7 5 "172.30.146.114" "curl/7.35.0" "b942b587-fac2-9756-8ec6-303561356204" "my-nginx.mesh-external.svc.cluster.local" "172.21.72.197:443"

清除相互 TLS 來源範例

  1. 移除 NGINX 相互 TLS 伺服器資源

    $ kubectl delete secret nginx-server-certs nginx-ca-certs -n mesh-external
    $ kubectl delete configmap nginx-configmap -n mesh-external
    $ kubectl delete service my-nginx -n mesh-external
    $ kubectl delete deployment my-nginx -n mesh-external
    $ kubectl delete namespace mesh-external
    
  2. 移除閘道組態資源

$ kubectl delete secret client-credential -n istio-system
$ kubectl delete gw istio-egressgateway
$ kubectl delete virtualservice direct-nginx-through-egress-gateway
$ kubectl delete destinationrule -n istio-system originate-mtls-for-nginx
$ kubectl delete destinationrule egressgateway-for-nginx
  1. 刪除憑證和私密金鑰

    $ rm example.com.crt example.com.key my-nginx.mesh-external.svc.cluster.local.crt my-nginx.mesh-external.svc.cluster.local.key my-nginx.mesh-external.svc.cluster.local.csr client.example.com.crt client.example.com.csr client.example.com.key
    
  2. 刪除此範例中使用的產生的組態檔

    $ rm ./nginx.conf
    

清理

刪除 curl 服務和部署

Zip
$ kubectl delete -f @samples/curl/curl.yaml@
此資訊對您有幫助嗎?
您有任何改進建議嗎?

感謝您的回饋!