將應用程式部署至 GKE 叢集


本快速入門導覽課程說明如何將範例網頁伺服器容器化應用程式部署至 Google Kubernetes Engine (GKE) 叢集。您將瞭解如何建立叢集,以及如何將應用程式部署至叢集,供使用者存取。

本頁適用於佈建及設定雲端資源,並部署應用程式和服務的營運人員和開發人員。如要進一步瞭解內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud

閱讀本頁面之前,請先熟悉 Kubernetes

事前準備

請依照下列步驟啟用 Kubernetes Engine API:
  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry and Google Kubernetes Engine APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Artifact Registry and Google Kubernetes Engine APIs.

    Enable the APIs

  8. 必要的角色

    Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/container.admin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.

啟動 Cloud Shell

在本教學課程中,您將使用 Cloud Shell,這是用來管理Google Cloud上託管資源的殼層環境。

Cloud Shell 已預先安裝 Google Cloud CLIkubectl 指令列工具。gcloud CLI 是 Google Cloud的主要指令列介面,而 kubectl 是用於執行 Kubernetes 叢集相關指令的主要指令列介面。

啟動 Cloud Shell:

  1. 前往 Google Cloud 控制台。

    Google Cloud console

  2. 按一下主控台右上角的「啟用 Cloud Shell」按鈕:

此時 Cloud Shell 工作階段會在主控台底部的頁框中開啟,您可以使用這個殼層來執行 gcloudkubectl 指令。執行指令前,請先使用下列指令,在 Google Cloud CLI 中設定預設專案:

gcloud config set project PROJECT_ID

PROJECT_ID 替換為專案 ID

建立 GKE 叢集

「叢集」是由至少一個「叢集控制層」機器,以及稱為「節點」的多個工作站機器所組成的。節點就是 Compute Engine 虛擬機器 (VM) 執行個體,且該執行個體必須執行會讓自己加入叢集的 Kubernetes 程序。當您將應用程式部署至叢集之後,該應用程式就會在節點上執行。

本節說明如何以 Autopilot 模式建立叢集。對於大多數實際工作環境用途,建議使用這種叢集模式。您也可以使用 GKE Standard 模式叢集執行這些步驟。

建立名為 hello-cluster 的 Autopilot 叢集:

gcloud container clusters create-auto hello-cluster \
    --location=us-central1

叢集會在幾分鐘內建立完畢。

取得叢集的驗證憑證

建立叢集後,您需要取得驗證憑證才能與叢集互動:

gcloud container clusters get-credentials hello-cluster \
    --location us-central1

這個指令會設定 kubectl 來使用您建立的叢集。

將應用程式部署至叢集

既然您已經建立叢集,那就能在這個叢集部署容器化的應用程式了。而在本快速入門導覽課程中,您可以部署網頁應用程式範例:hello-app

GKE 使用 Kubernetes 物件建立並管理您的叢集資源。Kubernetes 提供 Deployment 物件來部署無狀態應用程式,例如網路伺服器。Service 物件會定義要從網際網路存取應用程式時的規則和負載平衡。

建立 Deployment

如要在叢集中執行 hello-app,請執行下列指令來部署應用程式:

kubectl create deployment hello-server \
    --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0

這個 Kubernetes 指令 (kubectl create deployment) 會建立名為 hello-server 的 Deployment。Deployment 的 Pod 會執行 hello-app 容器映像檔。

在這個指令中:

  • --image 會指定要部署的容器映像檔。在本例中,指令會從 Artifact Registry 存放區 us-docker.pkg.dev/google-samples/containers/gke/hello-app 提取範例映像檔。:1.0 會指示要提取的特定映像檔版本。如未指定版本,系統會使用具有預設 latest 標記的圖片。

公開 Deployment

當您部署應用程式之後,就必須對網際網路公開該應用程式,好讓使用者能夠存取。您可以建立 Service 來公開應用程式;Service 就是能對外部流量公開應用程式的 Kubernetes 資源。

