1.4. 定制可观察性

以下部分介绍了对可观察性服务所收集的数据进行自定义、管理和查看的信息。

使用 must-gather 命令收集有关为可观察性资源创建的新信息的日志。如需更多信息,请参阅故障排除文档中的 Must-gather 部分。

1.4.1. 创建自定义规则

您可以通过在可观察性资源中添加 Prometheus 记录规则警报规则,为可观察性安装创建自定义规则。如需更多信息,请参阅 Prometheus 配置

:您只能针对从所有受管集群收集的指标创建自定义规则。查看通过运行以下命令收集的指标数据列表:kubectl describe cm observability-metrics-whitelist

使用 Prometheus 定义自定义规则来创建警报条件,并将通知发送到外部消息服务。完成以下步骤以创建自定义规则:

  1. 登录到您的 Red Hat Advanced Cluster Management hub 集群。
  2. open-cluster-management-observability 命名空间中创建一个名为 thanos-rule-custom-rules 的 ConfigMap。键必须被命名为 thanos-ruler-custom-rules.yaml,如下例所示。您可以在配置中创建多个规则:

    默认情况下,开箱即用的警报规则是在 open-cluster-management-observability 命名空间中的 ConfigMap 中定义的。

    例如,您可以创建一个自定义警报规则,在 CPU 使用量超过了您定义的值时通知您:

    data:
      custom_rules.yaml: |
        groups:
          - name: cluster-health
            rules:
            - alert: ClusterCPUHealth-jb
              annotations:
                summary: Notify when CPU utilization on a cluster is greater than the defined utilization limit
                description: "The cluster has a high CPU usage: {{ $value }} core for {{ $labels.cluster }} {{ $labels.clusterID }}."
              expr: |
                max(cluster:cpu_usage_cores:sum) by (clusterID, cluster, prometheus) > 0
              for: 5s
              labels:
                cluster: "{{ $labels.cluster }}"
                prometheus: "{{ $labels.prometheus }}"
                severity: critical

    :如果这是第一个新的自定义规则,它会立即创建。对于 ConfigMap 的更改,您必须使用以下命令重启 observability pod:kubectl rollout restart statefulset observability-observatorium-thanos-rule -n open-cluster-management-observability

  3. 如果要验证警报规则是否正常工作,请完成以下步骤:

    1. 访问 Grafana 仪表板,然后选择 Explore 图标。
    2. 在 Metrics 探索栏中,输入 "ALERTS" 并运行查询。系统中所有处于 pending 或 firing 状态的 ALERTS 都会被显示。
    3. 如果没有显示警报,查看规则来检查表达式是否正确。

已创建一个自定义规则。

1.4.1.1. 为 AlertManager 配置规则

集成外部消息工具,如 email、Slack 和 PagerDuty 以接收来自 AlertManager 的通知。您必须覆盖 open-cluster-management-observability 命名空间中的 alertmanager-config secret 来添加集成,并为 AlertManager 配置路由。完成以下步骤以更新自定义接收器规则:

  1. alertmanager-config secret 中提取数据。运行以下命令:

    oc -n open-cluster-management-observability get secret alertmanager-config --template='{{ index .data "alertmanager.yaml" }}' |base64 -d > alertmanager.yaml
  2. 运行以下命令,编辑并保存 alertmanager.yaml 文件配置:

    oc -n open-cluster-management-observability create secret generic alertmanager-config --from-file=alertmanager.yaml --dry-run -o=yaml |  oc -n open-cluster-management-observability replace secret --filename=-

    更新的 secret 可能与以下类似:

    global
      smtp_smarthost: 'localhost:25'
      smtp_from: 'alertmanager@example.org'
      smtp_auth_username: 'alertmanager'
      smtp_auth_password: 'password'
    templates:
    - '/etc/alertmanager/template/*.tmpl'
    route:
      group_by: ['alertname', 'cluster', 'service']
      group_wait: 30s
      group_interval: 5m
      repeat_interval: 3h
      receiver: team-X-mails
      routes:
      - match_re:
          service: ^(foo1|foo2|baz)$
        receiver: team-X-mails

您的更改会在修改后立即生效。如需 AlertManager 的示例,请参阅 prometheus/alertmanager