关于 gke-observability
面向 GKE 集群与工作负载运维人员的可观测性工作流指南,涵盖日志、指标、追踪和性能分析,并提供配置与查询方法。
主要功能
- 01检查 Cloud Logging 和 Monitoring
- 02启用托管 Prometheus
- 03核查工作负载日志
- 04配置仪表板与告警
- 05启用分布式追踪和持续分析
- 06查询日志及控制平面指标
适用场景
- 01核查生产集群的日志与监控配置
- 02采集应用自定义指标
- 03通过 LQL 定位错误日志
- 04追踪微服务延迟并分析性能
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ googlecloudplatform/ gke-mcp --skill gke-observability安装后在 Claude Code 中输入/gke-observability确认已加载。
详细介绍
GKE 可观测性技能
此技能提供一系列工作流,确保 GKE 集群和工作负载具备满足生产环境需求的可观测性。
工作流
1. 审查集群可观测性
检查集群是否已启用 Cloud Logging 和 Cloud Monitoring。
命令:
gcloud container clusters describe <cluster-name> --region <region> --project <project-id> --format="json(loggingConfig, monitoringConfig)"
检查 loggingService 和 monitoringService 是否设置为 none 以外的值(通常为 logging.googleapis.com/kubernetes 和 monitoring.googleapis.com/kubernetes)。
2. 启用 Managed Service for Prometheus
Google Cloud Managed Service for Prometheus 是从应用中收集指标的推荐方式。
启用命令:
gcloud container clusters update <cluster-name> \
--enable-managed-prometheus \
--region <region>
验证安装:
kubectl get pods -n gmp-system
3. 验证工作负载日志
确保工作负载将日志写入标准输出,以便 Cloud Logging 自动收集。
检查工作负载日志:
kubectl logs <pod-name> -n <namespace>
尽可能确保日志采用结构化格式(例如 JSON),以便更轻松地查询。
4. 信息中心和提醒
建议在 Cloud Monitoring 中为以下关键指标创建信息中心:
- CPU 使用率
- 内存使用率
- 请求延迟
- 错误率
为关键阈值设置提醒政策。
5. 分布式跟踪
启用分布式跟踪,以跟踪跨微服务的请求。
- 操作:建议在应用中使用 OpenTelemetry,将跟踪数据发送到 Cloud Trace。
- 优势:有助于识别分布式系统中的延迟瓶颈。
6. 持续性能分析
使用持续性能分析,以较低的开销分析生产环境中的应用性能。
- 操作:建议在应用代码中集成 Cloud Profiler Agent。
- 优势:有助于识别生产环境中消耗 CPU 和内存的函数。
7. 使用 LQL 查询日志
在 Cloud Logging 中使用 Logging Query Language(LQL)查找特定日志。
LQL 查询示例:
- 查找特定容器的错误日志:
resource.type="k8s_container" resource.labels.container_name="my-app" severity>=ERROR - 查找包含特定消息的日志:
resource.type="k8s_container" textPayload:"connection refused"
8. 启用控制平面指标
对于 Standard 集群,可以启用 Kubernetes API 服务器、调度器和控制器管理器的指标收集。
命令:
gcloud container clusters update <cluster-name> \
--monitoring=SYSTEM,API_SERVER,SCHEDULER,CONTROLLER_MANAGER \
--region <region>
9. 启用 Dataplane V2 可观测性
如果使用 GKE Dataplane V2,可以启用高级 L4 可观测性。
命令:
gcloud container clusters update <cluster-name> \
--enable-dataplane-v2-observability \
--region <region>
这样即可观测流量流向和网络指标。
最佳实践
- 结构化日志:在应用中使用 JSON 日志,方便在 Cloud Logging 中搜索和分析日志。
- 自定义指标:使用 Managed Service for Prometheus,以公开和收集自定义应用指标。
- 完善可观测性支柱:除了日志和指标之外,还要实施跟踪和性能分析,以实现全面可见性。
- 控制平面指标:启用控制平面指标(如果使用 Standard),以监控 API 服务器和调度器的运行状况。