管理指标

平台的监控系统基于 Prometheus / VictoriaMetrics 收集的指标。本文将指导您如何管理这些指标。

查看平台组件暴露的指标

平台内集群组件的监控方法是通过 ServiceMonitor 提取暴露的指标。平台中的指标通过 /metrics 端点公开。您可以使用以下示例命令查看平台中特定组件暴露的指标:

curl -s http://<组件 IP>:<组件指标端>/metrics | grep 'TYPE\|HELP'

输出示例:

# HELP controller_runtime_active_workers 每个控制器当前使用的工作者数量
# TYPE controller_runtime_active_workers gauge
# HELP controller_runtime_max_concurrent_reconciles 每个控制器的最大并发调和数
# TYPE controller_runtime_max_concurrent_reconciles gauge
# HELP controller_runtime_reconcile_errors_total 每个控制器的总调和错误数
# TYPE controller_runtime_reconcile_errors_total counter
# HELP controller_runtime_reconcile_time_seconds 每个控制器调和所花费的时间长度

查看 Prometheus / VictoriaMetrics 存储的所有指标

您可以查看集群中可用的指标列表,以便根据这些指标编写所需的 PromQL。

前提条件

  1. 您已获取您的用户 Token。
  2. 您已获取平台地址。

操作步骤

运行以下命令,通过 curl 命令获取指标列表:

curl -k -X 'GET' -H 'Authorization: Bearer <您的 token>' 'https://<您的平台访问地址>/v2/metrics/<您的集群名称>/prometheus/label/__name__/values'

输出示例:

{
  "status": "success",
  "data": [
    "ALERTS",
    "ALERTS_FOR_STATE",
    "advanced_search_cached_resources_count",
    "alb_error",
    "alertmanager_alerts",
    "alertmanager_alerts_invalid_total",
    "alertmanager_alerts_received_total",
    "alertmanager_cluster_enabled"
  ]
}

查看平台定义的所有内置指标

为了简化用户使用,平台内置了大量常用指标。您可以在配置告警或监控面板时直接使用这些指标,而无需自行定义。以下将介绍如何查看这些指标。

前提条件

  1. 您已获取您的用户 Token。
  2. 您已获取平台地址。

操作步骤

运行以下命令,通过 curl 命令获取指标列表:

curl -k -X 'GET' -H 'Authorization: Bearer <您的 token>' 'https://<您的平台访问地址>/v2/metrics/<您的集群名称>/indicators'

输出示例:

[
  {
    "alertEnabled": true, 
    "annotations": {
      "cn": "计算组件下容器组的 CPU 使用率",
      "descriptionEN": "Cpu utilization for pods in workload",
      "descriptionZH": "计算组件下容器组的 CPU 使用率",
      "displayNameEN": "CPU utilization of the pods",
      "displayNameZH": "计算组件下容器组的 CPU 使用率",
      "en": "Cpu utilization for pods in workload",
      "features": "SupportDashboard", 
      "summaryEN": "CPU usage rate {{.externalLabels.comparison}}{{.externalLabels.threshold}} of Pod ({{.labels.pod}})",
      "summaryZH": "容器组 ({{.labels.pod}}) 的 CPU 使用率 {{.externalLabels.comparison}}{{.externalLabels.threshold}}"
    },
    "displayName": "计算组件下容器组的 CPU 使用率",
    "kind": "workload",
    "multipleEnabled": true,  
    "name": "workload.pod.cpu.utilization",
    "query": "avg by (kind,name,namespace,pod) (avg by (kind,name,namespace,pod,container)(cpaas_advanced_container_cpu_usage_seconds_total_irate5m{kind=~\"{{.kind}}\",name=~\"{{.name}}\",namespace=~\"{{.namespace}}\",container!=\"\",container!=\"POD\"}) / avg by (kind,name,namespace,pod,container)(cpaas_advanced_kube_pod_container_resource_limits{kind=~\"{{.kind}}\",name=~\"{{.name}}\",namespace=~\"{{.namespace}}\",resource=\"cpu\"}))", 
    "summary": "容器组 ({{.labels.pod}}) 的 CPU 使用率 {{.externalLabels.comparison}}{{.externalLabels.threshold}}",
    "type": "metric",
    "unit": "%",
    "legend": "{{.namespace}}/{{.pod}}",
    "variables": [ 
      "namespace",
      "name",
      "kind"
    ]
  }
]
  1. 是否支持配置告警时使用该指标
  2. 是否支持在监控面板中使用该指标
  3. 是否支持配置告警且指定多个资源时使用该指标
  4. 指标所定义的 PromQL 语句
  5. 指标的 PromQL 语句中可使用的变量

