在 Ingress 閘道中設定 TLS 終止

總覽

本頁面說明如何在 Cloud Service Mesh 的輸入閘道中設定 TLS 終止,以便管理服務的 HTTPS 外部流量。您將瞭解如何使用 TLS 設定閘道,以便進行安全通訊,並啟用應用程式的加密存取權。這項程序會運用 Cloud Service Mesh 功能,安全地公開服務。

事前準備

如要完成本文件中的步驟,您需要下列資源:

  • 已安裝 Cloud Service Mesh 的 Kubernetes 叢集。

設定環境

請在可存取您要使用的叢集的工作站上執行下列指令。請確認 kubectl 工具已設定為使用叢集專屬的叢集背景資訊。

  1. 設定環境變數。

    export ASM_INGRESSGATEWAY_NAMESPACE=asm-ingressgateway
    export ASM_INGRESSGATEWAY_DEPLOYMENT_NAME=asm-ingressgateway
    export ASM_INGRESSGATEWAY_SERVICE_NAME=asm-ingressgateway
    
  2. 在叢集中部署的 foo 應用程式。安裝方式:

    apiVersion: v1
    kind: Service
    metadata:
      name: foo
      namespace: foo
    spec:
      selector:
        app: test-backend
      ports:
      - port: 8080
        targetPort: 8080
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: foo
      namespace: foo
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: test-backend
      template:
        metadata:
          labels:
            app: test-backend
        spec:
          containers:
          - name: whereami
            image: gcr.io/google-samples/whereami:v1.2.23
            ports:
            - containerPort: 8080
    EOF
    
  3. 產生憑證和金鑰

如要保護入口網關,您需要 TLS 憑證和金鑰。您可以使用任何憑證產生工具,或按照下列步驟使用 openssl 建立必要的憑證。

  • 建立根 CA 憑證和金鑰

    mkdir example_certs
    openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=Example Corp/CN=example.com' \
      -keyout example.com.key -out example.com.crt
    
  • 產生 ingress 的憑證和金鑰

    openssl req -out foo.example.com.csr -newkey rsa:2048 -nodes \
      -keyout foo.example.com.key -subj "/CN=foo.example.com/O=Foo Org"
    
    openssl x509 -req -sha256 -days 365 -CA example.com.crt \
      -CAkey example.com.key -set_serial 0 \
      -in foo.example.com.csr -out foo.example.com.crt
    

設定 TLS 輸入閘道

完成本節的操作說明前,您必須先決定控制平面實作方式。請按照「找出控制層實作項目」一文中的操作說明進行。

  1. 建立命名空間。這個命名空間用於部署入口網關。

    kubectl create namespace ${ASM_INGRESSGATEWAY_NAMESPACE}
    
  2. 將預設的注入標籤套用至命名空間:

    kubectl label namespace ${ASM_INGRESSGATEWAY_NAMESPACE} \
        istio.io/rev- istio-injection=enabled --overwrite
    
  3. 套用Ingress Gateway 資訊清單檔案

    kubectl --namespace ${ASM_INGRESSGATEWAY_NAMESPACE} apply --filename https://raw.githubusercontent.com/GoogleCloudPlatform/anthos-service-mesh-samples/main/docs/ingress-gateway-external-lb/ingress-gateway.yaml
    

    預期輸出內容:

    serviceaccount/asm-ingressgateway created
    role.rbac.authorization.k8s.io/asm-ingressgateway created
    rolebinding.rbac.authorization.k8s.io/asm-ingressgateway created
    deployment.apps/asm-ingressgateway created
    service/asm-ingressgateway created
    poddisruptionbudget.policy/asm-ingressgateway created
    horizontalpodautoscaler.autoscaling/asm-ingressgateway created
    
  4. 將 TLS 憑證儲存在 Kubernetes 密鑰中:

    kubectl create -n ${ASM_INGRESSGATEWAY_NAMESPACE} secret tls foo-credential \
      --key=example_certs/foo.example.com.key \
      --cert=example_certs/foo.example.com.crt
    
  5. 定義輸入閘道:建立閘道資源,以便處理通訊埠 443 上的 HTTPS 流量:

    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: Gateway
    metadata:
      name: secure-gateway
      namespace: ${ASM_INGRESSGATEWAY_NAMESPACE}
    spec:
      selector:
        app: asm-ingressgateway
        istio: ingressgateway
      servers:
      - port:
          number: 443
          name: https
          protocol: HTTPS
        tls:
          mode: SIMPLE
          credentialName: foo-credential
        hosts:
        - "foo.example.com"
    EOF
    
  6. 將流量轉送至 foo 服務:定義 VirtualService,將流量導向至 foo 部署:

    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: VirtualService
    metadata:
      name: foo-routing
      namespace: ${ASM_INGRESSGATEWAY_NAMESPACE}
    spec:
      hosts:
      - "foo.example.com"
      gateways:
      - secure-gateway
      http:
      - match:
        - uri:
            prefix: /status
        - uri:
            prefix: /delay
        route:
        - destination:
            host: foo
            port:
              number: 8080
    EOF
    
  7. 設定外部負載平衡器,以便從叢集連線至入口閘道

  8. 測試安全連線:使用 curl 驗證設定:

    export EXTERNAL_LB_IP_ADDRESS=EXTERNAL_LB_IP_ADDRESS
    curl -v -H "Host: foo.example.com" --resolve "foo.example.com:443:$EXTERNAL_LB_IP_ADDRESS" \
      --cacert example_certs/example.com.crt "https://foo.example.com:443/ping"
    

EXTERNAL_LB_IP_ADDRESS 替換為外部負載平衡器的 IP。

輸出結果會與下列內容相似:

    {
      "cluster_name": "gke-us",
      "host_header": "34.120.175.141",
      "pod_name": "whereami-deployment-954cbf78-mtlpf",
      "pod_name_emoji": "😎",
      "project_id": "my-project",
      "timestamp": "2021-11-29T17:01:59",
      "zone": "us-central1-b"
    }

後續步驟