关于 deploying-on-gcp
这项技能面向在 Google Cloud Platform 上构建应用的架构师和开发者,提供服务选型及基础设施模式,覆盖计算、数据、AI/ML、网络、安全和成本,并包含 Terraform、Python SDK 与 gcloud CLI 示例。
主要功能
- 01GCP 服务选型框架
- 02计算、存储与数据库模式
- 03分析和 ML 架构
- 04网络与安全指导
- 05Terraform、Python SDK 和 gcloud 示例
适用场景
- 01为工作负载选择计算与数据库服务
- 02设计 BigQuery 和 Dataflow 分析管道
- 03规划 Vertex AI 训练与模型服务
- 04将工作负载从 AWS 或 Azure 迁移
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ ancoleman/ ai-design-components --skill deploying-on-gcp安装后在 Claude Code 中输入/deploying-on-gcp确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
outputs.yamlexamples/gcloud/common-commands.shexamples/terraform/cloud-run-service.tfreferences/compute-services.mdcost-optimization.mddata-analytics.mdml-ai-services.mdnetworking.mdsecurity-iam.mdstorage-databases.md详细介绍
GCP 模式
使用 Google Cloud Platform 服务构建应用和基础设施,合理选择服务并采用适当的架构模式和最佳实践。
用途
此技能提供 Google Cloud Platform(GCP)服务的决策框架和实现模式,涵盖计算、存储、数据库、数据分析、机器学习、网络和安全。它根据工作负载需求指导服务选择,并通过 Terraform、Python SDK 和 gcloud CLI 展示可用于生产环境的模式。
使用场景
在以下情况下使用此技能:
- 选择 GCP 计算服务(Cloud Run、GKE、Cloud Functions、Compute Engine、App Engine)
- 选择存储或数据库服务(Cloud Storage、Cloud SQL、Spanner、Firestore、Bigtable、BigQuery)
- 设计数据分析管道(BigQuery、Pub/Sub、Dataflow、Dataproc、Composer)
- 实现机器学习工作流(Vertex AI、AutoML、预训练 API)
- 设计网络基础设施(VPC、负载均衡、CDN、Cloud Armor)
- 设置 IAM、安全和成本优化
- 从 AWS 或 Azure 迁移到 GCP
- 构建多云或以 GCP 为先的架构
核心概念
GCP 服务类别
计算选项:
- Cloud Run: 用于无状态 HTTP 服务的无服务器容器(可自动缩容至零)
- GKE(Google Kubernetes Engine): 用于复杂编排的托管 Kubernetes
- Cloud Functions: 用于简单处理的事件驱动函数
- Compute Engine: 可完全控制操作系统的虚拟机
- App Engine: 用于 Web 应用的 PaaS
存储与数据库:
- Cloud Storage: 对象存储,提供 Standard/Nearline/Coldline/Archive 存储类别
- Cloud SQL: 托管 PostgreSQL/MySQL/SQL Server(最高 96TB)
- Cloud Spanner: 全球分布式 SQL,SLA 达 99.999%
- Firestore: 支持实时同步的 NoSQL 文档数据库
- Bigtable: 用于时间序列和物联网的宽列 NoSQL 数据库(PB 级)
- AlloyDB: 兼容 PostgreSQL,性能提升 4 倍
数据与分析:
- BigQuery: 无服务器数据仓库(PB 级 SQL 分析)
- Pub/Sub: 全球消息传递和事件流
- Dataflow: 用于流处理和批处理的 Apache Beam
- Dataproc: 托管 Spark 和 Hadoop 集群
- Cloud Composer: 用于工作流的托管 Apache Airflow
AI/ML 服务:
- Vertex AI: 统一的 ML 平台(训练、部署、监控)
- AutoML: 面向常见任务的无代码 ML
- 预训练 API: 视觉、自然语言、语音、翻译
- TPU: 用于大型模型训练的张量处理单元
决策框架:计算服务选择
Need to run code in GCP?
├─ HTTP service?
│ ├─ YES → Stateless?
│ │ ├─ YES → Cloud Run (auto-scale to zero)
│ │ └─ NO → Need Kubernetes? → GKE | Compute Engine
│ └─ NO (Event-driven)
│ ├─ Simple function? → Cloud Functions
│ └─ Complex orchestration? → GKE | Cloud Run Jobs
选择指南:
- 首选: Cloud Run(除非需要有状态服务或 Kubernetes)
- 需要 Kubernetes: GKE Autopilot(托管)或 Standard(完全控制)
- 简单事件: Cloud Functions(最长执行时间为 60 分钟)
- 完全控制: Compute Engine(可自定义配置的 VM)
决策框架:数据库选择
Choose database type:
├─ Relational (SQL)
│ ├─ Multi-region required? → Cloud Spanner
│ ├─ PostgreSQL + high performance? → AlloyDB
│ └─ Standard RDBMS → Cloud SQL (PostgreSQL/MySQL/SQL Server)
│
├─ Document (NoSQL)
│ ├─ Mobile/web with offline sync? → Firestore
│ └─ Flexible schema, no offline? → MongoDB Atlas (Marketplace)
│
├─ Key-Value
│ ├─ Time-series or IoT data? → Bigtable
│ └─ Caching layer? → Memorystore (Redis/Memcached)
│
└─ Analytics
└─ Petabyte-scale SQL analytics → BigQuery
决策框架:存储选择
Storage type needed?
├─ Objects/Files
│ ├─ Frequent access → Cloud Storage (Standard)
│ ├─ Monthly access → Cloud Storage (Nearline)
│ ├─ Quarterly access → Cloud Storage (Coldline)
│ └─ Yearly access → Cloud Storage (Archive)
│
├─ Block storage → Persistent Disk (SSD/Standard/Extreme)
└─ Shared filesystem → Filestore (NFS)
GCP、AWS 与 Azure 服务对照
| 类别 | GCP | AWS | Azure |
|---|---|---|---|
| 无服务器容器 | Cloud Run | Fargate | Container Instances |
| Kubernetes | GKE | EKS | AKS |
| 函数 | Cloud Functions | Lambda | Functions |
| VM | Compute Engine | EC2 | Virtual Machines |
| 对象存储 | Cloud Storage | S3 | Blob Storage |
| SQL 数据库 | Cloud SQL | RDS | SQL Database |
| NoSQL 文档数据库 | Firestore | DynamoDB | Cosmos DB |
| 数据仓库 | BigQuery | Redshift | Synapse |
| 消息传递 | Pub/Sub | SNS/SQS | Service Bus |
| ML 平台 | Vertex AI | SageMaker | Machine Learning |
架构模式
模式 1:无服务器 Web 应用
用例: 无状态 HTTP API,配合数据库和缓存
架构:
Internet → Cloud Load Balancer → Cloud Run → Cloud SQL (PostgreSQL)
→ Memorystore (Redis)
→ Cloud Storage
关键服务:
- Cloud Run 用于 API 服务(容器自动扩缩)
- Cloud SQL 用于事务数据
- Memorystore 用于缓存
- Cloud Storage 用于文件上传
详细的 Terraform 配置请参阅 references/compute-services.md。
模式 2:数据分析平台
用例: 实时事件处理与分析
架构:
Data Sources → Pub/Sub → Dataflow → BigQuery → Looker/Tableau
↓
Cloud Storage (staging)
关键服务:
- Pub/Sub 用于事件接入(至少一次投递)
- Dataflow 用于流处理(Apache Beam)
- BigQuery 用于分析(分区表、聚簇)
- Cloud Storage 用于暂存和备份
BigQuery 优化模式请参阅 references/data-analytics.md。
模式 3:机器学习流水线
用例: 端到端机器学习工作流
架构:
Training Data (GCS) → Vertex AI Training → Model Registry → Vertex AI Endpoints
↓
Predictions
关键服务:
- Vertex AI Workbench 用于笔记本开发
- Vertex AI Training 用于自定义模型(支持 GPU/TPU)
- Vertex AI Endpoints 用于模型服务(自动扩缩)
- Vertex AI Pipelines 用于编排(Kubeflow)
机器学习实现示例请参阅 references/ml-ai-services.md。
模式 4:GKE 微服务平台
用例: 使用多个服务进行复杂编排
架构:
Internet → Cloud Load Balancer → GKE Cluster
├─ Ingress Controller
├─ Service Mesh (optional)
├─ Microservice A
├─ Microservice B
└─ Microservice C
关键特性:
- GKE Autopilot(全托管节点)或 Standard(自定义配置)
- Workload Identity,用于安全访问 GCP 服务
- 配置 Private Google Access 的私有集群
- Config Connector,用于通过 Kubernetes 管理 GCP 资源
GKE 设置和最佳实践请参阅 references/compute-services.md。
最佳实践
成本优化
计算:
- 对可预测的工作负载使用 Committed Use Discounts(优惠 57%)
- 对容错型工作负载使用 Spot VM(优惠 60-91%)
- Cloud Run 在空闲时缩容至零(不收费)
- GKE Autopilot 仅按 Pod 资源收费,不按节点收费
存储:
- 使用适当的 Cloud Storage 存储类别(Standard/Nearline/Coldline/Archive)
- 启用 Object Lifecycle Management,以转换冷数据
- 使用 Coldline 或 Archive 存档备份(比 Standard 便宜 99%)
数据:
- BigQuery:使用分区表和聚簇表
- 仅查询所需列(避免使用
SELECT *) - 使用 BI Engine 进行缓存(最多 10TB 免费)
- 对大量使用 BigQuery 的情况,可考虑固定费率定价
详细的成本策略请参阅 references/cost-optimization.md。
安全基础
IAM 最佳实践:
- 遵循最小权限原则
- 应用使用服务账号,而非用户账号
- 为 GKE 工作负载启用 Workload Identity(无需服务账号密钥)
- 使用 Secret Manager 管理密钥,而非环境变量
网络安全:
- 使用 Private Google Access(无需公共 IP 即可访问 GCP 服务)
- 启用 Cloud NAT,让私有实例访问出站互联网
- 实施 VPC Service Controls,防止数据外泄
- 使用 Identity-Aware Proxy(IAP)实现零信任访问
数据安全:
- 启用静态加密(默认启用)和传输中加密
- 对敏感数据使用 Customer-Managed Encryption Keys(CMEK)
- 实施 VPC Service Controls 边界,保护数据
- 为所有项目启用审计日志记录
全面的安全模式请参阅 references/security-iam.md。
高可用性
多区域策略:
- Cloud Storage:使用多区域位置(US、EU、ASIA)
- Cloud SQL:启用 Regional HA(自动故障转移)
- Cloud Spanner:使用多区域配置(99.999% SLA)
- Global Load Balancing:将流量路由至最近的健康后端
备份和灾难恢复:
- Cloud SQL:启用自动备份和时间点恢复
- Persistent Disk:安排快照备份
- Cloud Storage:为关键数据启用版本控制
- BigQuery:使用表快照进行时间旅行
网络和高可用性模式请参阅 references/networking.md。
快速参考
常用 gcloud 命令
# Project management
gcloud projects list
gcloud config set project PROJECT_ID
# Cloud Run
gcloud run deploy SERVICE_NAME --image IMAGE_URL --region REGION
gcloud run services list
# GKE
gcloud container clusters create-auto CLUSTER_NAME --region REGION
gcloud container clusters get-credentials CLUSTER_NAME --region REGION
# Cloud Storage
gsutil mb gs://BUCKET_NAME
gsutil cp FILE gs://BUCKET_NAME/
# BigQuery
bq mk DATASET_NAME
bq query --use_legacy_sql=false 'SELECT * FROM dataset.table LIMIT 10'
# Cloud SQL
gcloud sql instances create INSTANCE_NAME --database-version=POSTGRES_15 --region=REGION
gcloud sql connect INSTANCE_NAME --user=postgres
完整的命令参考请参阅 examples/gcloud/common-commands.sh。
Python SDK 快速入门
# Cloud Storage
from google.cloud import storage
client = storage.Client()
bucket = client.bucket('my-bucket')
blob = bucket.blob('file.txt')
blob.upload_from_filename('local-file.txt')
# BigQuery
from google.cloud import bigquery
client = bigquery.Client()
query = "SELECT * FROM `project.dataset.table` LIMIT 10"
results = client.query(query).result()
# Pub/Sub
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('project', 'topic-name')
future = publisher.publish(topic_path, b'message data')
完整的 Python 示例请参阅 examples/python/。
Terraform 快速入门
# Provider configuration
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}
provider "google" {
project = "my-project-id"
region = "us-central1"
}
# Cloud Run service
resource "google_cloud_run_service" "api" {
name = "api-service"
location = "us-central1"
template {
spec {
containers {
image = "gcr.io/project/api:latest"
}
}
}
}
完整的 Terraform 示例请参阅 examples/terraform/。
服务选择速查表
| 需求 | 推荐服务 | 替代方案 |
|---|---|---|
| 无状态 HTTP API | Cloud Run | App Engine |
| 复杂编排 | GKE Autopilot | GKE Standard |
| 事件处理 | Cloud Functions | Cloud Run Jobs |
| 对象存储 | Cloud Storage | 不适用 |
| 关系型数据库 | Cloud SQL | AlloyDB、Spanner |
| NoSQL 文档数据库 | Firestore | MongoDB Atlas |
| 时序数据 | Bigtable | 不适用 |
| 数据仓库 | BigQuery | 不适用 |
| 消息队列 | Pub/Sub | 不适用 |
| 流处理 | Dataflow | Dataproc |
| 批处理 | Dataflow | Dataproc |
| ML 训练 | Vertex AI | 在 GKE 上自定义 |
| 缓存 | Memorystore Redis | 不适用 |
与其他技能集成
相关技能:
- 基础设施即代码: 使用 Terraform 配置 GCP 资源(参见
examples/terraform/) - kubernetes-operations: 在 GKE 上部署和管理应用程序
- building-ci-pipelines: 使用 Cloud Build 为 Cloud Run 或 GKE 配置 CI/CD
- secret-management: 使用 Secret Manager 管理敏感配置
- observability: 使用 Cloud Monitoring 和 Cloud Logging 管理指标和日志
- data-architecture: 使用 BigQuery 和 Cloud Storage 设计数据湖和数据仓库
- mlops-patterns: 使用 Vertex AI 实现 ML 工作流
- aws-patterns: 对比 AWS 和 GCP 的同类服务,以支持多云部署
- azure-patterns: 对比 Azure 和 GCP 的同类服务
渐进式披露
详细文档:
- 计算服务: 参见
references/compute-services.md,了解 Cloud Run、GKE、Cloud Functions、Compute Engine 和 App Engine 的模式 - 存储与数据库: 参见
references/storage-databases.md,了解详细的服务选择和配置 - 数据分析: 参见
references/data-analytics.md,了解 BigQuery、Pub/Sub、Dataflow 和 Dataproc 的模式 - ML/AI 服务: 参见
references/ml-ai-services.md,了解 Vertex AI、AutoML 和预训练 API 的用法 - 网络: 参见
references/networking.md,了解 VPC、负载均衡、CDN 和 Cloud Armor 的模式 - 安全与 IAM: 参见
references/security-iam.md,了解 IAM 模式、Workload Identity 和 Secret Manager - 成本优化: 参见
references/cost-optimization.md,了解详细的成本削减策略
可运行示例:
- Terraform 配置: 参见
examples/terraform/,获取基础设施模板 - Python SDK 用法: 参见
examples/python/,获取客户端库示例 - gcloud CLI 命令: 参见
examples/gcloud/common-commands.sh,获取命令参考
关键决策摘要
选择 GCP 的情况:
- 数据分析工作负载(BigQuery 属于一流服务)
- ML/AI 应用(Vertex AI、TPU 以及 Google Research 的支持)
- Kubernetes 原生应用(GKE 由 Kubernetes 的创建者打造)
- 无服务器容器(Cloud Run 成熟且经济高效)
- 实时流处理(Pub/Sub + Dataflow)
GCP 的独特优势:
- BigQuery:无服务器、支持 PB 级规模、速度最快的数据仓库
- Cloud Run:最成熟的无服务器容器平台
- GKE:最先进的托管 Kubernetes(Autopilot 模式)
- Vertex AI:统一的 ML 平台(训练、部署、监控)
- 按秒计费和持续使用折扣(自动节省成本)
多区域建议:
- 生产工作负载:使用多区域配置,达到 99.95% 以上的 SLA
- Cloud Storage:使用多区域配置以支持全球访问
- Cloud Spanner:使用多区域配置以支持全球事务
- 全球负载均衡:将请求路由到最近的健康后端