GCP Kubernetes 實踐指南

GCP Kubernetes 實踐指南

參考至:google 線上lab 課程

目錄

  1. 專案配置
  2. GKE 集群管理
  3. Docker 鏡像構建
  4. Kubernetes 部署
  5. 應用更新與滾動升級
  6. 資源清理

專案配置

設置 GCP 專案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 列出所有可用的專案
gcloud projects list

# 設置當前工作的專案
gcloud config set project [PROJECT_ID]

# 查看當前專案配置
gcloud config list

# 設置預設的計算區域
gcloud config set compute/zone us-central1-a

# 查看特定配置
gcloud config get-value project

延伸用法

1
2
3
4
5
6
7
8
9
10
11
# 設置多個配置值
gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1

# 使用配置 profiles(在不同環境間切換)
gcloud config configurations create production
gcloud config configurations create development
gcloud config configurations activate production

# 列出所有配置 profiles
gcloud config configurations list

GKE 集群管理

創建集群

1
2
3
4
5
6
7
8
9
# 基本創建語法
gcloud container clusters create [CLUSTER_NAME] \
--num-nodes [NUMBER] \
--zone [ZONE]

# 實踐例子
gcloud container clusters create fancy-cluster \
--num-nodes 3 \
--zone us-central1-a

常用參數詳解

參數 說明 例子
--num-nodes 初始節點數量 --num-nodes 3
--machine-type 機器類型 --machine-type e2-medium
--zone 部署區域 --zone us-central1-a
--region 區域範圍 --region us-central1
--enable-autoscaling 啟用自動擴展 無值參數
--min-nodes 最小節點數 --min-nodes 1
--max-nodes 最大節點數 --max-nodes 10

進階創建示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 創建具有自動擴展的集群
gcloud container clusters create fancy-cluster \
--num-nodes 3 \
--zone us-central1-a \
--machine-type e2-medium \
--enable-autoscaling \
--min-nodes 1 \
--max-nodes 10

# 創建高可用性集群(跨區域)
gcloud container clusters create fancy-cluster \
--region us-central1 \
--num-nodes 3 \
--enable-autoscaling \
--min-nodes 1 \
--max-nodes 5

集群管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 列出所有集群
gcloud container clusters list

# 獲取集群詳細信息
gcloud container clusters describe fancy-cluster --zone us-central1-a

# 調整節點數量
gcloud container clusters resize fancy-cluster --num-nodes 5 --zone us-central1-a

# 升級集群版本
gcloud container clusters upgrade fancy-cluster --zone us-central1-a

# 刪除集群
gcloud container clusters delete fancy-cluster --zone us-central1-a --quiet

Docker 鏡像構建

啟用 Cloud Build 服務

1
2
3
4
5
# 啟用 Cloud Build API
gcloud services enable cloudbuild.googleapis.com

# 查看已啟用的服務
gcloud services list --enabled

構建和推送鏡像

1
2
3
4
5
6
7
8
9
10
# 基本構建語法
gcloud builds submit --tag gcr.io/[PROJECT_ID]/[IMAGE_NAME]:[TAG] [SOURCE_DIR]

# 實踐例子 - 構建版本 1.0.0
gcloud builds submit \
--tag gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0 .

# 構建版本 2.0.0
gcloud builds submit \
--tag gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0 .

進階 Cloud Build 用法

1
2
3
4
5
6
7
8
9
10
11
12
13
# 指定 Dockerfile 位置
gcloud builds submit \
--tag gcr.io/${GOOGLE_CLOUD_PROJECT}/myapp:latest \
--config cloudbuild.yaml .

# 查看構建歷史
gcloud builds list

# 查看特定構建的詳細日誌
gcloud builds log [BUILD_ID]

# 取消正在進行的構建
gcloud builds cancel [BUILD_ID]

鏡像管理

1
2
3
4
5
6
7
8
9
10
11
# 列出所有鏡像
gcloud container images list

# 列出特定鏡像的所有版本
gcloud container images list-tags gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith

# 刪除特定版本的鏡像
gcloud container images delete gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0 --quiet

# 刪除所有未標記的鏡像
gcloud container images delete gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith --quiet

Kubernetes 部署

基本部署命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 獲取 kubeconfig 認證信息
gcloud container clusters get-credentials fancy-cluster --zone us-central1-a

# 創建部署
kubectl create deployment monolith \
--image=gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0

# 列出所有資源
kubectl get all

# 查看 pods
kubectl get pods

# 查看詳細的 pod 信息
kubectl get pods -o wide

# 查看特定 pod 的日誌
kubectl logs [POD_NAME]

# 實時查看 pod 日誌
kubectl logs -f [POD_NAME]

暴露服務

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 創建 LoadBalancer 服務
kubectl expose deployment monolith \
--type=LoadBalancer \
--port 80 \
--target-port 8080

# 查看服務
kubectl get service

# 查看服務詳細信息
kubectl get service -o wide

# 刪除服務
kubectl delete service monolith

Service 類型對比

類型 說明 用途
ClusterIP 默認,僅在集群內部訪問 內部通訊
NodePort 通過節點端口訪問 開發測試
LoadBalancer 使用雲提供商的負載均衡器 外部訪問
ExternalName 映射到外部 DNS 名稱 外部服務集成

進階 Service 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 創建 NodePort 服務
kubectl expose deployment monolith \
--type=NodePort \
--port 8080

# 創建帶有標籤的 deployment
kubectl create deployment myapp \
--image=gcr.io/project/myapp:1.0.0 \
--labels=app=myapp,version=v1

# 使用標籤選擇器
kubectl get pods -l app=monolith

# 查看服務的詳細配置
kubectl describe service monolith

應用更新與滾動升級

擴展部署副本

1
2
3
4
5
6
7
8
9
10
11
# 擴展到 3 個副本
kubectl scale deployment monolith --replicas=3

# 查看擴展狀態
kubectl get pods

# 查看部署詳細信息
kubectl get deployment -o wide

# 等待所有 pods 準備就緒
kubectl rollout status deployment/monolith

滾動升級

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 更新部署的鏡像
kubectl set image deployment/monolith \
monolith=gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0

# 查看更新進度
kubectl rollout status deployment/monolith

# 查看部署的修訂歷史
kubectl rollout history deployment/monolith

# 回滾到上一個版本
kubectl rollout undo deployment/monolith

# 回滾到特定版本
kubectl rollout undo deployment/monolith --to-revision=1

進階更新策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 創建 deployment YAML 配置
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: monolith
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: monolith
template:
metadata:
labels:
app: monolith
spec:
containers:
- name: monolith
image: gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0
ports:
- containerPort: 8080
EOF

# 查看部署配置
kubectl get deployment monolith -o yaml

# 編輯部署配置
kubectl edit deployment monolith

# 打補丁更新部署
kubectl patch deployment monolith \
-p '{"spec":{"template":{"spec":{"containers":[{"name":"monolith","image":"gcr.io/PROJECT/monolith:3.0.0"}]}}}}'

資源清理

刪除 Kubernetes 資源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 刪除服務
kubectl delete service monolith

# 刪除部署
kubectl delete deployment monolith

# 刪除 pod
kubectl delete pod [POD_NAME]

# 刪除所有資源
kubectl delete all --all

# 使用標籤選擇器刪除
kubectl delete pods -l app=monolith

清理容器鏡像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 刪除單個鏡像版本
gcloud container images delete gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0 --quiet

# 刪除多個版本
gcloud container images delete \
gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0 \
gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0 \
--quiet

# 使用腳本刪除所有版本
gcloud container images list-tags gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith \
--format="get(digest)" | while read digest; do
gcloud container images delete \
"gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith@${digest}" --quiet
done

清理 Cloud Build 源存檔

1
2
3
4
5
6
7
8
9
10
# 列出所有源存檔
gsutil ls gs://${GOOGLE_CLOUD_PROJECT}_cloudbuild/source/

# 刪除特定源存檔
gsutil rm gs://bucket-name/source/file.tgz

# 使用腳本批量刪除
gcloud builds list | grep 'SOURCE' | cut -d ' ' -f2 | while read line; do
gsutil rm $line
done

刪除 GKE 集群

1
2
3
4
5
# 刪除集群
gcloud container clusters delete fancy-cluster --zone us-central1-a --quiet

# 刪除區域集群
gcloud container clusters delete fancy-cluster --region us-central1 --quiet

實用技巧

環境變量使用

1
2
3
4
5
6
7
8
# 設置環境變量以簡化命令
export PROJECT_ID=$(gcloud config get-value project)
export ZONE=us-central1-a
export CLUSTER_NAME=fancy-cluster

# 使用環境變量
gcloud container clusters create $CLUSTER_NAME --num-nodes 3 --zone $ZONE
gcloud builds submit --tag gcr.io/${PROJECT_ID}/myapp:1.0.0 .

常用別名設置

1
2
3
4
5
6
7
8
9
# 在 ~/.bashrc 或 ~/.zshrc 中添加
alias k=kubectl
alias gke='gcloud container'
alias gcr='gcloud container images'

# 使用別名
k get pods
gke clusters list
gcr list

故障排查命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看 pod 詳細信息
kubectl describe pod [POD_NAME]

# 進入 pod 容器執行命令
kubectl exec -it [POD_NAME] -- /bin/bash

# 查看 pod 事件
kubectl get events --sort-by='.lastTimestamp'

# 檢查資源使用情況
kubectl top nodes
kubectl top pods

# 查看部署狀態
kubectl describe deployment monolith

參考資源


GCP Kubernetes 實踐指南
https://shengshengyang.github.io/2025/10/17/gke-using/
作者
Dean Yang
發布於
2025年10月17日
許可協議