設定跨專案網路端點群組

跨專案網路端點群組 (NEG) 功能可讓客戶將不同專案的 NEG 附加至 Traffic Director/Cloud Service Mesh BackendService,實現下列用途:

  • 在多專案部署中,BackendServices 與其相關聯的路由和流量政策,以及來自不同專案的後端端點。

  • 在多專案部署中,您可以在單一集中式Google Cloud 專案中管理所有運算資源 (Compute Engine VM、GKE NEG 等),而服務團隊則擁有各自的 Google Cloud 服務專案,並在各自的服務專案中定義以 BackendServices 表示的服務政策,以及服務路由路徑。這樣一來,服務團隊就能委派管理服務,同時密切控管可能在不同服務團隊之間共用的運算資源。

本頁說明如何建立基準 2 專案設定,其中專案 A 中的 NEG (稱為工作負載專案) 會附加至專案 B 中的 BackendService (稱為政策專案)。以下範例會在工作負載專案的預設虛擬私有雲網路中設定工作負載 VM,並示範用戶端如何根據政策專案中的設定,將流量轉送至工作負載專案。

跨專案 NEG 基準範例

在更複雜的設定中,如果要跨多個專案建立相互連結的資料層,就需要使用共用虛擬私有雲等解決方案。這也表示 NEG 端點具有不重複的 IP。這個範例設定可擴充至更複雜的情況,例如工作負載位於跨多個專案的共用虛擬私有雲網路中。

限制

適用一般 Traffic Director 限制BackendService/NetworkEndpointGroup 限制

以下限制也適用於多個專案設定,但可能不限於此:

  • 單一 BackendService 最多只能支援 50 個後端(包括 NEG 和 MIG)。
  • 僅支援 GCP_VM_IP_PORT 類型的區域 NEG。
  • 不支援跨專案 BackendServices 對執行個體群組 (代管或非代管) 的參照。
  • 系統不支援列出可附加至特定 BackendService 的跨專案 NEG。
  • 系統不支援列出使用特定 NEG 的跨專案 BackendServices。

事前準備

您必須完成下列先決條件,才能設定跨專案 NEG。

啟用必要的 API

您必須啟用下列 API,才能完成本指南的操作:

  • osconfig.googleapis.com
  • trafficdirector.googleapis.com
  • compute.googleapis.com
  • networkservices.googleapis.com

執行下列指令,在工作負載專案和政策專案中啟用必要的 API:

gcloud services enable --project PROJECT_ID \
    osconfig.googleapis.com \
    trafficdirector.googleapis.com \
    compute.googleapis.com \
    networkservices.googleapis.com

授予必要的 IAM 權限

您必須具備足夠的 Identity and Access Management (IAM) 權限,才能完成本指南。如果您在啟用 Cloud Service Mesh 的專案中具備專案擁有者或編輯者角色 (roles/ownerroles/editor),就會自動取得正確的權限。

否則,您必須授予下列所有 IAM 角色。如果您具備這些角色,也將具備相關權限,詳情請參閱 Compute Engine IAM 說明文件

工作負載和政策專案需要下列角色:

  • roles/iam.serviceAccountAdmin
  • roles/serviceusage.serviceUsageAdmin
  • roles/compute.networkAdmin

只有工作負載專案需要下列角色:

  • roles/compute.securityAdmin
  • roles/container.admin
  • 包含下列權限的任何角色。最精細的預先定義角色包含所有將 NEG 附加至 BackendService 所需的權限,該角色為 roles/compute.loadBalancerServiceUser
    • compute.networkEndpointGroups.get
    • compute.networkEndpointGroups.use

此外,Traffic Director 管理的 xDS 用戶端 (例如 Envoy Proxy) 必須具備 roles/trafficdirector.client 下的權限。為示範起見,您可以使用下列指令,將政策專案中的這項權限授予工作負載專案的預設運算服務帳戶:

gcloud projects add-iam-policy-binding POLICY_PROJECT_ID \
    --member "serviceAccount:WORKLOAD_PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
    --role "roles/trafficdirector.client"

其中

  • POLICY_PROJECT_ID 是政策專案的 ID。
  • WORKLOAD_PROJECT_NUMBER 是工作負載專案的專案編號。

