在托管式 Kubernetes 上部署 Windows 应用

Last reviewed 2024-08-14 UTC

本文档介绍了如何在管理和扩缩在托管式 Kubernetes 上运行的 Windows 应用的网络中部署参考架构。

这些说明适用于负责设计和管理在 Google Kubernetes Engine (GKE) 集群上运行的 Windows 应用的云架构师、网络管理员和 IT 专业人员。

架构

下图展示了部署在托管式 GKE 集群上运行的 Windows 应用时使用的参考架构。

数据流经内部应用负载均衡器和 Envoy 网关。

如上图所示,箭头表示使用 Cloud Service Mesh 和 Envoy 网关为在 GKE 上运行的 Windows 应用管理网络的工作流程。区域级 GKE 集群包含 Windows 和 Linux 节点池。Cloud Service Mesh 会创建和管理指向 Windows Pod 的流量路线。

目标

  • 创建并设置 GKE 集群,以运行 Windows 应用和 Envoy 代理。
  • 部署和验证 Windows 应用。
  • 将 Cloud Service Mesh 配置为 Envoy 网关的控制平面。
  • 使用 Kubernetes Gateway API 预配内部应用负载均衡器并公开 Envoy 网关。
  • 了解您创建的持续部署操作。

费用

此架构的部署使用Google Cloud的以下收费组件:

完成本部署后,您可以通过删除您创建的资源来避免继续计费。如需了解详情,请参阅清理

