Private Service Connect - 將全域 XLB 適用的消費者 HTTP(S) Service Controls 使用代管服務

1. 簡介

服務供應商可透過 Private Service Connect 為服務用戶提供服務。服務供應商虛擬私有雲網路可以支援多個服務用戶。

Private Service Connect 端點可連線至已發布的服務:

  • Private Service Connect 端點 (根據轉送規則)

透過這個端點類型,使用者會連線至他們定義的內部 IP 位址。Private Service Connect 會執行網路位址轉譯 (NAT),以便將要求轉送至服務供應商。

  • 具備用戶 HTTP(S) 服務控管機制的 Private Service Connect 端點 (以全域外部 HTTP(S) 負載平衡器為基礎)

用戶可利用這個端點類型連線至外部 IP 位址。Private Service Connect 會透過網路端點群組,將要求轉送至服務供應商。

以全域外部 HTTP(S) 負載平衡器做為政策強制執行點有下列優點:

  • 您可以重新命名服務,並將其對應至您選擇的網址。
  • 您可以設定負載平衡器,將所有要求記錄到 Cloud Logging。
  • 可以使用客戶自行管理的 TLS 憑證。或 Google 代管憑證

在本程式碼研究室中,您將瞭解如何使用全域 XLB 建立 Private Service Connect 端點消費者 HTTP(S) Service Controls,以私密方式存取其他網路中的服務。這個 PSC 模式可以使用單一專案或不同專案來完成。為配合本研究室的學習目標,我們會使用含兩個不同虛擬私有雲的單一專案。

課程內容

  • 使用全域 XLB 建立採用消費者 HTTP(S) Service Controls 的 Private Service Connect 端點
  • 將代管服務設為透過服務連結公開,以接受 L7 XLB 連線。
  • 建立 SSL 憑證並設定 Apache 網路伺服器,以終止 TLS,並接受通訊埠 443 的流量。
  • 建立 PSC NEG。

軟硬體需求

  • Google Cloud 專案
  • 具備部署執行個體與設定網路元件的知識

2. 測試環境

您要建立的環境會包含消費者虛擬私有雲中的外部 HTTP(S) 負載平衡器和 PSC NEG。生產端虛擬私有雲會託管使用 HTTPS 設定的簡易 Apache 網路服務。您將透過 Apache 網路服務建立後端服務,並在前端使用設有 PSC 服務連結的內部 TCP 負載平衡器。

bbca972cf488ece.png

3. 設定和需求

自修環境設定

  1. 登入 Google Cloud 控制台,建立新專案或重複使用現有專案。如果您還沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • 「專案名稱」是這項專案參與者的顯示名稱。這是 Google API 不使用的字元字串,您可以隨時更新。
  • 所有 Google Cloud 專案的專案 ID 均不得重複,且設定後即無法變更。Cloud 控制台會自動產生一個不重複的字串。但通常是在乎它何在在大部分的程式碼研究室中,您必須參照專案 ID (通常稱為 PROJECT_ID),因此如果您不喜歡的話,請隨機產生一個,或者,您也可以自行嘗試看看是否可用。是「凍結」建立專案後
  • 還有第三個值,也就是部分 API 使用的專案編號。如要進一步瞭解這三個值,請參閱說明文件
  1. 接下來,您需要在 Cloud 控制台中啟用計費功能,才能使用 Cloud 資源/API。執行這個程式碼研究室並不會產生任何費用,如果有的話。如要關閉資源,以免產生本教學課程結束後產生的費用,請按照任「清除所用資源」操作請參閱本程式碼研究室結尾處的操作說明。Google Cloud 的新使用者符合 $300 美元免費試用計畫的資格。

啟動 Cloud Shell

雖然 Google Cloud 可以從筆記型電腦遠端操作,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是一種在 Cloud 中執行的指令列環境。

Google Cloud 控制台,按一下右上方的工具列上的 Cloud Shell 圖示:

55efc1aaa7a4d3ad.png

佈建並連線至環境的作業只需幾分鐘的時間。完成後,您應該會看到類似下方的內容:

7ffe5cbb04455448.png

這部虛擬機器都裝載了您需要的所有開發工具。提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作,大幅提高網路效能和驗證能力。這個研究室中的所有工作都可以透過瀏覽器完成。

4. 事前準備

啟用 API

在 Cloud Shell 中,確認您已設定專案 ID

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
export project=YOUR-PROJECT-NAME
export region=us-central1
echo $project
echo $region

啟用所有必要服務

gcloud services enable compute.googleapis.com
gcloud services enable servicedirectory.googleapis.com

5. 生產端虛擬私有雲、子網路、防火牆規則設定

虛擬私有雲網路

透過 Cloud Shell

gcloud compute networks create producer-vpc --subnet-mode custom

建立子網路

必須在生產端端建立子網路,才能執行 PSC 的網路位址轉譯 (NAT)。請注意,用途為 PRIVATE_SERVICE_CONNECT。這代表這個子網路無法用於部署工作負載。

透過 Cloud Shell

gcloud compute networks subnets create producer-nat-subnet \
--network=producer-vpc \
--region=$region \
--range=10.100.100.0/24 \
--purpose=PRIVATE_SERVICE_CONNECT

我們會在生產端虛擬私有雲中部署兩個子網路。第一位是第一個部署生產端服務,另一個區域則用於部署 Client-vm,以在 TCP 內部負載平衡器上透過全域存取權測試與服務之間的連線。

透過 Cloud Shell

gcloud compute networks subnets create service-subnet \
    --network=producer-vpc \
    --range=10.0.0.0/24 \
    --region=$region

透過 Cloud Shell

gcloud compute networks subnets create client-subnet \
    --network=producer-vpc \
    --range=10.0.1.0/24 \
    --region=us-east4

建立 Cloud NAT

需要 Cloud NAT,才能為我們的生產端服務安裝適當的套件。

透過 Cloud Shell

gcloud compute routers create service-cr \
--region=$region --network=producer-vpc \
--asn=65501

透過 Cloud Shell

gcloud compute routers nats create service-nat-gw \
--router=service-cr \
--router-region=$region \
--nat-custom-subnet-ip-ranges=service-subnet \
--auto-allocate-nat-external-ips

建立防火牆規則

在本研究室中,您將使用 IAP 連線至您建立的執行個體。下列防火牆規則可讓您透過 IAP 連線至執行個體。如果您不想使用 IAP,可以略過這個步驟,改為在執行個體中新增公開 IP 位址,並建立防火牆規則,允許從 0.0.0.0/0 的 TCP 通訊埠 22 輸入。

如要允許 IAP 連線至您的 VM 執行個體,請建立下列防火牆規則:

  • 適用於您要透過 IAP 存取的所有 VM 執行個體。
  • 允許來自 IP 範圍 35.235.240.0/20 的輸入流量。這個範圍包含 IAP 用於 TCP 轉送的所有 IP 位址。

透過 Cloud Shell

gcloud compute firewall-rules create allow-ssh-iap \
    --network producer-vpc \
--allow tcp:22 \
--source-ranges=35.235.240.0/20

用戶端流量將來自全域外部 HTTP(S) 負載平衡器,因此您必須建立防火牆規則,才能將流量傳送至已標記並用於代管網路服務的已標記目的地伺服器。我們也會開啟 client-subnet 的防火牆規則以進行測試。

透過 Cloud Shell

gcloud compute firewall-rules create allow-xlb-client \
  --network=producer-vpc \
  --direction=ingress \
  --allow=tcp:443 \
  --target-tags=psc-service \
  --source-ranges=130.211.0.0/22,35.191.0.0/16,10.0.1.0/24

建立 Apache 網路服務

我們將建立顯示「PSC 服務」的簡易 Apache Web Service

建立執行個體範本

