GCP GKE Kubernetes集群搭建指南
Google Kubernetes Engine(GKE)是全球最成熟的托管Kubernetes服务,也是GCP最具竞争力的产品之一。作为Kubernetes的创始者,Google在容器编排领域拥有无与伦比的深度。本文将指导你从零搭建一个生产级的GKE集群。
一、GKE模式选择
GKE提供两种运营模式,理解其区别是集群规划的第一步:
| 特性 | Autopilot | Standard | |------|----------|----------| | 节点管理 | Google全托管 | 用户自行管理 | | 计费方式 | 按Pod资源请求量计费 | 按节点计费 | | 节点运维 | 无需关注 | 需OS升级、安全补丁 | | 可定制性 | 有限(Kubernetes层面完全控制) | 完全控制 | | 适用场景 | 无专职SRE团队、专注应用 | 需要节点级控制、特殊硬件 | | 费用可预测性 | 高(按需付费) | 中(需预估节点利用率) |
选择建议:
- 初创公司、中小团队 → Autopilot
- 大型企业、有特殊合规要求 → Standard
- 成本敏感型工作负载 → Standard(配合预留折扣)
费用对比(asia-east1区域)
| 场景 | Autopilot费用 | Standard费用 | |------|-------------|-------------| | 10个Pod(1C2G each) | ~$180/月 | ~$120/月(含节点闲置) | | 50个Pod(1C2G each) | ~$900/月 | ~$600/月 | | 波动负载 | 更优(精确按需) | 需预留余量 |
与 阿里云 ACK相比,GKE Autopilot省去了节点运维开销,对没有专职SRE的团队尤为友好。
二、集群规划
2.1 网络模式
| 模式 | Pod IP来源 | 最大Pod数/节点 | 推荐 | |------|-----------|-------------|------| | VPC原生(别名IP) | VPC子网别名 | 110 | 是 | | 路由模式 | 节点路由 | 110 | 否(旧模式) |
创建集群时务必选择VPC原生模式,它支持网络策略和私有GKE集群。
2.2 集群网络规划
VPC: 10.0.0.0/8
├── 子网(节点): 10.0.0.0/20 (4091个节点)
├── 子网(Pod别名): 10.4.0.0/14 (262,144个Pod)
└── 子网(Service): 10.8.0.0/20 (4091个Service)
2.3 集群类型
| 类型 | 说明 | 适用场景 | |------|------|---------| | Zonal | 单可用区 | 开发/测试 | | Regional | 多可用区(3副本控制面) | 生产环境 | | Private | 节点无公网IP | 安全合规要求高 |
三、创建GKE集群
3.1 Autopilot集群
gcloud container clusters create-auto my-autopilot-cluster \
--region=asia-east1 \
--network=my-vpc \
--subnetwork=my-subnet \
--enable-private-nodes \
--master-ipv4-cidr-block=172.16.0.0/28 \
--release-channel=regular
3.2 Standard集群
gcloud container clusters create my-standard-cluster \
--region=asia-east1 \
--num-nodes=2 \
--machine-type=e2-standard-4 \
--disk-type=pd-ssd \
--disk-size=100GB \
--network=my-vpc \
--subnetwork=my-subnet \
--enable-private-nodes \
--enable-ip-alias \
--master-ipv4-cidr-block=172.16.0.0/28 \
--release-channel=regular \
--addons=HorizontalPodAutoscaling,NetworkPolicy
3.3 节点池管理
Standard集群推荐分离系统节点和工作负载节点:
# 系统组件节点池
gcloud container node-pools create system-pool \
--cluster=my-standard-cluster \
--machine-type=e2-standard-2 \
--num-nodes=1 \
--node-labels=dedicated=system
# 工作负载节点池
gcloud container node-pools create workload-pool \
--cluster=my-standard-cluster \
--machine-type=e2-standard-8 \
--min-nodes=1 --max-nodes=10 \
--enable-autoscaling \
--node-labels=dedicated=workload
四、连接集群
# 获取集群凭据
gcloud container clusters get-credentials my-standard-cluster \
--region=asia-east1
# 验证连接
kubectl get nodes
kubectl cluster-info
私有集群访问
私有集群的API Server无公网IP,需通过以下方式访问:
- Cloud Shell:自动在同一VPC内
- VPN/专线:从本地网络访问
- Authorized Networks:临时添加你的IP到授权列表
# 临时授权你的IP访问
gcloud container clusters update my-standard-cluster \
--region=asia-east1 \
--enable-master-authorized-networks \
--master-authorized-networks=YOUR_IP/32
五、部署应用
5.1 示例Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web
image: nginx:1.25
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1000m"
memory: "1Gi"
ports:
- containerPort: 80
5.2 暴露服务
| 方式 | 说明 | GCP集成 | |------|------|---------| | ClusterIP | 集群内部访问 | 无 | | NodePort | 节点端口暴露 | 不推荐生产 | | LoadBalancer | 自动创建Cloud Load Balancer | GCP原生集成 | | Ingress | HTTP(S)路由 | GKE Ingress → Cloud LB |
# 创建LoadBalancer Service
kubectl expose deployment web-app --type=LoadBalancer --port=80
# 或使用Ingress
kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-app-ingress
spec:
defaultBackend:
service:
name: web-app
port:
number: 80
EOF
5.3 自动扩缩
kubectl autoscale deployment web-app \
--cpu-percent=60 \
--min=2 \
--max=10
六、安全配置
| 安全措施 | 配置方法 | 重要性 | |---------|---------|-------| | Workload Identity | 关联K8s SA与GCP IAM SA | 高 | | Network Policy | 限制Pod间网络通信 | 高 | | Binary Authorization | 只允许可信镜像部署 | 中 | | Pod Security Standards | 限制特权容器 | 高 | | Secret Manager | 集成GCP Secret Manager | 中 | | Container Scanning | 自动漏洞扫描 | 中 |
Workload Identity配置
# 启用Workload Identity
gcloud container clusters update my-standard-cluster \
--region=asia-east1 \
--workload-pool=my-project.svc.id.goog
# 绑定K8s Service Account到GCP Service Account
gcloud iam service-accounts add-iam-policy-binding \
[email protected] \
--role=roles/iam.workloadIdentityUser \
--member="serviceAccount:my-project.svc.id.goog[my-namespace/my-k8s-sa]"
七、监控与运维
7.1 Cloud Monitoring集成
GKE自动将指标发送到Cloud Monitoring:
- 节点CPU/内存/磁盘
- Pod资源使用
- 控制面延迟
- 网络流量
7.2 Cloud Logging集成
所有容器日志自动发送到Cloud Logging,支持结构化日志和日志关联查询。
7.3 关键告警配置
| 告警 | 条件 | 建议阈值 | |------|------|---------| | 节点NotReady | 持续5分钟 | 立即处理 | | Pod重启次数 | 15分钟内>3次 | 检查应用健康 | | CPU使用率 | >80%持续10分钟 | 自动扩容 | | 磁盘使用率 | >85% | 清理日志/扩容 | | API Server延迟 | >1秒 | 检查集群负载 |
与 AWS EKS相比,GKE的监控集成更加开箱即用,无需额外安装Prometheus/ Grafana即可获得完整的可观测性。
八、成本优化
- 承诺使用折扣:1年约-20%,3年约-57%
- 可抢占式VM节点池:批处理和CI/CD工作负载
- HPA自动缩容:低谷期自动释放节点
- GKE成本分析:启用后可按Namespace/Label查看费用
- 合理设置requests:避免过度分配导致浪费
总结
GKE凭借Google在Kubernetes领域的深厚积累,提供了最省心的Kubernetes托管体验。无论是Autopilot的全托管模式还是Standard的灵活控制,GKE都能满足不同规模和需求的容器化场景。
多云(Duoyun Cloud) 作为GCP合作伙伴,提供GKE集群的架构咨询和资源代购服务。通过多云采购GCP资源,可享受专属渠道折扣和专业中文支持,帮助你以更低成本构建生产级Kubernetes平台。访问 duoyun.io 获取专属优惠。