本頁面說明如何透過 Google Kubernetes Engine (GKE) 設定 Service Directory,並自動向 Service Directory 註冊 GKE 叢集。您可以透過 Autopilot GKE 叢集或 Standard GKE 叢集,設定 Service Directory for GKE。如要瞭解這兩種模式,請參閱「選擇 GKE 作業模式」。
如要進一步瞭解 GKE 適用的 Service Directory,請參閱「GKE 適用的 Service Directory 總覽」。
限制
Service Directory for GKE 有以下限制:
- 您只能透過 Service Directory-GKE 整合功能註冊服務。您必須使用 Service Directory API 手動註冊 Ingress、閘道和其他資源。
- 您必須先使用機群註冊 GKE 叢集,才能將這些叢集中的服務同步至 Service Directory。如要瞭解如何註冊 GKE 叢集,請參閱「註冊叢集」。
- 所有 Service Directory 資源都會註冊至機群專案,而非叢集專案。
- 您無法修改透過這項整合功能註冊的 Service Directory 實體。您必須對 GKE 服務資源本身進行所有變更,才能將變更傳播至服務目錄。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- 您必須將 GKE 叢集註冊至機群。 如要註冊 GKE 叢集,請按照「註冊叢集」一文中的說明操作。
設定 GKE 叢集
如要設定 GKE 叢集,請按照下列步驟操作。
- 啟用 Service Directory GKE 機群功能。
- 設定 Service Directory 註冊政策。
- 部署 GKE 服務。
啟用 Service Directory GKE 機群功能
如要啟用 Service Directory GKE 機群功能,請執行 gcloud container hub 指令。
gcloud
gcloud alpha container hub service-directory enable
為服務設定 Service Directory 註冊政策
註冊政策會定義 Service Directory 自動匯入的 GKE 服務類型。下表列出這項政策的可設定選項:
resources:要匯入的 GKE 資源類型。您可以指定多個資源類型。資源由Group和Kind識別。Group的預設值為core。僅提供Kind: service。selector:GKE 標籤選取器,可比對套用至 GKE 資源的 key:value 標籤。根據預設,系統不會選取任何資源。annotationsToSync:資源上要同步至服務目錄的註解鍵。如果這份清單沒有內容,資源就會同步至 Service Directory,但不會加上註解。
每個命名空間應定義單一政策。這項政策必須有 name:
"default"。控制者不允許或調解其他名稱。
設定「ServiceDirectoryRegistrationPolicy」。
下列資訊清單說明 ServiceDirectoryRegistrationPolicy:
apiVersion: networking.gke.io/v1alpha1 kind: ServiceDirectoryRegistrationPolicy metadata: # Only the name "default" is allowed. name: default # The ServiceDirectoryRegistrationPolicy is a namespaced resource namespace: NAMESPACE spec: resources: # Kind specifies the types of Kubernetes resources that can be synced into Service Directory. - kind: Service # Selector is a label selector for the resource types specified in Kind. selector: matchLabels: LABELS # annotationsToSync specifies the annotations that are matched and imported. # Any annotations that do not match this set of keys will not be imported into Service Directory. annotationsToSync: - ANNOTATIONS
更改下列內容:
NAMESPACE:命名空間的名稱。命名空間中只能有一個ServiceDirectoryRegistrationPolicy。如果單一命名空間有多項政策,GKE 會選擇其中一項政策做為有效政策,並將其他政策標示為無效。LABELS:標籤選取器,例如sd-import: "true"。這個值適用於種類中指定的資源。根據預設,系統不會選取任何資源。ANNOTATIONS:相符並匯入的註解清單,例如- cloud.google.com/load-balancer-type。如果所選 GKE 資源具有清單中的其中一個註解,系統會將該註解及其值與資源同步至 Service Directory。如果這份清單沒有內容,資源就會同步至 Service Directory,但不會加上註解。
將資訊清單儲存為 registration_policy.yaml。
將資訊清單套用至叢集
如要將資訊清單套用至叢集,請執行 kubectl apply -f 指令:
kubectl apply -f registration_policy.yaml
部署 GKE 服務
在您所在區域的 GKE 叢集中部署 GKE 外部直通式網路負載平衡器服務和應用程式。舉例來說,如果您的區域是 us-central1,請在 GKE 叢集 gke-us-central1 中部署。
這會將 GKE 服務匯入 Service Directory。
建立 YAML 檔案並套用,就像其他 GKE 資源一樣。以下是 LoadBalancer 類型服務的資訊清單:
apiVersion: v1
kind: Service
metadata:
name: hello-app-service
labels:
sd-import: "true"
annotations:
cloud.google.com/load-balancer-type: "Internal"
spec:
type: LoadBalancer
selector:
app: hello
ports:
- name: tcp-port
protocol: TCP
port: 80
targetPort: 8080
以下是 Deployment 的資訊清單:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: hello
name: hello-app
spec:
replicas: 2
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- image: gcr.io/google-samples/hello-app:1.0
name: hello
ports:
- containerPort: 8080
protocol: TCP
如要驗證,請使用 gcloud service-directory services resolve 指令,對您的服務執行 Service Directory 查詢:
gcloud
gcloud beta service-directory services resolve SERVICE_NAME \ --location=LOCATION \ --namespace=NAMESPACE
更改下列內容:
SERVICE_NAME:服務的名稱或完整 ID,例如hello-app-serviceLOCATION:服務的區域名稱,例如us-central1NAMESPACE:服務的命名空間名稱,例如default
輸出內容會顯示您的服務,如下所示:
service:
endpoints:
- address: 10.140.0.5
metadata:
cloud.google.com/load-balancer-type: "Internal"
name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app-service/endpoints/my-cluster-12345
port: 80
network: projects/123456789/locations/global/networks/default
name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app-service
帳單
Service Directory 不會對 GKE 整合功能建立的資源收費。不過,API 呼叫會產生費用。如需定價詳情,請參閱 Service Directory 定價。
後續步驟
- 如要瞭解 Service Directory 總覽,請參閱「Service Directory 總覽」。
- 如要瞭解使用 Service Directory 時可能遇到的常見問題解決方案,請參閱這篇文章。