透過 Cloud Shell

gcloud compute instance-templates create producer-service-template \
    --network producer-vpc \
    --subnet service-subnet \
    --region $region \
    --no-address \
    --scopes=https://www.googleapis.com/auth/cloud-platform \
    --image-family=debian-10 \
    --image-project=debian-cloud \
    --tags=psc-service \
    --metadata startup-script='#! /bin/bash
    sudo apt-get update
    apt-get install apache2 -y
    a2ensite default-ssl
    echo "PSC Service" | \
    tee /var/www/html/index.html
    systemctl restart apache2'

建立 MIG 的健康狀態檢查

透過 Cloud Shell

gcloud compute health-checks create https psc-service-mig-healthcheck \
    --port=443 \
    --global

建立代管執行個體群組

透過 Cloud Shell

gcloud compute instance-groups managed create psc-service-mig \
    --region $region \
    --size=2 \
    --template=producer-service-template \
    --health-check=psc-service-mig-healthcheck

在 Apache 網路伺服器上設定安全資料傳輸層 (SSL)

接下來,需要在每個 Apache 網路伺服器上設定 SSL。方法是產生憑證,然後將該憑證新增至 Apache 設定。

您必須在後端服務上設定 SSL 終止,因為服務必須在這種特定 PSC 模式的情況下,由內部 TCP/UDP (L4) 負載平衡器在前方。內部 TCP/UDP 負載平衡器不會在負載平衡器層終止 SSL。

請先透過 SSH 連線至 MIG 中的第一個 VM。系統會為各個環境動態分配 VM 可用區和 VM 名稱。前往控制台前往「Compute Engine」>「Compute Engine」>「找出執行個體的名稱和可用區。

透過 Cloud Shell

gcloud compute ssh --zone "<YOUR_VM_ZONE>" "<YOUR_MIG_VM_1>"  --tunnel-through-iap --project $project

接下來,透過 OpenSSL 建立憑證。系統會請您填寫國家/地區、州/省、縣市、機構、機構單位名稱、通用名稱和電子郵件地址的相關資訊。您只須填寫「Common Name」(常用名稱) 欄位,也就是您選擇的內部 FQDN。為達到本研究室的目的,您應選擇 example.com

透過 Cloud Shell

sudo openssl genrsa -out private-key-file.pem 2048

透過 Cloud Shell

cat <<'EOF' >config.txt
[req]
default_bits              = 2048
req_extensions            = extension_requirements
distinguished_name        = dn_requirements

[extension_requirements]
basicConstraints          = CA:FALSE
keyUsage                  = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName            = @sans_list

[dn_requirements]
countryName               = Country Name (2 letter code)
stateOrProvinceName       = State or Province Name (full name)
localityName              = Locality Name (eg, city)
0.organizationName        = Organization Name (eg, company)
organizationalUnitName    = Organizational Unit Name (eg, section)
commonName                = Common Name (e.g. server FQDN or YOUR name)
emailAddress              = Email Address

[sans_list]
DNS.1                     = example.com

EOF

透過 Cloud Shell

sudo openssl req -new -key private-key-file.pem \
    -out csr.pem \
    -config config.txt

透過 Cloud Shell

sudo openssl x509 -req \
    -signkey private-key-file.pem \
    -in csr.pem \
    -out cert.cert \
    -extfile config.txt \
    -extensions extension_requirements \
    -days 10

現在讓我們使用新的憑證詳細資料來更新 Apache 設定資訊。

sudo vi /etc/apache2/sites-enabled/default-ssl.conf

在 ServerAdmin 下方新增一行,顯示以下程式碼:

ServerName example.com

在 VM 上更新 cert.cert 檔案位置和 private-key-file.pem 位置的 SSLCertificateFile 和 SSLCertificateKeyFile。範例如下所示。請務必更新 <profile>改為您的目錄名稱。

SSLCertificateFile  /home/<profile>/cert.cert