接入外部指标

除了平台内置的指标,您还可以通过 ServiceMonitorPodMonitor 接入您的应用或第三方应用所暴露的指标。本章节以同集群中以 pod 形式安装的 Elasticsearch Exporter 为例进行说明。

前提条件

您已安装您的应用,并通过指定接口暴露了指标。本文假设您的应用安装在 cpaas-system 命名空间下,并暴露了 http://<elasticsearch-exporter-ip>:9200/_prometheus/metrics 端点。

操作步骤

  1. 创建 Exporter 的 Service/Endpoint,用于暴露指标
apiVersion: v1
kind: Service
metadata:
  labels:
    chart: elasticsearch
    service_name: cpaas-elasticsearch
  name: cpaas-elasticsearch
  namespace: cpaas-system
spec:
  clusterIP: 10.105.125.99
  ports:
  - name: cpaas-elasticsearch
    port: 9200
    protocol: TCP
    targetPort: 9200
  selector:
    service_name: cpaas-elasticsearch
  sessionAffinity: None
  type: ClusterIP
  1. 创建一个 ServiceMonitor 对象,用于描述您的应用所暴露的指标:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: cpaas-monitor
    chart: cpaas-monitor
    heritage: Helm
    prometheus: kube-prometheus
    release: cpaas-monitor
  name: cpaas-elasticsearch-Exporter
  namespace: cpaas-system
spec:
  jobLabel: service_name
  namespaceSelector:
    any: true
  selector:
    matchExpressions:
    - key: service_name
      operator: Exists
  endpoints:
  - port: cpaas-elasticsearch
    path: /_prometheus/metrics
    interval: 60s
    honorLabels: true
    basicAuth:
      password:
        key: ES_PASSWORD
        name: acp-config-secret
      username:
        key: ES_USER
        name: acp-config-secret
  1. ServiceMonitor 要被同步到哪个 Prometheus;operator 会根据 Prometheus CR 的 serviceMonitorSelector 配置去监听对应的 ServiceMonitor 资源。如果 ServiceMonitor 的 labels 中没有被 Prometheus CR 的 serviceMonitorSelector 配置匹配到,这个 ServiceMonitor 不会被 operator 监听。
  2. operator 会根据 Prometheus CR 的 serviceMonitorNamespaceSelector 配置,监听哪些命名空间的 ServiceMonitor;如果 ServiceMonitor 不在 Prometheus CR 的 serviceMonitorNamespaceSelector 中,这个 ServiceMonitor 不会被 operator 监听。
  3. 采集到 Prometheus 的 Metrics 会添加一个 job 标签,值为 jobLabel 对应的 service label 的值。
  4. ServiceMonitor 根据 namespaceSelector 配置去匹配对应命名空间的 Service。
  5. ServiceMonitor 根据 selector 配置去匹配 Service。
  6. ServiceMonitor 根据 port 的配置去匹配 Service 的 port。
  7. Exporter 的访问路径,默认是 /metrics。
  8. Prometheus 抓取 Exporter 指标的间隔。
  9. 如果访问 Exporter 的路径需要认证,需要添加认证信息;同样支持 bearer token、tls 认证等方式。
  1. 检查 ServiceMonitor 是否被 Prometheus 监听

访问监控组件的 UI,查看是否存在 cpaas-elasticsearch-exporter 的 job。

  • Prometheus 的 UI 地址:https://<您的平台访问地址>/clusters/<集群名称>/prometheus-0/targets
  • VictoriaMetrics 的 UI 地址:https://<您的平台访问地址>/clusters/<集群名称>/vmselect/vmui/?#/metrics