將 Cloud Service Mesh 工作負載的流量路由至 Compute Engine VM
本頁面說明如何將 GKE 上 Cloud Service Mesh 工作負載的網路流量,安全地轉送至以 BackendService 為前端的 Compute Engine VM。
請注意,從 GKE 將流量轉送至 Compute Engine VM 時,Compute Engine VM 或 BackendService 不必加入 Cloud Service Mesh。不過,Compute Engine VM 和 BackendService 必須與 Cloud Service Mesh GKE 叢集位於同一專案。這項功能為公開預先發布版,因此有這項限制。Compute Engine VM 不支援 MTLS
事前準備
以下各節假設您已具備下列條件:
或者,您也可以執行下列指令,部署由 BackendService 做為前端的範例 Compute Engine VM。
部署範例 Compute Engine VM 和 BackendService:
gcloud compute instance-templates create td-httpd-vm-template \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --tags=http-td-server \ --image-family=debian-11 \ --image-project=debian-cloud \ --metadata=startup-script="#! /bin/bash sudo apt-get update -y sudo apt-get install apache2 -y sudo service apache2 restart echo '<!doctype <html><body><h1>'\`$(/bin/hostname)\`'</h1></body></html>' | sudo tee /var/www/html/index.html" gcloud compute instance-groups managed create http-td-mig-us-east1 \ --zone=VM_ZONE \ --size=2 \ --template=td-httpd-vm-template gcloud compute health-checks create http http-helloworld-health-check gcloud compute firewall-rules create http-vm-allow-health-checks \ --network=default \ --action=ALLOW \ --direction=INGRESS \ --source-ranges=0.0.0.0/0 \ --target-tags=http-td-server \ --rules=tcp:80 gcloud compute backend-services create helloworld \ --global \ --load-balancing-scheme=INTERNAL_SELF_MANAGED \ --protocol=HTTP \ --health-checks http-helloworld-health-check gcloud compute backend-services add-backend helloworld \ --instance-group=http-td-mig-us-east1 \ --instance-group-zone=VM_ZONE \ --global
其中:
- VM_ZONE 是要部署 Compute Engine VM 的區域。
將 Compute Engine VM 設定為 GCPBackend
在本節中,您會使用 GCPBackend 將 Compute Engine VM 公開給 GKE 工作負載。GCPBackend 包含:
- 前端資訊,具體來說,就是 GKE 工作負載用來呼叫這個 GCPBackend 的主機名稱和通訊埠。
- 後端資訊 - BackendService 詳細資料,例如服務名稱、位置和專案編號。
GCPBackend 包含主機名稱和通訊埠詳細資料,以及 BackendService 詳細資料 (服務名稱、位置和專案編號)。GKE 工作負載應在 HTTP 要求中使用 GCPBackend 主機名稱和通訊埠,存取 Compute Engine VM。
如要在叢集內解析主機名稱的 DNS (預設無法解析),您必須設定 Google Cloud DNS,將所選主機名稱下的所有主機解析為任意 IP 位址。您必須先設定這個 DNS 項目,要求才會成功。 Google Cloud DNS 設定是每個自訂網域的一次性設定。
建立代管區域:
gcloud dns managed-zones create prod \ --description="zone for gcpbackend" \ --dns-name=gcpbackend \ --visibility=private \ --networks=default
在本範例中,DNS 名稱為 gcpbackend,虛擬私有雲網路為 default。
設定記錄,讓網域可解析:
gcloud beta dns record-sets create *.gcpbackend \ --ttl=3600 --type=A --zone=prod \ --rrdatas=10.0.0.1
使用先前網域下的主機名稱建立 GCPBackend:
cat <<EOF > gcp-backend.yaml apiVersion: networking.gke.io/v1 kind: GCPBackend metadata: name: vm-gcp-backend namespace: NAMESPACE spec: type: "BackendService" hostname: hello-world.gcpbackend backendservice: name: helloworld location: global EOF kubectl apply -f gcp-backend.yaml
在本例中,GCP_BACKEND_NAME 為
vm-gcp-backend
。建立測試 Pod,驗證 GKE 與 Compute Engine VM 的連線:
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: testcurl namespace: default spec: containers: - name: curl image: curlimages/curl command: ["sleep", "3000"] EOF kubectl exec testcurl -c curl -- curl http://hello-world.gcpbackend:80
現在,您的 GKE 工作負載可以將 HTTP 要求傳送至 hello-world.gcpbackend:80,存取 Compute Engine VM。
您應為 GCPBackend 使用不同的名稱,以免與現有的 Kubernetes 服務或 Istio 服務項目發生衝突。如果發生衝突,優先順序 (由高至低) 為 Kubernetes Service、istio ServiceEntry 和 GCPBackend。
請注意,虛擬服務和 GCPBackend 必須位於相同命名空間,且 Compute Engine VM 必須與 Cloud Service Mesh GKE 叢集位於相同專案。