指标管理

WARNING

本页示例中使用的 /v2/metrics 端点已在 ACP 4.3.3 中废弃,并计划在未来版本中移除。请改用 /platform/monitoring.alauda.io/v1beta1 端点。更多信息请参见 Monitoring APIs

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

查看平台组件暴露的指标

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

curl -s http://<Component IP>:<Component metrics port>/metrics | grep 'TYPE\|HELP'

示例输出:

# HELP controller_runtime_active_workers Number of currently used workers per controller
# TYPE controller_runtime_active_workers gauge
# HELP controller_runtime_max_concurrent_reconciles Maximum number of concurrent reconciles per controller
# TYPE controller_runtime_max_concurrent_reconciles gauge
# HELP controller_runtime_reconcile_errors_total Total number of reconciliation errors per controller
# TYPE controller_runtime_reconcile_errors_total counter
# HELP controller_runtime_reconcile_time_seconds Length of time per reconciliation per controller

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

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

前提条件

  1. 您已获取用户 Token

  2. 您已获取平台地址

操作步骤

使用 curl 命令运行以下命令以获取指标列表:

curl -k -X 'GET' -H 'Authorization: Bearer <Your token>' 'https://<Your platform access address>/v2/metrics/<Your cluster name>/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 <Your token>' 'https://<Your platform access address>/v2/metrics/<Your cluster name>/indicators'

示例输出:

[
  {
  "alertEnabled": true, 
  "annotations": {
   "cn": "CPU utilization of containers in the compute component",
   "descriptionEN": "Cpu utilization for pods in workload",
   "descriptionZH": "CPU utilization of containers in the compute component",
   "displayNameEN": "CPU utilization of the pods",
   "displayNameZH": "CPU utilization of containers in the compute component",
   "en": "Cpu utilization for pods in workload",
   "features": "SupportDashboard", 
   "summaryEN": "CPU usage rate {{.externalLabels.comparison}}{{.externalLabels.threshold}} of Pod ({{.labels.pod}})",
   "summaryZH": "CPU usage rate {{.externalLabels.comparison}}{{.externalLabels.threshold}} of pod ({{.labels.pod}})"
  },
  "displayName": "CPU utilization of containers in the compute component",
  "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": "CPU usage rate {{.externalLabels.comparison}}{{.externalLabels.threshold}} of pod ({{.labels.pod}})",
  "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
  2. 创建一个 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 的标签与 Prometheus CR 的 serviceMonitorSelector 配置不匹配,则 operator 不会监控该 ServiceMonitor。
    2. operator 将根据 Prometheus CR 的 serviceMonitorNamespaceSelector 配置监听哪些命名空间中的 ServiceMonitor;如果该 ServiceMonitor 不在 Prometheus CR 的 serviceMonitorNamespaceSelector 中,则 operator 不会监控该 ServiceMonitor。
    3. Prometheus 收集到的指标会新增一个 job 标签,其值为 jobLabel 对应的 service 标签值。
    4. ServiceMonitor 根据 namespaceSelector 配置匹配对应的 Service。
    5. ServiceMonitor 根据 selector 配置匹配 Service。
    6. ServiceMonitor 根据 port 配置匹配 Service 的端口。
    7. Exporter 的访问路径,默认为 /metrics。
    8. Prometheus 抓取 Exporter 指标的间隔。
    9. 如果访问 Exporter 路径需要认证,则需要添加认证信息;也支持 bearer token、tls 认证等方式。
  3. 检查 ServiceMonitor 是否已被 Prometheus 监控

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

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