You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

K8s上如何配置Grafana Agent供Prometheus拉取指标及推送至Pushgateway

问题解答合集

1. K8s集群部署Grafana Agent后能否由中心化Prometheus拉取监控指标

完全支持,Grafana Agent默认开放12345端口,通过/metrics路径暴露所有采集到的监控指标,你只需要配置Prometheus的拉取规则指向对应端口即可。

2. 能否将Grafana Agent采集的指标推送至Prometheus Pushgateway

可以,但无法直接通过remote_write接口推送,因为Prometheus Pushgateway原生不兼容Prometheus remote_write协议,你可以通过官方原生支持的pushgateway集成组件,或者通过中转服务转换协议实现推送。

操作实现指引

方案一:中心化Prometheus主动拉取Grafana Agent指标

步骤1:K8s部署Grafana Agent时暴露指标端口

DaemonSet配置示例(核心片段):

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: grafana-agent
  namespace: monitoring
  labels:
    app.kubernetes.io/name: grafana-agent
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: grafana-agent
  template:
    metadata:
      labels:
        app.kubernetes.io/name: grafana-agent
    spec:
      containers:
      - name: agent
        image: grafana/agent:v0.38.1
        args:
        - run
        - /etc/agent/config.river
        - --server.http.listen-addr=0.0.0.0:12345
        ports:
        - name: metrics
          containerPort: 12345
          protocol: TCP

步骤2:配置Prometheus拉取规则

如果使用Prometheus Operator,直接创建ServiceMonitor即可,否则直接在Prometheus的scrape_configs中添加如下规则:

scrape_configs:
- job_name: grafana-agent
  honor_labels: true
  kubernetes_sd_configs:
  - role: pod
    namespaces:
      names: [monitoring]
  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_name]
    regex: grafana-agent
    action: keep
  - source_labels: [__meta_kubernetes_pod_container_port_name]
    regex: metrics
    action: keep

方案二:Grafana Agent推送指标到Prometheus Pushgateway

直接在Grafana Agent的River配置中添加pushgateway导出组件即可,配置示例:

// 采集任务配置,示例为采集K8s节点指标
prometheus.scrape "node_metrics" {
  targets = discovery.kubernetes.nodes.targets
  forward_to = [prometheus.exporter.pushgateway.default.receiver]
  scrape_interval = "15s"
}

// Pushgateway推送配置
prometheus.exporter.pushgateway "default" {
  endpoint = "http://<你的Pushgateway服务地址>:9091"
  job_name = "grafana_agent_<集群标识>"
  // 可选配置,开启推送时替换原有同标签组的指标
  replace = true
}

配置生效后,Grafana Agent会自动将采集到的指标转换为Pushgateway兼容的格式推送,你可以直接在Pushgateway的UI中查看推送上来的指标。

内容的提问来源于stack exchange,提问作者user3422981

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 04:45:05