如要公開應用程式,請執行下列 kubectl expose 指令:

kubectl expose deployment hello-server \
    --type LoadBalancer \
    --port 80 \
    --target-port 8080

傳入 --type LoadBalancer 標記會為容器建立 Compute Engine 負載平衡器。--port 標記會對網際網路啟用公開通訊埠 80,而 --target-port 標記則會將流量導向應用程式的通訊埠 8080。

負載平衡器是依照每個 Compute Engine 的負載平衡器計價方式來計費的。

檢查及查看應用程式

  1. 使用 kubectl get pods 檢查執行中的 Pod:

    kubectl get pods
    

    您應該會看到在叢集執行的一個 hello-server Pod。

  2. 使用 kubectl get service 來檢查 hello-server Service:

    kubectl get service hello-server
    

    請在上述指令的輸出內容中,複製 EXTERNAL-IP 欄的 Service 外部 IP 位址。

  3. 在網路瀏覽器上利用外部 IP 位址與公開的通訊埠來查看應用程式:

    http://EXTERNAL_IP
    

您已經將容器化的網頁應用程式部署到 GKE 了!

如要以應用程式為中心追蹤及整理 GKE 資源,請將資源新增為App Hub 應用程式的服務和工作負載。

如要進一步瞭解 App Hub 支援的資源,請參閱「支援的資源」。

如要瞭解如何在專案中設定 App Hub,請參閱「設定 App Hub」。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。

  1. 執行 kubectl delete,以便刪除應用程式的 Service:

    kubectl delete service hello-server
    

    這個指令會刪除您在公開 Deployment 時所建立的 Compute Engine 負載平衡器。

  2. 執行 gcloud container clusters delete,以便刪除叢集:

    gcloud container clusters delete hello-cluster \
        --location us-central1
    

選用:hello-app 程式碼審查

hello-app 是由兩個檔案組成的網路伺服器應用程式:main.goDockerfile

hello-app 被封裝為 Docker 容器映像檔。容器映像檔可儲存在任何 Docker 映像檔註冊資料庫中,例如 Artifact Registry。我們把 hello-app 託管給 us-docker.pkg.dev/google-samples/containers/gke/hello-app 的 Artifact Registry 存放區。

main.go

main.go 是以 Go 程式設計語言編寫的網路伺服器實作。該伺服器會用「Hello, world!」訊息回應所有 HTTP 要求。

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
	// register hello function to handle all requests
	mux := http.NewServeMux()
	mux.HandleFunc("/", hello)

	// use PORT environment variable, or default to 8080
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	// start the web server on port and accept requests
	log.Printf("Server listening on port %s", port)
	log.Fatal(http.ListenAndServe(":"+port, mux))
}

// hello responds to the request with a plain-text "Hello, world" message.
func hello(w http.ResponseWriter, r *http.Request) {
	log.Printf("Serving request: %s", r.URL.Path)
	host, _ := os.Hostname()
	fmt.Fprintf(w, "Hello, world!\n")
	fmt.Fprintf(w, "Version: 1.0.0\n")
	fmt.Fprintf(w, "Hostname: %s\n", host)
}

Dockerfile

Dockerfile 會說明您希望 Docker 建構的映像檔 (包括該映像檔的所有資源和依附元件),以及指定要讓應用程式對哪個網路通訊埠公開。如要進一步瞭解該檔案的運作方式,請參閱 Docker 說明文件中的 Dockerfile 參考資料

FROM golang:1.24.3 as builder
WORKDIR /app
RUN go mod init hello-app
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /hello-app

FROM gcr.io/distroless/base-debian11
WORKDIR /
COPY --from=builder /hello-app /hello-app
ENV PORT 8080
USER nonroot:nonroot
CMD ["/hello-app"]

後續步驟

歡迎試用

如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用 GKE 的成效。新客戶可以獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。

免費試用 GKE