在 GKE 上设置 Envoy Sidecar 服务网格

本页介绍了如何在 GKE 上设置 Envoy Sidecar 服务网格。

前提条件

首先,本指南假定您已完成以下操作:

设置服务

  1. 创建一个 HTTP 示例服务:

    kubectl apply -f - <<EOF
    kind: Namespace
    apiVersion: v1
    metadata:
      name: sidecar-example
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: whereami
      namespace: sidecar-example
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: whereami
      template:
        metadata:
          labels:
            app: whereami
        spec:
          containers:
          - name: whereami
            image: gcr.io/google-samples/whereami:v1.2.20
            ports:
            - containerPort: 8080
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: whereami
      namespace: sidecar-example
    spec:
      selector:
        app: whereami
      ports:
      - port: 8080
        targetPort: 8080
    EOF
    
  2. 为该服务创建基准 HTTPRoute:

    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: HTTPRoute
    metadata:
      name: whereami-route
      namespace: sidecar-example
    spec:
      parentRefs:
      - name: whereami
        kind: Service
        group: ""
      rules:
      - backendRefs:
        - name: whereami
          port: 8080
    EOF
    

    或者,您也可以参考以下清单,了解 gRPC 服务示例:

    apiVersion: v1
    kind: Service
    metadata:
      name: sample-service
      namespace: sample-ns
      annotations:
        networking.gke.io/app-protocols: '{"50051": "HTTP2"}' # 50051 is backendref.port
    spec:
      ports:
      - port: 50051
        targetPort: 50051
    

设置客户端

  1. 运行以下命令以在 sidecar-example 命名空间中启用 Sidecar 注入:

    kubectl label namespace sidecar-example csm-injection=enabled
    
  2. 创建客户端:

    kubectl apply -f - <<EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        run: client
      name: client
      namespace: sidecar-example
    spec:
      replicas: 1
      selector:
        matchLabels:
          run: client
      template:
        metadata:
          labels:
            run: client
        spec:
          containers:
          - name: client
            image: curlimages/curl
            command:
            - sh
            - -c
            - while true; do sleep 1; done
    EOF
    
  3. 验证客户端 Pod 是否自动注入了 Envoy Sidecar 容器:

    kubectl get pods -n sidecar-example -l run=client
    

    输出类似于以下内容:

    NAME                    READY   STATUS    RESTARTS   AGE
    client-xxxx             2/2     Running   0          20s
    

    等待客户端准备就绪并让 Status 运行,然后 继续。

  4. 验证 Envoy-sidecar 服务网格设置。以下命令会将 客户端向 whereami 服务发出的请求

    CLIENT_POD=$(kubectl get pod -n sidecar-example -l run=client -o=jsonpath='{.items[0].metadata.name}')
    
    # The VIP where the following request will be sent. Because all requests
    # from the client container are redirected to the Envoy proxy sidecar, you
    # can use any IP address, including 10.0.0.2, 192.168.0.1, and others.
    VIP='10.0.0.1'
    
    TEST_CMD="curl -v -H 'host: whereami.sidecar-example.svc.cluster.local' $VIP"
    
    kubectl exec -it $CLIENT_POD -n sidecar-example -c client -- /bin/sh -c "$TEST_CMD"
    

    输出类似于以下内容:

    < Trying 10.0.0.1:80...
    < Connected to 10.0.0.1 (10.0.0.1) port 80 (#0)
    < GET / HTTP/1.1
    < Host: whereami
    < User-Agent: curl/7.82.0-DEV
    < Accept: */*
    <
    < Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < content-type: application/json
    < content-length: 318
    < access-control-allow-origin: *
    < server: envoy
    < date: Tue, 12 Apr 2022 22:30:13 GMT
    <
    {
      "cluster_name": "${CLUSTER_NAME}",
      "location": "${LOCATION}",
      "host_header": "whereami",
      ...
    }