SSLCertificateKeyFile /home/<profile>/private-key-file.pem

關閉編輯器,然後重新啟動 Apache。

sudo a2enmod ssl
sudo systemctl restart apache2

結束執行個體,並在代管執行個體群組中,針對另一個執行個體重複相同的步驟。

6. 建立生產端服務

接下來,我們要為服務建立負載平衡器元件。

建立負載平衡器健康狀態檢查。

透過 Cloud Shell

gcloud compute health-checks create https service-lb-healthcheck \
    --port=443 \
    --region=$region

建立後端服務。

透過 Cloud Shell

 gcloud compute backend-services create psc-backend-service \
    --load-balancing-scheme=internal \
    --protocol=TCP \
    --region=$region \
    --health-checks=service-lb-healthcheck \
    --health-checks-region=$region

gcloud compute backend-services add-backend psc-backend-service \
--region=$region \
--instance-group=psc-service-mig

建立轉送規則。請注意,轉送規則必須透過通訊埠 443 和全域存取權設定。必須符合上述條件,這個 PSC 模式才能正常運作。

透過 Cloud Shell

 gcloud compute forwarding-rules create producer-fr \
    --region=$region \
    --load-balancing-scheme=internal \
    --network=producer-vpc \
    --subnet=service-subnet \
    --address=10.0.0.100 \
    --ip-protocol=TCP \
    --ports=443 \
    --backend-service=psc-backend-service \
    --backend-service-region=$region \
    --allow-global-access

7. 測試服務

建立服務連結之前,我們會在其他區域中建立用戶端,用來測試已設定全域存取權的負載平衡器,以及設為終止 TLS 的 Apache 服務。

透過 Cloud Shell

gcloud compute instances create vm-client \
    --zone=us-east4-a \
    --image-family=debian-10 \
    --image-project=debian-cloud \
    --subnet=client-subnet \
    --no-address

透過 SSH 登入執行個體。

透過 Cloud Shell

gcloud compute ssh \
    --zone "us-east4-a" "vm-client" \
    --tunnel-through-iap \
    --project $project

透過負載平衡器連線至 443,測試 Apache 服務。

curl https://example.com:443 -k --connect-to example.com:443:10.0.0.100:443

預期結果

PSC Service

8. 建立服務連結

透過 Cloud Shell

gcloud compute service-attachments create pscservice \
    --region=$region \
    --producer-forwarding-rule=producer-fr \
    --connection-preference=ACCEPT-AUTOMATIC \
    --nat-subnets=producer-nat-subnet

請務必記下服務連結 URI,因為在下個步驟中,您會在端點設定中使用。如要取得該名稱,請在 Cloud Shell 中執行下列指令

透過 Cloud Shell

gcloud compute service-attachments describe pscservice --region $region

複製 /projects 開頭的 URI

範例: /projects/<YOUR_PROJECT_ID>/regions/us-central1/serviceAttachments/pscservice

9. 用戶端虛擬私有雲與子網路設定

虛擬私有雲網路

透過 Cloud Shell

gcloud compute networks create consumer-vpc --subnet-mode custom

建立子網路

在要部署 Private Service Connect 網路端點群組 (NEG) 的用戶端上必須提供子網路。

透過 Cloud Shell

gcloud compute networks subnets create psc-neg-subnet \
--network=consumer-vpc \
--region=$region \
--range=10.100.200.0/24 \
--purpose=private

10. 建立 Private Service Connect 端點和測試連線

我們會按照程序建立與剛剛建立的服務連結建立關聯的 PSC NEG、將 PSC NEG 附加至後端服務,並將後端服務與轉送規則建立關聯。

準備好我們在上個步驟記下的服務連結 URI。將下方網址替換為您的 URI。

透過 Cloud Shell

gcloud beta compute network-endpoint-groups create xlb-psc-neg \
--network-endpoint-type=private-service-connect \
--psc-target-service=projects/<PROJECT-ID>/regions/us-central1/serviceAttachments/pscservice \
--region=$region \
--network=consumer-vpc \
--subnet=psc-neg-subnet

