关于 gcp-gke
面向 Google Cloud 上 Kubernetes 运维者的 GKE 技能,提供集群部署、扩缩容和故障排查指导,涵盖 Standard 与 Autopilot 模式。
主要功能
- 01创建 Standard 和 Autopilot 集群
- 02配置节点池与自动扩缩容
- 03设置 Workload Identity
- 04部署工作负载与托管 SSL Ingress
- 05使用 Terraform 配置集群
- 06诊断常见集群问题
适用场景
- 01在 Google Cloud 部署容器化微服务
- 02为不同工作负载配置节点池
- 03管理集群身份与访问
- 04排查 Pod、节点和 Ingress 故障
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill gcp-gke安装后在 Claude Code 中输入/gcp-gke确认已加载。
详细介绍
Google Kubernetes Engine (GKE)
在 Google Cloud Platform 上部署、运维和扩展托管式 Kubernetes 集群。
适用场景
- 通过自动扩缩和自愈,以大规模运行容器化微服务
- 需要精细编排、服务网格或自定义调度的工作负载
- 已在 Kubernetes 工具(Helm、Argo CD、Flux)上投入资源的团队
- Cloud Run 基于请求的模型不适用时(长时间运行、有状态的工作负载)
前提条件
- 已安装 Google Cloud SDK(
gcloud)和kubectl - 已启用 API:Kubernetes Engine、Compute Engine
- 用于集群管理的 IAM 角色
roles/container.admin
gcloud services enable container.googleapis.com compute.googleapis.com
gcloud components install kubectl
Standard 与 Autopilot 对比
| 功能 | Standard | Autopilot |
|---|---|---|
| 节点管理 | 由你管理节点池 | Google 管理节点 |
| 定价 | 按节点(VM)付费 | 按 Pod 资源请求付费 |
| GPU/TPU | 完全支持 | 支持(有限制) |
| DaemonSets | 允许 | 受限 |
| 适用场景 | 完全控制、专用硬件 | 免运维、成本优化 |
创建 Standard 集群
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
节点池
# High-memory pool with taint
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 pool
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 pool for batch workloads
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
# Create GSA and grant permissions
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"
# Create KSA and bind to GSA
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.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: myapp
spec:
replicas: 3
selector:
matchLabels: { app: web-app }
template:
metadata:
labels: { app: web-app }
spec:
serviceAccountName: app-ksa
containers:
- name: web
image: us-central1-docker.pkg.dev/PROJECT_ID/repo/web-app:v1.2.0
ports: [{ containerPort: 8080 }]
resources:
requests: { cpu: 250m, memory: 512Mi }
limits: { cpu: 500m, memory: 1Gi }
readinessProbe:
httpGet: { path: /healthz, port: 8080 }
initialDelaySeconds: 5
livenessProbe:
httpGet: { path: /healthz, port: 8080 }
initialDelaySeconds: 15
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels: { app: web-app }
---
apiVersion: v1
kind: Service
metadata: { name: web-app, namespace: myapp }
spec:
selector: { app: web-app }
ports: [{ port: 80, targetPort: 8080 }]
type: ClusterIP
使用托管 SSL 的 Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
namespace: myapp
annotations:
kubernetes.io/ingress.class: "gce"
networking.gke.io/managed-certificates: "web-cert"
kubernetes.io/ingress.global-static-ip-name: "web-static-ip"
spec:
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: { name: web-app, port: { number: 80 } }
---
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata: { name: web-cert, namespace: myapp }
spec:
domains: [app.example.com]
gcloud compute addresses create web-static-ip --global
Terraform 配置
resource "google_container_cluster" "primary" {
name = "prod-cluster"
location = "us-central1"
release_channel { channel = "REGULAR" }
workload_identity_config { workload_pool = "${var.project_id}.svc.id.goog" }
network = google_compute_network.vpc.name
subnetwork = google_compute_subnetwork.gke.name
ip_allocation_policy {
cluster_secondary_range_name = "pods"
services_secondary_range_name = "services"
}
private_cluster_config {
enable_private_nodes = true
master_ipv4_cidr_block = "172.16.0.0/28"
}
network_policy { enabled = true }
logging_config { enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"] }
monitoring_config {
enable_components = ["SYSTEM_COMPONENTS", "WORKLOADS"]
managed_prometheus { enabled = true }
}
remove_default_node_pool = true
initial_node_count = 1
}
resource "google_container_node_pool" "primary" {
name = "primary-pool"
cluster = google_container_cluster.primary.name
location = "us-central1"
initial_node_count = 2
autoscaling { min_node_count = 1; max_node_count = 5 }
management { auto_repair = true; auto_upgrade = true }
node_config {
machine_type = "e2-standard-4"
disk_size_gb = 100
disk_type = "pd-balanced"
oauth_scopes = ["https://www.googleapis.com/auth/cloud-platform"]
shielded_instance_config {
enable_secure_boot = true
enable_integrity_monitoring = true
}
metadata = { disable-legacy-endpoints = "true" }
}
}
resource "google_compute_subnetwork" "gke" {
name = "gke-subnet"
ip_cidr_range = "10.0.0.0/20"
region = "us-central1"
network = google_compute_network.vpc.id
secondary_ip_range { range_name = "pods"; ip_cidr_range = "10.4.0.0/14" }
secondary_ip_range { range_name = "services"; ip_cidr_range = "10.8.0.0/20" }
}
常见操作
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
故障排查
| 症状 | 原因 | 解决方法 |
|---|---|---|
Pod 卡在 Pending 状态 | 没有资源充足的节点 | 检查自动扩缩器;添加更大的节点池;确认资源请求 |
ImagePullBackOff | 镜像路径错误或缺少 AR 访问权限 | 验证镜像 URL;向节点 SA 授予 roles/artifactregistry.reader |
| Workload Identity 使用了错误的账号 | 缺少 KSA 注释 | 重新为 KSA 添加注释;重启 Pod 以使用新令牌 |
节点处于 NotReady 状态 | 磁盘/内存压力或网络问题 | 运行 kubectl describe node;检查污点和状态条件 |
| Ingress 返回 502 | 后端 Pod 未通过健康检查 | 验证就绪探针;在 Console 中检查 NEG 健康状态 |
| 创建集群时配额错误 | 区域 CPU/IP 配额不足 | 在 IAM & Admin > Quotas 中申请提高配额 |
| 网络策略不起作用 | 集群未启用网络策略 | 使用 --enable-network-policy 重新创建,或使用 Dataplane V2 |
相关技能
- gcp-networking - GKE 集群的 VPC、防火墙规则和负载均衡器
- terraform-gcp - 使用基础设施即代码配置 GKE 集群
- gcp-compute - 工作负载更适合在 VM 而非容器上运行时
- gcp-cloud-sql - 通过 sidecar 代理将 GKE Pod 连接到 Cloud SQL
评论
支持 Markdown