設定閘道 TLS 路由
本指南說明如何使用 Gateway 和 TLSRoute 資源,設定以 Envoy Proxy 為基礎的 Ingress 閘道。你也可以附上TLSRoute資源。
下圖說明您設定的部署作業。區域性外部直通式網路負載平衡器會將流量導向 Envoy Proxy,做為 Ingress 閘道。Envoy Proxy 會使用 TLS 直通路由,將流量直接導向後端 VM 執行個體上執行的 HTTPS 伺服器。
事前準備
請務必完成「準備使用 Envoy 和無 Proxy 工作負載進行設定」一文所述的工作。
設定防火牆規則
在本節中,您會建立防火牆規則,允許連入網路中 VM 執行個體的健康狀態檢查連線。
建立防火牆規則:
gcloud compute firewall-rules create allow-gateway-health-checks \ --network=NETWORK_NAME \ --direction=INGRESS \ --action=ALLOW \ --rules=tcp \ --source-ranges="35.191.0.0/16,209.85.152.0/22,209.85.204.0/22" \ --target-tags=gateway-proxy
設定防火牆規則,允許來自任何來源的流量。編輯通訊埠和來源 IP 位址範圍的指令:
gcloud compute firewall-rules create allow-gateway-ingress-traffic \ --network=NETWORK_NAME \ --direction=INGRESS \ --action=ALLOW \ --rules=tcp:443 \ --source-ranges="0.0.0.0/0" \ --target-tags=gateway-proxy
設定 Identity and Access Management 權限
在本節中,您會為閘道 Proxy 指定服務帳戶,並將正確的 IAM 角色指派給服務帳戶。
為閘道 Proxy 建立服務帳戶身分:
gcloud iam service-accounts create gateway-proxy
將必要的 IAM 角色指派給服務帳戶身分:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:gateway-proxy@PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/trafficdirector.client"
gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:gateway-proxy@PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/logging.logWriter"
設定 Gateway 資源
在名為
gateway443.yaml的檔案中,建立 HTTP 流量的Gateway規格:name: gateway443 scope: gateway-proxy ports: - 443 type: OPEN_MESH
使用
gateway443.yaml規格建立Gateway資源:gcloud network-services gateways import gateway443 \ --source=gateway443.yaml \ --location=global
建立含有 Envoy Proxy 的代管執行個體群組
在本節中,您將建立與 Ingress 閘道相關聯的 Envoy Proxy。
為執行自動部署 Envoy 服務 Proxy 的 VM 建立執行個體範本。Envoys 的範圍設為
gateway-proxy。請勿將服務通訊埠做為--service-proxy標記的參數傳遞。gcloud beta compute instance-templates create gateway-proxy \ --machine-type=n1-standard-1 \ --boot-disk-size=10GB \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --tags=gateway-proxy \ --network-interface=network=NETWORK_NAME,no-address \ --service-account="gateway-proxy@PROJECT_ID.iam.gserviceaccount.com" \ --service-proxy=enabled,scope=gateway-proxy
從執行個體範本建立地區代管執行個體群組:
gcloud compute instance-groups managed create gateway-proxy \ --region=REGION \ --size=1 \ --template=gateway-proxy
設定代管執行個體群組的服務通訊埠名稱:
gcloud compute instance-groups managed set-named-ports gateway-proxy \ --named-ports=https:443 \ --region=REGION
設定區域性外部直通式網路負載平衡器
在本節中,您將建立外部直通式網路負載平衡器。
建立靜態外部區域 IP 位址:
gcloud compute addresses create xnlb-REGION \ --region=REGION
取得為外部負載平衡器保留的 IP 位址:
gcloud compute addresses describe xnlb-REGION \ --region=REGION --format='value(address)'
這個 IP 位址會在本設定指南中稍後做為變數
IP_ADDRESS使用。為閘道 Proxy 建立健康狀態檢查:
gcloud compute health-checks create tcp xnlb-REGION \ --region=REGION \ --use-serving-port
為閘道 Proxy 建立後端服務:
gcloud compute backend-services create xnlb-REGION \ --health-checks=xnlb-REGION \ --health-checks-region=REGION \ --load-balancing-scheme=EXTERNAL \ --protocol=TCP \ --region=REGION \ --port-name=https
將代管執行個體群組新增為後端:
gcloud compute backend-services add-backend xnlb-REGION \ --instance-group=gateway-proxy \ --instance-group-region=REGION \ --region=REGION
建立轉送規則,將流量轉送至閘道 Proxy:
gcloud compute forwarding-rules create xnlb-REGION \ --region=REGION \ --load-balancing-scheme=EXTERNAL \ --address=IP_ADDRESS \ --ip-protocol=TCP \ --ports=443 \ --backend-service=xnlb-REGION \ --backend-service-region=REGION
設定執行 HTTPS 服務的代管執行個體群組
為示範之用,請在代管執行個體群組中,使用自動調度資源的 VM 建立後端服務。VM 會使用通訊埠 443 上的 HTTPS 通訊協定,回應網路要求的詳細資料。
建立執行個體範本,其中包含在通訊埠
443上公開的 HTTPS 服務:gcloud compute instance-templates create td-https-vm-template \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --tags=https-td-server \ --image-family=debian-10 \ --image-project=debian-cloud \ --metadata=startup-script='#! /bin/bash sudo rm -rf /var/lib/apt/lists/* sudo apt-get -y clean sudo apt-get -y update sudo apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common sudo curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" sudo apt-get -y update sudo apt-get -y install docker-ce sudo which docker echo "{ \"registry-mirrors\": [\"https://mirror.gcr.io\"] }" | sudo tee -a /etc/docker/daemon.json sudo service docker restart sudo docker run -e HTTPS_PORT=9999 -p 443:9999 --rm -dt mendhak/http-https-echo:22'依據執行個體範本建立代管執行個體群組:
gcloud compute instance-groups managed create https-td-mig-us-REGION \ --zone=ZONE \ --size=2 \ --template=td-https-vm-template
設定代管執行個體群組的服務通訊埠名稱:
gcloud compute instance-groups managed set-named-ports https-td-mig-us-REGION \ --named-ports=https:443 \ --zone=ZONE
建立健康狀態檢查:
gcloud compute health-checks create https https-helloworld-health-check \ --port=443
建立防火牆規則,允許傳入的健康狀態檢查連線抵達您網路中的執行個體:
gcloud compute firewall-rules create https-vm-allow-health-checks \ --network NETWORK_NAME --action allow --direction INGRESS \ --source-ranges 35.191.0.0/16,130.211.0.0/22 \ --target-tags https-td-server \ --rules tcp:443
使用
INTERNAL_SELF_MANAGED的負載平衡機制建立全域後端服務,並新增健康狀態檢查:gcloud compute backend-services create https-helloworld-service \ --global \ --load-balancing-scheme=INTERNAL_SELF_MANAGED \ --port-name=https \ --health-checks https-helloworld-health-check
將代管執行個體群組新增至後端服務,做為後端使用:
gcloud compute backend-services add-backend https-helloworld-service \ --instance-group=https-td-mig-us-REGION \ --instance-group-zone=ZONE \ --global
使用 TLSRoute 資源設定路由
在先前的章節中,您設定了 Gateway 資源和 HTTPS 伺服器。接著,使用 TLSRoute 資源將 SNI 主機名稱與後端服務建立關聯。
在名為
tls_route.yaml的檔案中,建立TLSRoute規格:name: helloworld-tls-route gateways: - projects/PROJECT_NUMBER/locations/global/gateways/gateway443 rules: - matches: - sniHost: - example.com alpn: - h2 action: destinations: - serviceName: projects/PROJECT_NUMBER/locations/global/backendServices/https-helloworld-service在先前的指令中,
TLSRoute會將example.com視為 SNI 和h2,並將h2視為 ALPN。如果比對結果變更如下,則TLSRoute會比對 SNI 或 ALPN:- matches: - sniHost: - example.com - alpn: - h2使用
tls_route.yaml規格建立TLSRoute資源:gcloud network-services tls-routes import helloworld-tls-route \ --source=tls_route.yaml \ --location=global
Cloud Service Mesh 已設定為指定服務進行負載平衡,其為代管執行個體群組後端間的 TLSRoute 資源。
驗證部署作業
在本節中,您將驗證是否能透過外部直通式網路負載平衡器和 Cloud Service Mesh Gateway 資源,從外部用戶端存取服務。
執行下列
curl指令,驗證與您建立的測試服務之間的 HTTP 連線:curl https://example.com --resolve example.com:443:IP_ADDRESS -k
指令會傳回代管執行個體群組中其中一個 VM 的回應。輸出內容如下:
"path": "/",
"headers": {
"host": "example.com",
"user-agent": "curl/7.81.0",
"accept": "*/*"
},
"method": "GET",
"body": "",
"fresh": false,
"hostname": "example.com",
"ip": "::ffff:10.142.0.2",
"ips": [],
"protocol": "https",
"query": {},
"subdomains": [],
"xhr": false,
"os": {
"hostname": "0cd3aec9b351"
},
"connection": {
"servername": "example.com"
}
}
使用負向驗證進行驗證
您也可以執行負向驗證。如果您執行本節中的指令,系統會捨棄要求,因為要求不符合 TLSRoute 比對條件。
在下列指令中,SNI 與 example.com 不符,因此 Gateway 會拒絕連線:
curl https://invalid-server.com --resolve invalid-server.com:443:IP_ADDRESS -k
在下列指令中,ALPN 不符合 h2 (HTTP2 通訊協定),因此 Gateway 會拒絕連線:
curl https://example.com --resolve example.com:443:IP_ADDRESS -k --http1.1
在下列指令中,用戶端會建立純文字 (未加密) 連線,因此 Gateway 會拒絕連線:
curl example.com:443 --resolve example.com:443:IP_ADDRESS -k
上述指令都會傳回下列錯誤:
curl: (35) OpenSSL SSL_connect: Connection reset by peer in connection.
後續步驟
- 如要瞭解如何列出與
Mesh或Gateway資源相關聯的路徑資源,請參閱「列出Route資源」。