在工作負載專案中設定服務後端

  1. 執行下列指令,將 Google Cloud CLI 指向政策專案,並設定偏好的 Google Cloud 運算區:

    gcloud config set project WORKLOAD_PROJECT_ID
    gcloud config set compute/zone ZONE
    

    其中

    • WORKLOAD_PROJECT_ID 是工作負載專案的 ID。
    • ZONE 是 GKE 叢集的區域,例如 us-central1
  2. 建立 GKE 叢集。為方便說明,下列指令會建立可用區 GKE 叢集。不過,這項功能也適用於地區性 GKE 叢集。

    gcloud container clusters create test-cluster \
        --scopes=https://www.googleapis.com/auth/cloud-platform
        --zone=ZONE
    
  3. 建立防火牆規則:

    gcloud compute firewall-rules create http-allow-health-checks \
        --network=default \
        --action=ALLOW \
        --direction=INGRESS \
        --source-ranges=35.191.0.0/16,130.211.0.0/22 \
        --rules=tcp:80
    

    防火牆規則可讓 Google Cloud 控制平面將健康狀態檢查探測器傳送至預設 VPC 網路中的後端。

  4. 將 kubectl 目前的背景資訊變更為新建立的叢集:

    gcloud container clusters get-credentials test-cluster \
        --zone=ZONE
    
  5. 建立及部署 whereami 範例應用程式:

    kubectl apply -f - <<EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: whereami
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: whereami
      template:
        metadata:
          labels:
            app: whereami
        spec:
          containers:
          - name: whereami
            image: us-docker.pkg.dev/google-samples/containers/gke/whereami:v1
            ports:
            - containerPort: 8080
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: whereami
      annotations:
        cloud.google.com/neg: '{"exposed_ports":{"8080":{"name": "example-neg"}}}'
    spec:
      selector:
        app: whereami
      ports:
      - port: 8080
        targetPort: 8080
    EOF
    
  6. 執行下列指令,將 NEG 的參照值儲存在變數中:

    NEG_LINK=$(gcloud compute network-endpoint-groups describe example-neg --format="value(selfLink)")
    

    NEG 控制器會自動為每個區域中的服務後端建立區域性 NetworkEndpointGroup。在這個範例中,NEG 名稱已硬式編碼為 example-neg。在下一個工作階段中,當您將這個 NEG 附加至政策專案中的 BackendService 時,將其儲存為變數會很有用。

    舉例來說,$NEG_LINK 應如下所示:

    $ echo ${NEG_LINK}
    https://www.googleapis.com/compute/v1/projects/WORKLOAD_PROJECT/zones/ZONE/networkEndpointGroups/example-neg
    

    或者,您也可以讀取服務上的 neg-status 註解,擷取 NEG URL:

    kubectl get service whereami -o jsonpath="{.metadata.annotations['cloud\.google\.com/neg-status']}"
    NEG_LINK="https://www.googleapis.com/compute/v1/projects/WORKLOAD_PROJECT_ID/zones/ZONE/networkEndpointGroups/example-neg"
    

在政策專案中設定 Google Cloud 網路資源

  1. 將 Google Cloud CLI 指向政策專案:

    gcloud config set project POLICY_PROJECT_ID
    
  2. 設定網格資源:

    gcloud network-services meshes import example-mesh --source=- --location=global << EOF
    name: example-mesh
    EOF
    

    網格資源的名稱是 sidecar proxy 用來要求服務網格設定的鍵。

  3. 使用健康狀態檢查設定基準 BackendService

    gcloud compute health-checks create http http-example-health-check
    
    gcloud compute backend-services create example-service \
      --global \
      --load-balancing-scheme=INTERNAL_SELF_MANAGED \
      --protocol=HTTP \
      --health-checks http-example-health-check
    
  4. 將先前建立的 NetworkEndpointGroup 附加至 BackendService

    gcloud compute backend-services add-backend example-service --global \
      --network-endpoint-group=${NEG_LINK} \
      --balancing-mode=RATE \
      --max-rate-per-endpoint=5
    
  5. 建立 HTTPRoute,將所有含有主機標頭 example-service 的 HTTP 要求,導向工作負載專案中的伺服器:

    gcloud network-services http-routes import example-route --source=- --location=global << EOF
    name: example-route
    hostnames:
    - example-service
    meshes:
    - projects/POLICY_PROJECT_NUMBER/locations/global/meshes/example-mesh
    rules:
    - action:
        destinations:
        - serviceName: "projects/POLICY_PROJECT_NUMBER/locations/global/backendServices/example-service"
    EOF
    

    其中 POLICY_PROJECT_NUMBER 是政策專案的專案編號。

確認設定

您可以將 HTTP 要求傳送至 Traffic Director 管理的補充 Proxy 後方的 VIP,並將 HOST 標頭設為 example-service,藉此驗證設定:

curl -H "Host: example-service" http://10.0.0.1/

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

{"cluster_name":"test-cluster","gce_instance_id":"4879146330986909656","gce_service_account":"...","host_header":"example-service","pod_name":"whereami-7fbffd489-nhkfg","pod_name_emoji":"...","project_id":"...","timestamp":"2024-10-15T00:42:22","zone":"us-west1-a"}

請注意,由於 Pod 的所有傳出流量都會在服務網格中遭到 Envoy 附加元件攔截,而先前的 HTTPRoute 會根據 L7 屬性 (主機標頭) 將所有流量傳送至「whereami」Kubernetes 服務。為方便說明,這個指令中的 VIP 為 10.0.0.1,但 VIP 可以是任何 IP。

副車 Proxy 應要求與政策專案中網格資源相關聯的設定。如要這樣做,請確認節點 ID 已設為以下格式:

projects/POLICY_PROJECT_NUMBER/networks/mesh:example-mesh/nodes/UUID"

後續步驟