建立 XLB 公開 IP 位址,然後擷取指派的實際 IP 位址,以供日後測試使用。

透過 Cloud Shell

gcloud compute addresses create xlb-psc-address \
--ip-version=IPv4 --global

gcloud compute addresses describe xlb-psc-address --format="get(address)" --global

接下來,我們要在外部負載平衡器中建立 PSC 端點。在本例中,

透過 Cloud Shell

gcloud beta compute backend-services create pscneg-backend-service \
--load-balancing-scheme=EXTERNAL_MANAGED \
--protocol=HTTPS \
--global

透過 Cloud Shell

gcloud beta compute backend-services add-backend pscneg-backend-service \
--network-endpoint-group=xlb-psc-neg \
--network-endpoint-group-region=$region \
--global

透過 Cloud Shell

gcloud beta compute url-maps create xlb-psc-map \
--default-service=pscneg-backend-service \
--global

透過 Cloud Shell

gcloud beta compute target-http-proxies create psc-http-proxy \
--url-map=xlb-psc-map

透過 Cloud Shell

gcloud beta compute forwarding-rules create xlb-psc-fr \
--load-balancing-scheme=EXTERNAL_MANAGED \
--network-tier=PREMIUM \
--address=xlb-psc-address \
--target-http-proxy=psc-http-proxy \
--ports=80 \
--global

請等待 5 至 7 分鐘,然後在瀏覽器的網址列輸入與 xlb-psc-address 相關聯的 IP 位址。

如果「PSC 服務」顯示後,就表示您已正確設定解決方案。

11. 清除步驟

從單一 Cloud Shell 終端機刪除研究室元件

gcloud beta compute forwarding-rules delete xlb-psc-fr --global --quiet

gcloud beta compute target-http-proxies delete psc-http-proxy --quiet

gcloud beta compute url-maps delete xlb-psc-map --global --quiet

gcloud beta compute backend-services delete pscneg-backend-service --global --quiet

gcloud compute addresses delete xlb-psc-address --global --quiet

gcloud beta compute network-endpoint-groups delete xlb-psc-neg --region $region --quiet

gcloud compute networks subnets delete psc-neg-subnet --region $region --quiet

gcloud compute networks delete consumer-vpc --quiet

gcloud compute service-attachments delete pscservice --region $region --quiet

gcloud compute instances delete vm-client --zone=us-east4-a --quiet

gcloud compute forwarding-rules delete producer-fr --region $region --quiet

gcloud compute backend-services delete psc-backend-service --region $region --quiet

gcloud compute health-checks delete service-lb-healthcheck --region $region --quiet

gcloud compute instance-groups managed delete psc-service-mig --region $region --quiet

gcloud compute health-checks delete psc-service-mig-healthcheck --region $region --quiet

gcloud compute instance-templates delete producer-service-template --quiet

gcloud compute firewall-rules delete allow-xlb-client --quiet

gcloud compute firewall-rules delete allow-ssh-iap --quiet

gcloud compute routers nats delete service-nat-gw –router service-cr --region $region --quiet

gcloud compute routers delete service-cr --region $region --quiet

gcloud compute networks subnets delete client-subnet --quiet

gcloud compute networks subnets delete service-subnet --quiet

gcloud compute networks subnets delete producer-nat-subnet --quiet

gcloud compute networks delete producer-vpc --quiet

12. 恭喜!

恭喜您完成本程式碼研究室。

涵蓋內容

  • 使用全域 XLB 建立採用消費者 HTTP(S) Service Controls 的 Private Service Connect 端點
  • 將代管服務設為透過服務連結公開,以接受 L7 XLB 連線。
  • 建立 SSL 憑證並設定 Apache 網路伺服器,以終止 TLS,並接受通訊埠 443 的流量。
  • 建立 PSC NEG。