在入站流量网关中设置 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
    
  • 为入站流量生成证书和密钥

    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. 应用入站流量网关清单文件

    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 Secret 中:

    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. 定义入站流量网关:创建 Gateway 资源以处理端口 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"
    }

后续步骤