本教程介绍如何将基于 Stackdriver Prometheus Sidecar 的指标收集流水线迁移到全代管式 Google Kubernetes Engine (GKE) 工作负载指标。与使用 Stackdriver Prometheus Sidecar 相比,使用 GKE 工作负载指标具有诸多优势:
- 设置简单:使用单个
kubectl
命令来部署 PodMonitor 自定义资源,即可以开始收集指标。 - 可灵活配置:调整抓取端点、频率和其他参数。
- 全代管式:Google 会维护流水线,从而降低总体拥有费用。
- 控制费用:通过灵活的指标过滤,轻松管理 Cloud Monitoring 费用。
- 开放标准:使用 PodMonitor 自定义资源配置工作负载指标,该资源是根据 Prometheus Operator 的 PodMonitor 资源建模的。
- HPA 支持:与 Stackdriver 自定义指标适配器兼容,可对自定义指标启用横向自动扩缩功能。
- 价格更优:价格更直观、可预测且价格更低。
- Autopilot 支持:GKE 工作负载指标同时适用于 GKE Standard 和 GKE Autopilot 集群。
确认您使用的是 Stackdriver Prometheus Sidecar
Stackdriver Prometheus Sidecar 是之前推荐用于从 GKE 集群收集 Prometheus 样式指标并将其提取到 Cloud Monitoring 的方法。在此方法中,现有 Prometheus 安装(通常在 GKE 下作为 StatefulSet 或 Deployment 运行)通过 Sidecar 进行修补,该 Sidecar 会将其抓取的所有指标导出到 Cloud Monitoring。Prometheus Helm 图表提供了一种配置此集成的便捷方式。
确定 GKE 工作负载指标是否涵盖您的用量
以下核对清单可帮助确定您使用的 Stackdriver Prometheus Sidecar 方法中是否有无法通过 GKE 工作负载指标重现的功能。
特征 | 支持/解决方法 |
---|---|
Prometheus 是否用于监控同一集群中除 Pod 之外的其他内容?要确定这一点,请查看以下内容的使用情况:
|
pod 角色支持开箱即用。endpoint 角色可通过公开端口以及为端口命名进行模拟。 |
是否根据字段而不是标签来选择 Pod? | 使用等效标签选择器,这可能需要向所需的 Pod 添加新标签。 |
Prometheus 是否配置了任何形式的 HTTP 授权或双向 TLS 以进行抓取? | GKE 工作负载指标不支持此功能。 |
Prometheus 是否配置了使用 keep 和 drop 以外的操作的 metric_relabel_configs ? |
GKE 工作负载指标不支持此功能。 |
您是否使用 Stackdriver Prometheus Sidecar 的计数器聚合器或指标重命名功能? | GKE 工作负载指标不支持此功能。 |
Prometheus 配置是否包含提醒功能? | 将其转换为 Monitoring 中的提醒。 |
您的 Sidecar 配置是否包含 static_metadata ? |
GKE 工作负载指标会收集您的指标,但会忽略文档。 |
将 Prometheus 配置迁移到 PodMonitor 自定义资源
对于 Prometheus 配置中定义的每个作业(scrape_configs
数组中的项),请创建相应的 PodMonitor 自定义资源。说明如下所示:
Prometheus 配置 | PodMonitor CRD |
---|---|
scrape_configs: - job_name: example metrics_path: /metrics scheme: http scrape_interval: 20s kubernetes_sd_configs: - role: endpoints namespaces: names: - gke-workload-metrics selectors: - role: endpoints label: "app=prom-example" field: "port=metrics-port" |
apiVersion: monitoring.gke.io/v1alpha1 kind: PodMonitor metadata: name: example spec: namespaceSelector: matchNames: - gke-workload-metrics selector: matchLabels: app: prom-example podMetricsEndpoints: - port: metrics-port path: /metrics scheme: http interval: 20s |
将 Sidecar 配置迁移到 PodMonitor 自定义资源
在 Sidecar 级层配置的过滤条件适用于所有抓取作业。因此,您必须将这些配置附加到您在上一步中创建的每个 PodMonitor CRD。
Sidecar CLI | PodMonitor CRD |
---|---|
$ stackdriver-prometheus-sidecar \ --include='metric_name{label="foo"}' |
apiVersion: monitoring.gke.io/v1alpha1 kind: PodMonitor metadata: name: example spec: namespaceSelector: matchNames: - gke-workload-metrics selector: matchLabels: app: prom-example podMetricsEndpoints: - port: metrics-port path: /metrics scheme: http interval: 20s metricRelabelings: - sourceLabels: [__name__, label] regex: "^metric_name;foo$" action: keep - sourceLabels: [__name__] |
在 Google Cloud Monitoring 中查看指标
使用 Metrics Explorer 验证现在是否已通过 GKE 工作负载指标流水线提取指标。
请注意,如果指标以前命名为 external.googleapis.com/prometheus/metric_name
,则现在命名为 workload.googleapis.com/metric_name
。请务必修改依赖于这些指标的任何信息中心或提醒,以便改用新的命名方案。