在 Google Cloud 控制台中建立叢集及部署工作負載


Kubernetes 叢集可為應用程式提供運算、儲存空間、網路和其他服務,類似於虛擬資料中心。在 Kubernetes 中執行的應用程式及其相關服務稱為「工作負載」

本教學課程會使用 Google Cloud 控制台,快速設定並執行 Google Kubernetes Engine 叢集和範例工作負載。接著,您可以在 Google Cloud 控制台中探索工作負載,然後繼續深入學習,或是開始規劃及建立自己的正式叢集。

如要使用 Terraform 設定範例叢集和工作負載,請參閱「使用 Terraform 建立叢集」。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

請依照下列步驟啟用 Kubernetes Engine API:
  1. 前往 Google Cloud 控制台的 Kubernetes Engine 頁面
  2. 建立或選取專案。
  3. 等待 API 和相關服務完成啟用。 這可能需要幾分鐘的時間。
  4. Make sure that billing is enabled for your Google Cloud project.

必要的角色

Make sure that you have the following role or roles on the project: Compute Admin, Kubernetes Engine Admin, Service Account User

Check for the roles

  1. In the Google Cloud console, go to the IAM page.

    Go to IAM
  2. Select the project.
  3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

  4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

Grant the roles

  1. In the Google Cloud console, go to the IAM page.

    前往「IAM」頁面
  2. 選取專案。
  3. 按一下「授予存取權」
  4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

  5. 在「Select a role」(選取角色) 清單中,選取角色。
  6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
  7. 按一下 [Save]

在 GKE Autopilot 模式中建立叢集

在 Autopilot 模式中,Google 會管理您的叢集設定,包括資源調度、安全性及其他預設設定。Autopilot 模式的叢集已經過最佳化,可執行大部分的實際工作負載,並根據您的 Kubernetes 資訊清單佈建運算資源。

  1. 在 Google Cloud 控制台中,前往 GKE 的「Create an Autopilot cluster」(建立 Autopilot 叢集) 頁面。

    前往「Create an Autopilot cluster」(建立 Autopilot 叢集) 頁面

  2. 在「Cluster basics」(叢集基本資訊) 下方,執行下列操作:

    1. 在「Name」(名稱) 欄位中輸入下列名稱:

      hello-world-cluster
      
    2. 保留其餘設定的預設值,然後按一下「Create」(建立),開始建立叢集。

  3. 重新導向到「Kubernetes clusters」(Kubernetes 叢集) 頁面時,按一下「Name」(名稱) 欄中的「hello-world-cluster」

    您可以觀察叢集設定、部署及驗證的進度。

  4. 等待「hello-world-cluster」頁面標題旁邊顯示勾號。

將範例應用程式部署至叢集

部署 Google 提供的範例「hello world」網頁應用程式,並儲存為 Artifact Registry 中的容器。

  1. 前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

    前往「Workloads」(工作負載)

  2. 按一下「Deploy」(部署)

  3. 在「Deployment name」(Deployment 名稱) 中輸入下列名稱:

    hello-world-app
    
  4. 「Kubernetes Cluster」(Kubernetes 叢集) 中,選取「hello-world-cluster」

  5. 按一下「Next: Container details」(下一步:容器詳細資料)

  6. 保持選取「Existing container image」(現有容器映像檔),並在「Image path」(映像檔路徑) 中輸入下列路徑:

    us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
    

    這個簡單的「hello world」應用程式會封裝為單一容器,但大型應用程式通常包含數個相關容器,這些容器可一起部署,並以單一工作負載的形式執行。

  7. 按一下「Next: Expose (optional)」[下一步:公開 (選用)]

  8. 在「Expose」(公開) 區段中建立負載平衡 Kubernetes Service,將外部要求導向您的應用程式:

    1. 選取「Expose deployment as a new service」(以新 Service 的形式公開 Deployment)。

    2. 將「Port 1」(通訊埠 1) 的設定保留為「80」

    3. 在「Target port 1」(目標通訊埠 1) 中輸入「8080」

    4. 按一下 [Deploy] (部署)

    GKE 會自動將可用的外部 IP 位址指派給 Service。

    這項 Service 屬於 hello-world-app 工作負載的一部分。

  9. 如果是 Autopilot 叢集,可能會看到錯誤訊息,如 Does not have minimum availability。這是因為 Autopilot 會先刪除節點,再重新建立。請稍候幾分鐘,然後按一下「Refresh」(重新整理),以便更新頁面。

  10. 請等待部署作業完成,然後您會看見「Deployment details」(Deployment 詳細資料) 頁面。

在瀏覽器中查看現場示範

  1. 在 Google Cloud 控制台中,前往 hello-world-app 的「Deployment details」(Deployment 詳細資料) 頁面:

    1. 前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。

      前往「Workloads」(工作負載)

    2. 在「Name」(名稱) 欄中,按一下您部署的工作負載名稱 (hello-world-app)

  2. 「Endpoints」(端點) 欄中,按一下可公開取得的 IP 位址。

    GKE 會開啟新的瀏覽器分頁,並傳送要求至應用程式。關閉所有安全網站的警告,而新的瀏覽器分頁應該會顯示「Hello, world!」

    如果「Endpoints」(端點) 為空白,表示貴機構可能設有限制外部存取的政策。

您已成功在 Autopilot 模式中建立 GKE 叢集,並部署範例工作負載。

清除所用資源,以免產生帳單費用

如果您打算參加其他教學課程或進一步探索範例,請先完成這些作業,再執行這個清除步驟。大部分 GKE 教學課程都可以繼續使用 Kubernetes 範例叢集。

如果您建立新專案是為了瞭解如何使用 GKE,且現在已不再需要該項專案,請刪除專案

如果您使用的是現有的 GKE 專案,請刪除稍早建立的資源,以免系統向您的帳戶收取費用:

  1. 前往 GKE「Clusters」(叢集) 頁面。

    前往「Clusters」(叢集)

  2. 選取含有「hello-world-cluster」的資料列,然後按一下「Delete」(刪除)

  3. 在「Delete hello-world-cluster」(刪除 hello-world-cluster) 視窗中,執行下列操作:

    1. 在「hello-world-cluster」欄位中輸入 hello-world-cluster

    2. 按一下「Delete」(刪除)。

    如果收到正在修復叢集的錯誤訊息,請等待程序完成,再刪除叢集。這項作業可能需要一段時間才能完成。

  4. 如果您按照其他教學課程的操作說明,建立了記錄檔接收器和 bucket:

    1. 前往 Cloud Logging 的「Logs storage」(記錄檔儲存空間) 頁面。

      前往「Logs storage」(記錄檔儲存空間)

    2. 選取「hello-world-cluster-bucket」,然後按一下「Delete」(刪除)

    3. 前往 Logging「Log router」(記錄檔路由器) 頁面。

      前往「Log router」(記錄檔路由器)

    4. 選取「hello-world-cluster-sink」,然後按一下「Delete」(刪除)

後續步驟