本頁說明如何在 Google Kubernetes Engine (GKE) 上,跨虛擬私有雲網路建立內部直通式網路負載平衡器。
閱讀本頁面之前,請先熟悉下列概念:
事前準備
開始之前,請確認你已完成下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update
,取得最新版本。
使用 Private Service Connect 建立內部負載平衡器
服務生產者可以使用服務連結,透過 Private Service Connect,讓其他虛擬私有雲網路中的服務消費者使用您的服務。您可以使用 ServiceAttachment
自訂資源建立、管理及刪除服務附件。
需求條件和限制
- 適用 Private Service Connect 的限制。
- 您可以在 GKE 1.21.4-gke.300 以上版本中建立服務附件。
- 您無法在多個服務附件設定中使用相同的子網路。
- 您必須建立使用內部直通式網路負載平衡器的 GKE 服務。
- 如果 GKE 版本早於 1.22.4-gke.100,您就無法為不同專案 (共用虛擬私有雲) 指定子網路。如果是共用虛擬私有雲,請確保符合共用虛擬私有雲的所有規定。
- 建立服務附件後,您就無法更新內部負載平衡器。如要變更負載平衡器設定,您必須刪除並重新建立服務附件。
建立 ServiceAttachment
建立子網路。
您必須為每個
ServiceAttachment
建立新的子網路。gcloud beta compute networks subnets create SUBNET_NAME \ --project PROJECT_ID \ --network NETWORK_NAME \ --region REGION \ --range SUBNET_RANGE \ --purpose PRIVATE_SERVICE_CONNECT
更改下列內容:
SUBNET_NAME
:新子網路的名稱。在 GKE 1.22.4-gke.100 以上版本中,您可以使用這個欄位的完整資源網址,在不同專案中指定子網路。您可以使用gcloud compute networks subnets describe
指令取得完整資源網址。PROJECT_ID
:專案的 ID。 Google CloudNETWORK_NAME
:子網路的 VPC 網路名稱。REGION
:新子網路的區域。您必須使用與所建立服務相同的區域。SUBNET_RANGE
:用於子網路的 IP 位址範圍。
部署工作負載。
下列資訊清單說明執行範例網路應用程式容器映像檔的部署。將資訊清單儲存為
my-deployment.yaml
:apiVersion: apps/v1 kind: Deployment metadata: name: psc-ilb spec: replicas: 3 selector: matchLabels: app: psc-ilb template: metadata: labels: app: psc-ilb spec: containers: - name: whereami image: us-docker.pkg.dev/google-samples/containers/gke/whereami:v1 ports: - name: http containerPort: 8080 readinessProbe: httpGet: path: /healthz port: 8080 scheme: HTTP initialDelaySeconds: 5 timeoutSeconds: 1
將資訊清單套用至叢集:
kubectl apply -f my-deployment.yaml
建立服務。 下列資訊清單說明一項服務,該服務會在 TCP 通訊埠 8080 上建立內部直通式網路負載平衡器。將資訊清單儲存為
my-service.yaml
:apiVersion: v1 kind: Service metadata: name: SERVICE_NAME annotations: networking.gke.io/load-balancer-type: "Internal" spec: type: LoadBalancer selector: app: psc-ilb ports: - port: 80 targetPort: 8080 protocol: TCP
更改下列內容:
SERVICE_NAME
:新服務的名稱。
將資訊清單套用至叢集:
kubectl apply -f my-service.yaml
建立
ServiceAttachment
。下列資訊清單說明
ServiceAttachment
,可向服務消費者公開您建立的服務。將資訊清單儲存為my-psc.yaml
:apiVersion: networking.gke.io/v1 kind: ServiceAttachment metadata: name: SERVICE_ATTACHMENT_NAME namespace: default spec: connectionPreference: ACCEPT_AUTOMATIC natSubnets: - SUBNET_NAME proxyProtocol: false resourceRef: kind: Service name: SERVICE_NAME
更改下列內容:
SERVICE_ATTACHMENT_NAME
:新服務附件的名稱。SUBNET_NAME
:新子網路的名稱。在 GKE 1.22.4-gke.100 以上版本中,您可以使用這個欄位的完整資源網址,在不同專案中指定子網路。您可以使用gcloud compute networks subnets describe
指令取得完整資源網址。如為共用虛擬私有雲設定,請使用以下格式:projects/HOST_PROJECT_ID/regions/COMPUTE_REGION/subnetworks/SUBNET_NAME
。
如要進一步瞭解資訊清單欄位,請參閱「服務附件欄位」。
將資訊清單套用至叢集:
kubectl apply -f my-psc.yaml
確認 Private Service Connect 控制器已建立服務連結:
gcloud beta compute service-attachments list
輸出內容會顯示服務連結,以及系統自動產生的名稱:
NAME REGION PRODUCER_FORWARDING_RULE CONNECTION_PREFERENCE k8s1-sa-... REGION_NAME a3fea439c870148bdba5e59c9ea9451a ACCEPT_AUTOMATIC
查看ServiceAttachment
您可以使用下列指令查看 ServiceAttachment
的詳細資料:
kubectl describe serviceattachment SERVICE_ATTACHMENT_NAME
輸出結果會與下列內容相似:
Name: <sa-name>
Namespace: default
Labels: <none>
Annotations: <none>
API Version: networking.gke.io/v1beta1
Kind: ServiceAttachment
Metadata:
...
Status:
Forwarding Rule URL: https://www.googleapis.com/compute/beta/projects/<project>/regions/<region>/forwardingRules/<fr-name>
Last Modified Timestamp: 2021-07-08T01:32:39Z
Service Attachment URL: https://www.googleapis.com/compute/beta/projects/<projects>/regions/<region>/serviceAttachments/<gce-service-attachment-name>
Events: <none>
使用 ServiceAttachment
如要從其他專案使用服務,請按照下列步驟操作:
取得
ServiceAttachment
的網址:kubectl get serviceattachment SERVICE_ATTACHMENT_NAME -o=jsonpath="{.status.serviceAttachmentURL}"
輸出結果會與下列內容相似:
serviceAttachmentURL: https://www.googleapis.com/compute/alpha/projects/<project>/region/<region>/serviceAttachments/k8s1-...my-sa
使用
ServiceAttachment
的網址建立 Private Service Connect 端點。確認您可以使用消費者專案中 VM 的
curl
指令,連線至在生產者專案中部署的服務:curl PSC_IP_ADDRESS
將
PSC_IP_ADDRESS
替換為消費者專案中轉送規則的 IP 位址。輸出結果會與下列內容相似:
{ "cluster_name":"cluster", "host_header":"10.128.15.200", "node_name":"gke-psc-default-pool-be9b6e0e-dvxg.c.gke_project.internal", "pod_name":"foo-7bf648dcfd-l5jf8", "pod_name_emoji":"👚", "project_id":"gke_project", "timestamp":"2021-06-29T21:32:03", "zone":"ZONE_NAME" }
更新 ServiceAttachment
如要更新 ServiceAttachment
,請按照下列步驟操作:
在
my-psc.yaml
中編輯ServiceAttachment
資訊清單:apiVersion: networking.gke.io/v1 kind: ServiceAttachment metadata: name: my-sa namespace: default spec: connectionPreference: ACCEPT_AUTOMATIC natSubnets: - my-nat-subnet proxyProtocol: false resourceRef: kind: Service name: ilb-service
將資訊清單套用至叢集:
kubectl apply -f my-psc.yaml
刪除 ServiceAttachment
您無法刪除已連線至服務連結的內部直通式網路負載平衡器。您必須分別刪除服務連結和 GKE 服務。
刪除服務附件:
kubectl delete serviceattachment SERVICE_ATTACHMENT_NAME --wait=false
這項指令會將服務連結標示為待刪除,但資源仍會存在。您也可以省略
--wait
旗標,等待刪除作業完成。刪除服務:
kubectl delete svc SERVICE_NAME
刪除子網路:
gcloud compute networks subnets delete SUBNET_NAME
ServiceAttachment
個欄位
ServiceAttachment
包含下列欄位:
connectionPreference
:連線偏好設定,決定顧客如何連線至服務。您可以使用ACCEPT_AUTOMATIC
自動核准專案,也可以使用ACCEPT_MANUAL
明確核准專案。詳情請參閱「使用 Private Service Connect 發布服務」。natSubnets
:要用於服務附件的子網路資源名稱清單。proxyProtocol
:設為 true 時,要求中會提供消費者來源 IP 和 Private Service Connect 連線 ID。這是選填欄位,如未提供,預設值為 false。consumerAllowList
:允許連線至ServiceAttachment
的消費者專案清單。只有在connectionPreference
為ACCEPT_MANUAL
時,才能使用此欄位。如要進一步瞭解這個欄位,請參閱「使用 Private Service Connect 發布服務」。project
:消費者專案的 ID 或編號。connectionLimit
:消費者專案的連線限制。這個欄位為選填。forceSendFields
:要傳送的欄位名稱,會納入 API 要求。這是選填欄位。nullFields
:要在 API 要求中加入的欄位名稱,值為空值。這是選填欄位。
consumerRejectList
:禁止連線至ServiceAttachment
的消費者專案 ID 或編號清單。只有在connectionPreference
為ACCEPT_MANUAL
時,才能使用此欄位。如要進一步瞭解這個欄位,請參閱「使用 Private Service Connect 來發布服務」。resourceRef
:Kubernetes 資源的參照。kind
:Kubernetes 資源的類型。必須使用Service
。name
:Kubernetes 資源的名稱,必須與內部直通式網路負載平衡器位於相同命名空間。
疑難排解
您可以使用下列指令查看錯誤訊息:
kubectl get events -n NAMESPACE
將 NAMESPACE
替換為內部直通式網路負載平衡器的命名空間。
如果您嘗試刪除服務附件使用的內部直通網路負載平衡器,就會看到類似以下的錯誤訊息。您必須先刪除「ServiceAttachment
」,才能刪除內部直通式網路負載平衡器。
Error syncing load balancer: failed to ensure load balancer: googleapi:
Error 400: The forwarding_rule resource '<fwd-rule-URL>' is already being used
by '<svc-attachment-URL>', resourceInUseByAnotherResource.