准备工作

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the Cloud Shell, and Cloud Service Mesh APIs.

    Enable the APIs

  4. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    如果在共享虚拟私有云 (VPC) 环境中运行,您还需要按照手动创建代理专用子网和防火墙规则的说明,以进行 Cloud Load Balancing 响应能力检查。

    创建 GKE 集群

    按照以下步骤创建 GKE 集群。您将使用 GKE 集群来容纳和运行此部署中的 Windows 应用和 Envoy 代理。

    1. 在 Cloud Shell 中,运行以下 Google Cloud CLI 命令以创建一个区域级 GKE 集群,其中三个区域各有一个节点:

      gcloud container clusters create my-cluster
          --enable-ip-alias \
          --num-nodes=1 \
          --release-channel stable \
          --enable-dataplane-v2 \
          --region us-central1 \
          --scopes=cloud-platform \
          --gateway-api=standard
      
    2. 将 Windows 节点池添加到 GKE 集群:

      gcloud container node-pools create win-pool \
          --cluster=my-cluster \
          --image-type=windows_ltsc_containerd \
          --no-enable-autoupgrade \
          --region=us-central1 \
          --num-nodes=1 \
          --machine-type=n1-standard-2 \
          --windows-os-version=ltsc2019
      

      此操作可能需要大约 20 分钟才能完成。

    3. 将您的 Google Cloud 项目 ID 存储在环境变量中:

      export PROJECT_ID=$(gcloud config get project)
      
    4. 连接到 GKE 集群:

      gcloud container clusters get-credentials my-cluster --region us-central1
      
    5. 列出 GKE 集群中的所有节点:

      kubectl get nodes
      

      输出应显示三个 Linux 节点和三个 Windows 节点。

      GKE 集群准备就绪后,您可以部署两个基于 Windows 的测试应用。

    部署两个测试应用

    在本部分中,您将部署两个基于 Windows 的测试应用。这两个测试应用都会输出应用运行的主机名。您还需要创建一个 Kubernetes Service,以便通过独立的网络端点组 (NEG) 公开应用。

    当您在区域性集群上部署基于 Windows 的应用和 Kubernetes Service 时,系统会为应用运行的每个可用区创建一个 NEG。稍后,本部署指南介绍如何将这些 NEG 配置为 Cloud Service Mesh 服务的后端。

    1. 在 Cloud Shell 中,使用 kubectl 应用以下 YAML 文件,以部署第一个测试应用。此命令会部署测试应用的三个实例,每个区域可用区一个。

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        labels:
          app: win-webserver-1
        name: win-webserver-1
      spec:
        replicas: 3
        selector:
          matchLabels:
            app: win-webserver-1
        template:
          metadata:
            labels:
              app: win-webserver-1
            name: win-webserver-1
          spec:
           containers:
            - name: windowswebserver
              image: k8s.gcr.io/e2e-test-images/agnhost:2.36
              command: ["/agnhost"]
              args: ["netexec", "--http-port", "80"]
           topologySpreadConstraints:
            - maxSkew: 1
              topologyKey: kubernetes.io/hostname
              whenUnsatisfiable: DoNotSchedule
              labelSelector:
                matchLabels:
                  app: win-webserver-1
           nodeSelector:
            kubernetes.io/os: windows
      
    2. 应用匹配的 Kubernetes Service,并使用 NEG 公开该 Service:

      apiVersion: v1
      kind: Service
      metadata:
        name: win-webserver-1
        annotations:
          cloud.google.com/neg: '{"exposed_ports": {"80":{"name": "win-webserver-1"}}}'
      spec:
        type: ClusterIP
        selector:
          app: win-webserver-1
        ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 80
      
    3. 验证部署:

      kubectl get pods
      

      输出显示该应用有三个正在运行的 Windows Pod。

      NAME                               READY   STATUS    RESTARTS   AGE
      win-webserver-1-7bb4c57f6d-hnpgd   1/1     Running   0          5m58s
      win-webserver-1-7bb4c57f6d-rgqsb   1/1     Running   0          5m58s
      win-webserver-1-7bb4c57f6d-xp7ww   1/1     Running   0          5m58s
      
    4. 验证 Kubernetes Service 是否已创建:

      $ kubectl get svc
      

      输出类似以下内容:

      NAME              TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
      kubernetes        ClusterIP   10.64.0.1            443/TCP   58m
      win-webserver-1   ClusterIP   10.64.6.20           80/TCP    3m35s
      
    5. 针对 kubectl 运行 describe 命令,验证是否已在应用运行的每个可用区中为 Kubernetes Service 创建了相应的 NEG:

      $ kubectl describe service win-webserver-1
      

      输出类似以下内容:

      Name:              win-webserver-1
      Namespace:         default
      Labels:            
      Annotations:       cloud.google.com/neg: {"exposed_ports": {"80":{"name": "win-webserver-1"}}}
                         cloud.google.com/neg-status: {"network_endpoint_groups":{"80":"win-webserver-1"},"zones":["us-central1-a","us-central1-b","us-central1-c"]}
      Selector:          app=win-webserver-1
      Type:              ClusterIP
      IP Family Policy:  SingleStack
      IP Families:       IPv4
      IP:                10.64.6.20
      IPs:               10.64.6.20
      Port:              http  80/TCP
      TargetPort:        80/TCP
      Endpoints:         10.60.3.5:80,10.60.4.5:80,10.60.5.5:80
      Session Affinity:  None
      Events:
        Type    Reason  Age    From            Message
        ----    ------  ----   ----            -------
        Normal  Create  4m25s  neg-controller  Created NEG "win-webserver-1" for default/win-webserver-1-win-webserver-1-http/80-80-GCE_VM_IP_PORT-L7 in "us-central1-a".
        Normal  Create  4m18s  neg-controller  Created NEG "win-webserver-1" for default/win-webserver-1-win-webserver-1-http/80-80-GCE_VM_IP_PORT-L7 in "us-central1-b".
        Normal  Create  4m11s  neg-controller  Created NEG "win-webserver-1" for default/win-webserver-1-win-webserver-1-http/80-80-GCE_VM_IP_PORT-L7 in "us-central1-c".
        Normal  Attach  4m9s   neg-controller  Attach 1 network endpoint(s) (NEG "win-webserver-1" in zone "us-central1-a")
        Normal  Attach  4m8s   neg-controller  Attach 1 network endpoint(s) (NEG "win-webserver-1" in zone "us-central1-c")
        Normal  Attach  4m8s   neg-controller  Attach 1 network endpoint(s) (NEG "win-webserver-1" in zone "us-central1-b")
      

      上述命令的输出显示,系统已为每个可用区创建了 NEG。

    6. 可选:使用 gcloud CLI 验证 NEG 是否已创建:

      gcloud compute network-endpoint-groups list
      

      输出如下所示:

      NAME                                                        LOCATION            ENDPOINT_TYPE     SIZE
      win-webserver-1                                us-central1-a  GCE_VM_IP_PORT  1
      win-webserver-1                                us-central1-b  GCE_VM_IP_PORT  1
      win-webserver-1                                us-central1-c  GCE_VM_IP_PORT  1
      
    7. 如需部署第二个测试应用,请应用以下 YAML 文件:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        labels:
          app: win-webserver-2
        name: win-webserver-2
      spec:
        replicas: 3
        selector:
          matchLabels:
            app: win-webserver-2
        template:
          metadata:
            labels:
              app: win-webserver-2
            name: win-webserver-2
          spec:
           containers:
            - name: windowswebserver
              image: k8s.gcr.io/e2e-test-images/agnhost:2.36
              command: ["/agnhost"]
              args: ["netexec", "--http-port", "80"]
           topologySpreadConstraints:
            - maxSkew: 1
              topologyKey: kubernetes.io/hostname
              whenUnsatisfiable: DoNotSchedule
              labelSelector:
                matchLabels:
                  app: win-webserver-2
           nodeSelector:
            kubernetes.io/os: windows
      
    8. 创建相应的 Kubernetes 服务:

      apiVersion: v1
      kind: Service
      metadata:
        name: win-webserver-2
        annotations:
          cloud.google.com/neg: '{"exposed_ports": {"80":{"name": "win-webserver-2"}}}'
      spec:
        type: ClusterIP
        selector:
          app: win-webserver-2
        ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 80
      
    9. 验证应用部署:

      kubectl get pods
      

      检查输出,并验证是否有三个正在运行的 Pod。

    10. 验证是否已创建 Kubernetes Service 和三个 NEG:

      kubectl describe service win-webserver-2
      

    配置 Cloud Service Mesh

    在本部分中,Cloud Service Mesh 会配置为 Envoy 网关的控制平面。

    可通过指定 scope_name 参数将 Envoy 网关映射到相关的 Cloud Service Mesh 路由配置。使用 scope_name 参数,您可以为不同的 Envoy 网关配置不同的路由规则。

    1. 在 Cloud Shell 中,创建一条防火墙规则,允许从检查应用响应能力的 Google 服务传入流量:

      gcloud compute firewall-rules create allow-health-checks \
        --network=default \
        --direction=INGRESS \
        --action=ALLOW \
        --rules=tcp \
        --source-ranges="35.191.0.0/16,130.211.0.0/22,209.85.152.0/22,209.85.204.0/22"
      
    2. 检查第一个应用的响应能力:

      gcloud compute health-checks create http win-app-1-health-check \
        --enable-logging \
        --request-path="/healthz" \
        --use-serving-port
      
    3. 检查第二个应用的响应能力:

      gcloud compute health-checks create http win-app-2-health-check \
        --enable-logging \
        --request-path="/healthz" \
        --use-serving-port
      
    4. 为第一个应用创建 Cloud Service Mesh 后端服务:

      gcloud compute backend-services create win-app-1-service \
       --global \
       --load-balancing-scheme=INTERNAL_SELF_MANAGED \
       --port-name=http \
       --health-checks win-app-1-health-check
      
    5. 为第二个应用创建 Cloud Service Mesh 后端服务:

      gcloud compute backend-services create win-app-2-service \
       --global \
       --load-balancing-scheme=INTERNAL_SELF_MANAGED \
       --port-name=http \
       --health-checks win-app-2-health-check
      
    6. 添加您之前创建的 NEG。这些 NEG 与您创建的第一个应用相关联,该应用是 Cloud Service Mesh 后端服务的后端。此代码示例为您创建的区域级集群中的每个可用区添加一个 NEG。

      BACKEND_SERVICE=win-app-1-service
      APP1_NEG_NAME=win-webserver-1
      MAX_RATE_PER_ENDPOINT=10
      
      gcloud compute backend-services add-backend $BACKEND_SERVICE \
        --global \
        --network-endpoint-group $APP1_NEG_NAME \
        --network-endpoint-group-zone us-central1-b \
        --balancing-mode RATE \
        --max-rate-per-endpoint $MAX_RATE_PER_ENDPOINT
      
      gcloud compute backend-services add-backend $BACKEND_SERVICE \
        --global \
        --network-endpoint-group $APP1_NEG_NAME \
        --network-endpoint-group-zone us-central1-a \
        --balancing-mode RATE \
        --max-rate-per-endpoint $MAX_RATE_PER_ENDPOINT
      
      gcloud compute backend-services add-backend $BACKEND_SERVICE \
        --global \
        --network-endpoint-group $APP1_NEG_NAME \
        --network-endpoint-group-zone us-central1-c \
        --balancing-mode RATE \
        --max-rate-per-endpoint $MAX_RATE_PER_ENDPOINT
      
    7. 添加其他 NEG。这些 NEG 与您创建的第二个应用(作为 Cloud Service Mesh 后端服务的后端)相关联。此代码示例为您创建的区域级集群中的每个可用区添加一个 NEG。

      BACKEND_SERVICE=win-app-2-service
      APP2_NEG_NAME=win-webserver-2
      
      gcloud compute backend-services add-backend $BACKEND_SERVICE \
        --global \
        --network-endpoint-group $APP2_NEG_NAME \
        --network-endpoint-group-zone us-central1-b \
        --balancing-mode RATE \
        --max-rate-per-endpoint $MAX_RATE_PER_ENDPOINT
      
      gcloud compute backend-services add-backend $BACKEND_SERVICE \
        --global \
        --network-endpoint-group $APP2_NEG_NAME \
        --network-endpoint-group-zone us-central1-a \
        --balancing-mode RATE \
        --max-rate-per-endpoint $MAX_RATE_PER_ENDPOINT
      
      gcloud compute backend-services add-backend $BACKEND_SERVICE \
        --global \
        --network-endpoint-group $APP2_NEG_NAME \
        --network-endpoint-group-zone us-central1-c \
        --balancing-mode RATE \
        --max-rate-per-endpoint $MAX_RATE_PER_ENDPOINT
      

    配置其他 Cloud Service Mesh 资源

    现在,您已配置 Cloud Service Mesh 服务,接下来需要配置另外两个资源,以完成 Cloud Service Mesh 设置。

    首先,以下步骤展示了如何配置 Gateway 资源。Gateway 资源是一种虚拟资源,用于生成 Cloud Service Mesh 路由规则。Cloud Service Mesh 路由规则用于将 Envoy 代理配置为网关。

    接下来的步骤展示了如何为每个后端服务配置 HTTPRoute 资源。HTTPRoute 资源会将 HTTP 请求映射到相关后端服务。

    1. 在 Cloud Shell 中,创建一个名为 gateway.yaml 的 YAML 文件,用于定义 Gateway 资源:

      cat <<EOF> gateway.yaml
      name: gateway80
      scope: gateway-proxy
      ports:
      - 8080
      type: OPEN_MESH
      EOF
      
    2. 调用 gateway.yaml 文件以创建 Gateway 资源:

      gcloud network-services gateways import gateway80 \
        --source=gateway.yaml \
        --location=global
      

      Gateway 名称将为 projects/$PROJECT_ID/locations/global/gateways/gateway80

      您在为每个后端服务创建 HTTPRoutes 时需使用此 Gateway 名称。

    为每个后端服务创建 HTTPRoutes

    1. 在 Cloud Shell 中,将您的 Google Cloud 项目 ID 存储在环境变量中:

      export PROJECT_ID=$(gcloud config get project)
      
    2. 为第一个应用创建 HTTPRoute YAML 文件:

      cat <<EOF> win-app-1-route.yaml
      name: win-app-1-http-route
      hostnames:
      - win-app-1
      gateways:
      - projects/$PROJECT_ID/locations/global/gateways/gateway80
      rules:
      - action:
         destinations:
         - serviceName: "projects/$PROJECT_ID/locations/global/backendServices/win-app-1-service"
      EOF
      
    3. 为第一个应用创建 HTTPRoute 资源:

      gcloud network-services http-routes import win-app-1-http-route \
        --source=win-app-1-route.yaml \
        --location=global
      
    4. 为第二个应用创建 HTTPRoute YAML 文件:

      cat <<EOF> win-app-2-route.yaml
      name: win-app-2-http-route
      hostnames:
      - win-app-2
      gateways:
      - projects/$PROJECT_ID/locations/global/gateways/gateway80
      rules:
      - action:
         destinations:
       - serviceName: "projects/$PROJECT_ID/locations/global/backendServices/win-app-2-service"
      EOF
      
    5. 为第二个应用创建 HTTPRoute 资源:

      gcloud network-services http-routes import win-app-2-http-route \
        --source=win-app-2-route.yaml \
        --location=global
      

    部署和公开 Envoy 网关

    创建两个基于 Windows 的测试应用和 Cloud Service Mesh 后,您可以创建部署 YAML 文件以部署 Envoy 网关。部署 YAML 文件会完成以下任务:

    • 引导 Envoy 网关。
    • 配置 Envoy 网关以使用 Cloud Service Mesh 作为其控制平面。
    • 配置 Envoy 网关以将 HTTPRoutes 用于名为 Gateway80 的网关。

    部署两个副本 Envoy 网关。这种方法有助于使网关具有容错能力并提供冗余。如需根据负载自动扩缩 Envoy 网关,您可以视需要配置 Pod 横向自动扩缩器。如果您决定配置 Pod 横向自动扩缩器,则必须按照配置 Pod 横向自动扩缩中的说明进行操作。

    1. 在 Cloud Shell 中,创建一个 YAML 文件:

      apiVersion: apps/v1
      kind: Deployment
          metadata:
        creationTimestamp: null
        labels:
          app: td-envoy-gateway
        name: td-envoy-gateway
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: td-envoy-gateway
        template:
          metadata:
            creationTimestamp: null
            labels:
              app: td-envoy-gateway
          spec:
            containers:
            - name: envoy
              image: envoyproxy/envoy:v1.21.6
              imagePullPolicy: Always
              resources:
                limits:
                  cpu: "2"
                  memory: 1Gi
                requests:
                  cpu: 100m
                  memory: 128Mi
              env:
              - name: ENVOY_UID
                value: "1337"
              volumeMounts:
                - mountPath: /etc/envoy
                  name: envoy-bootstrap
            initContainers:
            - name: td-bootstrap-writer
              image: gcr.io/trafficdirector-prod/xds-client-bootstrap-generator
              imagePullPolicy: Always
              args:
                - --project_number='my_project_number'
                - --scope_name='gateway-proxy'
                - --envoy_port=8080
                - --bootstrap_file_output_path=/var/lib/data/envoy.yaml
                - --traffic_director_url=trafficdirector.googleapis.com:443
                - --expose_stats_port=15005
              volumeMounts:
                - mountPath: /var/lib/data
                  name: envoy-bootstrap
            volumes:
              - name: envoy-bootstrap
                emptyDir: {}
      
      • my_project_number 替换为您的项目编号。

        • 您可以通过运行以下命令找到项目编号:
        gcloud projects describe $(gcloud config get project)
         --format="value(projectNumber)"
        

      端口 15005 用于公开名为 /stats 的 Envoy 管理端点。它还用于以下用途:

      • 作为内部应用负载均衡器的响应端点。
      • 作为从 Envoy 使用 Google Cloud Managed Service for Prometheus 指标的方式。

      当两个 Envoy 网关 Pod 运行时,请创建一个类型为 ClusterIP 的服务来公开它们。您还必须创建一个名为 BackendConfig 的 YAML 文件。BackendConfig 定义了非标准响应能力检查。该检查用于验证 Envoy 网关的响应能力。

    2. 如需使用非标准响应能力检查来创建后端配置,请创建一个名为 envoy-backendconfig 的 YAML 文件:

      apiVersion: cloud.google.com/v1
      kind: BackendConfig
      metadata:
        name: envoy-backendconfig
      spec:
        healthCheck:
          checkIntervalSec: 5
          timeoutSec: 5
          healthyThreshold: 2
          unhealthyThreshold: 3
          type: HTTP
          requestPath: /stats
          port: 15005
      

      响应能力检查将使用端口 15005 上的 /stats 端点持续检查 Envoy 网关的响应能力。

    3. 创建 Envoy 网关服务:

      apiVersion: v1
      kind: Service
      metadata:
        name: td-envoy-gateway
        annotations:
          cloud.google.com/backend-config: '{"default": "envoy-backendconfig"}'
      spec:
        type: ClusterIP
        selector:
          app: td-envoy-gateway
        ports:
        - name: http
          protocol: TCP
          port: 8080
          targetPort: 8080
        - name: stats
          protocol: TCP
          port: 15005
          targetPort: 15005
      
    4. 查看您创建的 Envoy 网关服务:

      kubectl get svc td-envoy-gateway
      

    创建 Kubernetes Gateway 资源

    创建 Kubernetes Gateway 资源会预配内部应用负载均衡器,以公开 Envoy 网关。

    在创建该资源之前,您必须创建两个示例自签名证书,然后将其作为 Kubernetes Secret 导入 GKE 集群。证书可启用以下网关架构:

    • 每个应用程序均通过 HTTPS 获得服务。
    • 每个应用都使用专用证书。

    使用自行管理的证书时,内部应用负载均衡器可以使用证书数量上限来公开具有不同完全限定域名的应用。

    如需创建证书,请使用 openssl

    1. 在 Cloud Shell 中,为第一个证书生成配置文件:

      cat <<EOF >CONFIG_FILE
      [req]
      default_bits              = 2048
      req_extensions            = extension_requirements
      distinguished_name        = dn_requirements
      prompt                    = no
      [extension_requirements]
      basicConstraints          = CA:FALSE
      keyUsage                  = nonRepudiation, digitalSignature, keyEncipherment
      subjectAltName            = @sans_list
      [dn_requirements]
      0.organizationName        = example
      commonName                = win-webserver-1.example.com
      [sans_list]
      DNS.1                     = win-webserver-1.example.com
      EOF
      
    2. 为第一个证书生成私钥:

      openssl genrsa -out sample_private_key 2048
      
    3. 生成证书请求:

      openssl req -new -key sample_private_key -out CSR_FILE -config CONFIG_FILE
      
    4. 签署并生成第一个证书:

      openssl x509 -req -signkey sample_private_key -in CSR_FILE -out sample.crt     -extfile CONFIG_FILE -extensions extension_requirements -days 90
      
    5. 为第二个证书生成配置文件:

      cat <<EOF >CONFIG_FILE2
      [req]
      default_bits              = 2048
      req_extensions            = extension_requirements
      distinguished_name        = dn_requirements
      prompt                    = no
      [extension_requirements]
      basicConstraints          = CA:FALSE
      keyUsage                  = nonRepudiation, digitalSignature, keyEncipherment
      subjectAltName            = @sans_list
      [dn_requirements]
      0.organizationName        = example
      commonName                = win-webserver-2.example.com
      [sans_list]
      DNS.1                     = win-webserver-2.example.com
      EOF
      
    6. 为第二个证书生成私钥:

      openssl genrsa -out sample_private_key2 2048
      
    7. 生成证书请求:

      openssl req -new -key sample_private_key2 -out CSR_FILE2 -config CONFIG_FILE2
      
    8. 签署并生成第二个证书:

      openssl x509 -req -signkey sample_private_key2 -in CSR_FILE2 -out sample2.crt     -extfile CONFIG_FILE2 -extensions extension_requirements -days 90
      

    将证书作为 Kubernetes Secret 导入

    在本部分中,您将完成以下任务:

    • 将自签名证书作为 Kubernetes Secret 导入到 GKE 集群。
    • 为内部 VPC 创建静态 IP 地址。
    • 创建 Kubernetes Gateway API 资源。
    • 验证证书是否有效。
    1. 在 Cloud Shell 中,将第一个证书作为 Kubernetes Secret 导入:

      kubectl create secret tls sample-cert --cert sample.crt --key sample_private_key
      
    2. 将第二个证书作为 Kubernetes Secret 导入:

      kubectl create secret tls sample-cert-2 --cert sample2.crt --key sample_private_key2
      
    3. 如需启用内部应用负载均衡器,请在内部 VPC 上创建静态 IP 地址:

      gcloud compute addresses create sample-ingress-ip --region us-central1 --subnet default
      
    4. 创建 Kubernetes Gateway API 资源 YAML 文件:

      kind: Gateway
      apiVersion: gateway.networking.k8s.io/v1beta1
      metadata:
        name: internal-https
      spec:
        gatewayClassName: gke-l7-rilb
        addresses:
          - type: NamedAddress
            value: sample-ingress-ip
        listeners:
        - name: https
          protocol: HTTPS
          port: 443
          tls:
            mode: Terminate
            certificateRefs:
            - name: sample-cert
            - name: sample-cert-2
      

      默认情况下,Kubernetes 网关没有默认路由。向网关发送请求时,网关会返回“未找到网页 (404)”错误。

    5. 为 Kubernetes Gateway 配置默认的 route YAML 文件,以将所有传入请求传递给 Envoy 网关:

        kind: HTTPRoute
        apiVersion: gateway.networking.k8s.io/v1beta1
        metadata:
          name: envoy-default-backend
        spec:
          parentRefs:
          - kind: Gateway
            name: internal-https
          rules:
          - backendRefs:
            - name: td-envoy-gateway
              port: 8080
      

      向这两个应用发送 HTTP 请求,以验证整个流程。如需验证 Envoy 网关是否将流量路由到正确的应用 Pod,请检查 HTTP Host 标头。

    6. 查找 Kubernetes Gateway IP 地址并将其存储在环境变量中:

      export EXTERNAL_IP=$(kubectl get gateway internal-https -o json | jq .status.addresses[0].value -r)
      
    7. 向第一个应用发送请求:

      curl --insecure -H "Host: win-app-1" https://$EXTERNAL_IP/hostName
      
    8. 向第二个应用发送请求:

      curl --insecure -H "Host: win-app-2" https://$EXTERNAL_IP/hostName
      
    9. 验证从请求返回的主机名是否与运行 win-app-1win-app-2 的 Pod 匹配:

      kubectl get pods
      

      输出应显示 win-app-1win-app-2

    监控 Envoy 网关

    使用 Google Cloud Managed Service for Prometheus 监控您的 Envoy 网关。

    默认情况下,应在您之前创建的集群上启用 Google Cloud Managed Service for Prometheus。

    1. 在 Cloud Shell 中,通过应用以下 YAML 文件创建 PodMonitoring 资源:

      apiVersion: monitoring.googleapis.com/v1
      kind: PodMonitoring
      metadata:
        name: prom-envoy
      spec:
        selector:
          matchLabels:
            app: td-envoy-gateway
        endpoints:
        - port: 15005
          interval: 30s
          path: /stats/prometheus
      

      应用 YAML 文件后,系统会开始在信息中心内收集 Google Cloud Managed Service for Prometheus 指标。

    2. 如需创建 Google Cloud Managed Service for Prometheus 指标信息中心,请按照以下说明操作:

      1. 登录 Google Cloud 控制台。
      2. 打开 菜单
      3. 点击运维 > Monitoring > 信息中心
    3. 如需导入信息中心,请按以下说明操作:

      1. 在“信息中心”页面上,点击示例库
      2. 在过滤条件框中输入 Envoy。
      3. 点击 Istio Envoy Prometheus 概览
      4. 选中复选框。
      5. 点击导入,然后点击确认以导入信息中心。
    4. 如需查看信息中心,请按以下说明操作:

      1. 点击信息中心列表
      2. 选择集成
      3. 点击 Istio Envoy Prometheus 概览以查看该信息中心。

    您现在可以查看 Envoy 网关最重要的指标。您还可以根据自己的条件配置提醒。在清理之前,请向应用再发送几个测试请求,并查看信息中心如何使用最新指标进行更新。

    清理

    为避免因本部署中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留该项目但删除各个资源。

    删除项目

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    后续步骤

    贡献者

    作者:Eitan Eibschutz | 员工技术解决方案顾问

    其他贡献者: