用途
Google Kubernetes Engine(GKE)用于在 Google Cloud Platform 上部署、运维和扩展托管 Kubernetes 集群。适合以下场景:以自动扩缩容和自愈方式大规模运行容器化微服务;需要细粒度编排、服务网格或自定义调度的工作负载;已投入 Kubernetes 工具链(Helm、Argo CD、Flux)的团队;以及 Cloud Run 基于请求的模型不适用时(长时间运行、有状态工作负载)。
前置条件
需安装 Google Cloud SDK(gcloud)与 kubectl;启用 Kubernetes Engine、Compute Engine API;具备 roles/container.admin 角色用于集群管理。
gcloud services enable container.googleapis.com compute.googleapis.com
gcloud components install kubectl
Standard 与 Autopilot 对比
- 节点管理:Standard 由用户管理节点池;Autopilot 由 Google 管理节点。
- 计费:Standard 按节点(VM)付费;Autopilot 按 Pod 资源请求付费。
- GPU/TPU:Standard 完全支持;Autopilot 支持(有限制)。
- DaemonSets:Standard 允许;Autopilot 受限。
- 适用:Standard 适合需要完全控制与专用硬件;Autopilot 适合免运维、成本优化。
主要能力
创建集群
Standard 集群创建示例(含自动扩缩容、自动修复、自动升级、Workload Identity、IP 别名、网络策略、屏蔽节点、日志与监控):
gcloud container clusters create prod-cluster \
--region=us-central1 --num-nodes=2 \
--machine-type=e2-standard-4 --disk-size=100 \
--enable-autoscaling --min-nodes=1 --max-nodes=5 \
--enable-autorepair --enable-autoupgrade \
--release-channel=regular \
--workload-pool=${PROJECT_ID}.svc.id.goog \
--enable-ip-alias --enable-network-policy \
--enable-shielded-nodes \
--logging=SYSTEM,WORKLOAD --monitoring=SYSTEM,WORKLOAD \
--labels=env=production,team=platform
gcloud container clusters get-credentials prod-cluster --region=us-central1
Autopilot 集群创建示例:
gcloud container clusters create-auto autopilot-prod \
--region=us-central1 --release-channel=regular \
--workload-pool=${PROJECT_ID}.svc.id.goog \
--network=my-vpc --subnetwork=gke-subnet
节点池
支持创建高内存池(带污点)、GPU 池、Spot 池(用于批处理工作负载),示例:
# 高内存池
gcloud container node-pools create highmem-pool \
--cluster=prod-cluster --region=us-central1 \
--machine-type=n2-highmem-8 --disk-size=200 --disk-type=pd-ssd \
--num-nodes=1 --enable-autoscaling --min-nodes=0 --max-nodes=4 \
--node-labels=workload=memory-intensive \
--node-taints=dedicated=highmem:NoSchedule
# GPU 池
gcloud container node-pools create gpu-pool \
--cluster=prod-cluster --region=us-central1 \
--machine-type=n1-standard-8 \
--accelerator=type=nvidia-tesla-t4,count=1 \
--num-nodes=0 --enable-autoscaling --min-nodes=0 --max-nodes=4 \
--node-taints=nvidia.com/gpu=present:NoSchedule
# Spot 池
gcloud container node-pools create spot-pool \
--cluster=prod-cluster --region=us-central1 \
--machine-type=e2-standard-4 --spot \
--num-nodes=0 --enable-autoscaling --min-nodes=0 --max-nodes=20 \
--node-taints=cloud.google.com/gke-spot=true:NoSchedule
Workload Identity
通过 GSA(Google 服务账号)与 KSA(Kubernetes 服务账号)绑定实现工作负载身份:
gcloud iam service-accounts create app-gsa
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member="serviceAccount:app-gsa@${PROJECT_ID}.iam.gserviceaccount.com" \
--role="roles/storage.objectViewer"
kubectl create namespace myapp
kubectl create serviceaccount app-ksa --namespace=myapp
gcloud iam service-accounts add-iam-policy-binding \
app-gsa@${PROJECT_ID}.iam.gserviceaccount.com \
--role=roles/iam.workloadIdentityUser \
--member="serviceAccount:${PROJECT_ID}.svc.id.goog[myapp/app-ksa]"
kubectl annotate serviceaccount app-ksa --namespace=myapp \
iam.gke.io/gcp-service-account=app-gsa@${PROJECT_ID}.iam.gserviceaccount.com
部署工作负载
提供 Deployment 与 Service 的 YAML 示例,包含副本数、资源请求与限制、就绪与存活探针、拓扑分布约束(跨可用区)等配置。
托管 SSL 的 Ingress
通过 Ingress 注解使用 GCE 入口类、托管证书与全局静态 IP:
annotations:
kubernetes.io/ingress.class: "gce"
networking.gke.io/managed-certificates: "web-cert"
kubernetes.io/ingress.global-static-ip-name: "web-static-ip"
并创建 ManagedCertificate 资源与全局静态地址:
gcloud compute addresses create web-static-ip --global
Terraform 配置
提供 google_container_cluster、google_container_node_pool、google_compute_subnetwork 等资源示例,涵盖发布通道、Workload Identity、IP 分配策略、私有集群、网络策略、日志监控、自动扩缩容、屏蔽实例配置等。
使用方式
通过 gcloud 命令行创建与管理集群、节点池,使用 kubectl 部署与操作工作负载,或通过 Terraform 以基础设施即代码方式配置。常用操作包括:
gcloud container clusters list
gcloud container clusters upgrade prod-cluster --region=us-central1 --master
kubectl top nodes && kubectl top pods --namespace=myapp
kubectl scale deployment web-app --replicas=5 --namespace=myapp
kubectl autoscale deployment web-app --namespace=myapp --min=3 --max=20 --cpu-percent=70
kubectl logs -f deployment/web-app --namespace=myapp --all-containers
适用场景
大规模容器化微服务、需要服务网格或自定义调度的工作负载、已使用 Kubernetes 工具链的团队、以及不适合 Cloud Run 请求模型的长时运行或有状态工作负载。
限制与故障排查
常见问题及处理:Pod 卡在 Pending(节点资源不足,检查自动扩缩容、增加更大节点池、核对资源请求);ImagePullBackOff(镜像路径错误或缺少 Artifact Registry 访问权限);Workload Identity 账号错误(KSA 注解缺失,重新注解并重启 Pod);节点 NotReady(磁盘/内存压力或网络问题,使用 kubectl describe node 检查);Ingress 返回 502(后端 Pod 健康检查失败,核对就绪探针与 NEG 健康状态);集群创建配额错误(区域 CPU/IP 配额不足,申请配额提升);网络策略不生效(集群未启用,需以 --enable-network-policy 重建或使用 Dataplane V2)。
相关技能
gcp-networking(GKE 集群的 VPC、防火墙规则与负载均衡器)、terraform-gcp(以基础设施即代码配置 GKE 集群)、gcp-compute(工作负载更适合 VM 而非容器时)、gcp-cloud-sql(通过 sidecar 代理连接 GKE Pod 与 Cloud SQL)。