将流量从 Cloud Service Mesh 工作负载路由到 Compute Engine 虚拟机
本页介绍了如何将网络流量从 GKE 上的 Cloud Service Mesh 工作负载安全地路由到由 BackendService 充当前端的 Compute Engine 虚拟机。
请注意,将流量从 GKE 路由到 Compute Engine 虚拟机时,无需让 Compute Engine 虚拟机或 BackendService 加入 Cloud 服务网格。不过,Compute Engine 虚拟机和 BackendService 必须与 Cloud Service Mesh GKE 集群位于同一项目中。此功能在公开预览版阶段存在此限制。Compute Engine 虚拟机不支持 MTLS
准备工作
以下部分假定您已完成以下操作:
或者,您也可以运行以下命令,部署一个由 BackendService 充当前端的示例 Compute Engine 虚拟机。
部署示例 Compute Engine 虚拟机和 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 虚拟机的可用区。
将 Compute Engine 虚拟机配置为 GCPBackend
在本部分中,您将使用 GCPBackend 将 Compute Engine VM 公开给 GKE 工作负载。GCPBackend 由以下部分组成:
- 前端信息,具体而言,是 GKE 工作负载用于调用此 GCPBackend 的主机名和端口。
- 后端信息 - BackendService 详细信息,例如服务名称、位置和项目编号。
GCPBackend 包含主机名和端口详细信息,以及 BackendService 详细信息(服务名称、位置和项目编号)。GKE 工作负载应在其 HTTP 请求中使用 GCPBackend 主机名和端口来访问 Compute Engine 虚拟机。
若要使主机名 DNS 在集群内可解析(默认情况下不可解析),您必须配置 DNS,以将所选主机名下的所有主机解析为任意 IP 地址。 Google Cloud 在您配置此 DNS 条目之前,请求会失败。 Google Cloud DNS 配置是每个自定义网域的一次性设置。
创建托管地区:
gcloud dns managed-zones create prod \ --description="zone for gcpbackend" \ --dns-name=gcpbackend \ --visibility=private \ --networks=default
在此示例中,DNS 名称为 gcpbackend,VPC 网络为 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 虚拟机的连接:
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 工作负载可以通过向 hello-world.gcpbackend:80 发送 HTTP 请求来访问 Compute Engine 虚拟机。
您应为 GCPBackend 使用不同的名称,以免与现有 Kubernetes 服务或 Istio 服务条目冲突。如果存在冲突,则优先级顺序(从高到低)为 Kubernetes Service、istio ServiceEntry 和 GCPBackend。
请注意,虚拟服务和 GCPBackend 必须位于同一命名空间中,并且 Compute Engine 虚拟机必须与 Cloud Service Mesh GKE 集群位于同一项目中。