正式上市前版本:2026-02-26

7 使用 Prometheus 监视 Besu 度量

您可以使用 Prometheus 和 kube-prometheus-stack 从在 Kubernetes 集群中运行的 Besu 节点检索度量。

要安装监视,请使用 Consensys/quorum-kubernetes 系统信息库中维护的 Helm、kube-prometheus-stack 和预定义值文件 (monitoring.yml)。在这种情况下,Prometheus 在 Istio 服务网格中运行,并通过双向 TLS 安全地抓取度量。kube-prometheus-stack 软件包安装以下软件。
  • Prometheus
  • Prometheus 运算符
  • Grafana
  • 预警管理器
  • 标准 Kubernetes 导出器

在 kube-prometheus-stack 架构中,Prometheus 使用名为 ServiceMonitor 的 Kubernetes 定制资源来发现抓取目标。此资源定义要擦除的服务、暴露度量的端口和路径以及 TLS 和双向 TLS 的配置。使用 Helm 安装 kube-prometheus-stack 时,会自动创建 kube-state-metrics 和 node-exporter 等组件的 ServiceMonitor 资源。但是,要抓取 Oracle Blockchain Platform Enterprise Edition for Hyperledger Besu 指标,您必须创建和管理自己的 ServiceMonitor 资源。

安装以下先决条件。
  • Helm v3.x。您可以通过运行以下命令验证 Helm 版本。
    helm version
  • kubectl
您还必须具有对 Kubernetes 集群的命令行访问权限。有关详细信息,请参阅连接到 Oracle Kubernetes Engine
  1. 使用与 Besu 兼容的 monitoring.yml 文件安装 kube-prometheus-stack。
    1. 输入以下命令下载该文件。monitoring.yml 文件在 Consensys/quorum-kubernetes 存储库中维护。
      curl -o monitoring.yml \ 
      https://raw.githubusercontent.com/Consensys/quorum-kubernetes/master/helm/values/monitoring.yml 
    2. 如果需要,请在部署前在 monitoring.yml 中更新 Grafana 管理员密码并配置警报接收器(例如电子邮件或 Slack)。
    3. 如果尚不存在,请添加 Prometheus 社区 Helm 存储库
      helm repo add prometheus-community https://prometheus-community.github.io/helm-charts 
      helm repo update 
    4. 将监视堆栈安装在专用于监视的名称空间中。在以下命令中,名称空间称为监视
      helm install monitoring prometheus-community/kube-prometheus-stack \ 
        --version 34.10.0 \ 
        --namespace monitoring \ 
        --create-namespace \ 
        --values monitoring.yml 
    5. 运行以下命令以检查部署状态。
      kubectl get pods -n monitoring -l release=monitoring
    安装过程将监视堆栈部署到 monitoring 名称空间中,应用 monitoring.yml 文件中的覆盖以实现 Besu 兼容性,并创建堆栈所需的标准 Kubernets ServiceMonitor 资源。
  2. 将 Istio 注释添加到 monitoring.yml 文件中,以便为 Prometheus 启用 Istio sidecar 注入。Prometheus 必须加入 Istio 网格,通过相互 TLS 抓取 Besu 端点。打开 monitoring.yml 文件进行编辑,并找到以下部分。
    prometheus: 
      prometheusSpec: 
        podMetadata:
    添加以下 Istio 注释。
    prometheus: 
      prometheusSpec: 
        podMetadata: 
          annotations: 
            sidecar.istio.io/inject: "true" 
            sidecar.istio.io/userVolumeMount: | 
              [{"name": "istio-certs", "mountPath": "/etc/istio-certs", "readOnly": true}] 
            proxy.istio.io/config: | 
              proxyMetadata: 
                OUTPUT_CERTS: /etc/istio-certs 
              proxyMetadata.INBOUND_CAPTURE_PORTS: "" 
    这些注释将 Prometheus 添加到 Istio 网格中,配置 Istio 将工作量 mTLS 证书写入共享卷,阻止 Envoy 拦截 Prometheus 入站流量,并使 Istio 生成的证书可用于 Prometheus 容器。
  3. 将卷和卷挂载信息添加到 monitoring.yml 文件以将 Istio 证书挂载到 Prometheus 中。在 monitoring.yml 文件中查找以下文本。
    找到最初为空的 volumes 部分。
        volumes: []
    按以下文本所示更新 volumes 部分。
        volumes: 
          - name: istio-certs 
            emptyDir: 
              medium: Memory 
     
    找到最初为空的 volumeMounts 部分。
        volumeMounts: []
    按以下文本所示更新 volumeMounts 部分。
        volumeMounts: 
          - name: istio-certs 
            mountPath: /etc/prom-certs 
            readOnly: true
    由 Istio 生成的证书现可在 Prometheus 中获得,网址为 /etc/prom-certs/.
  4. 应用更新的配置。
    1. 使用修改的值文件升级现有的 Helm 发行版。
      helm upgrade monitoring prometheus-community/kube-prometheus-stack \ 
        --namespace monitoring \ 
        --values monitoring.yml 
    2. 验证 Prometheus pod 是否已重新启动,并且正在使用 Istio sidecar 运行。
      kubectl get pods -n monitoring | grep prometheus 
  5. 使用以下示例为 Besu 度量创建 ServiceMonitor 资源。
    apiVersion: monitoring.coreos.com/v1 
    kind: ServiceMonitor 
    metadata: 
      name: obp-besu-rpc-metrics 
      namespace: <besu-namespace> 
      labels: 
        release: monitoring   # Must match Prometheus serviceMonitorSelector 
    spec: 
      selector: 
        matchLabels: 
          app: besu 
          besu-role: rpc 
      namespaceSelector: 
        matchNames: 
          - <besu-namespace> 
      endpoints: 
        - port: metrics 
          path: /metrics 
          interval: 30s 
          scheme: https 
          tlsConfig: 
            caFile: /etc/prom-certs/root-cert.pem 
            certFile: /etc/prom-certs/cert-chain.pem 
            keyFile: /etc/prom-certs/key.pem 
            insecureSkipVerify: true 
    证书路径必须与挂载的位置 (etc/prom-certs) 匹配。release 标签必须与 Prometheus 中的 serviceMonitorSelector 值匹配。上面的示例从 RPC 节点 (besu-role: rpc) 报废度量。要从引导节点、验证器节点或归档节点中提取度量,请创建单独的 ServiceMonitor 资源并相应地编辑 besu-role 标签(bootnodevalidatorarchive)。
  6. 验证是否正在 Prometheus 中收集度量。
    1. 运行以下命令以转发 Prometheus 服务的端口。
      kubectl port-forward -n <namespace> prometheus-monitoring-kube-prometheus-prometheus-
    2. 打开 http://localhost:9090,然后依次选择状态目标。确认 Besu 目标处于 UP 状态。
    3. 运行示例查询,例如以下示例。
      besu_blockchain_chain_head_transaction_count 
  7. 访问 Grafana 以确认正在收集度量。运行以下命令以转发 Grafana 服务的端口。
    kubectl port-forward svc/monitoring-grafana -n <namespace> 3000:80 
    • URL:http://localhost:3000
    • 用户名:admin
    • 运行以下命令以检索密码:
      kubectl get secret -n <namespace> monitoring-grafana \ 
      -o jsonpath="{.data.admin-password}" | base64 --decode ; echo