2.5. 配置 Prometheus 以监控 OpenShift 中的 Fuse 应用程序

2.5.1. 关于 Prometheus

Prometheus 是一个开源系统和服务监控与警报工具包,可用于监控 Red Hat OpenShift 环境中部署的服务。Prometheus 以给定间隔从配置的服务收集和存储指标,评估规则表达式,显示结果,并在指定条件变为 true 时触发警报。

重要

Red Hat support for Prometheus 仅限于 Red Hat 产品文档中提供的设置和配置建议。

要监控 OpenShift 服务,您必须配置每个服务,以便为 Prometheus 格式公开端点。此端点是一个 HTTP 接口,提供指标列表和指标的当前值。Prometheus 定期提取每个目标定义的端点,并将收集的数据写入其数据库。Prometheus 会延长数据,而不只收集当前正在运行的会话。Prometheus 存储数据,以便您可以视觉化呈现并在数据上运行查询。

2.5.1.1. Prometheus 查询

在 Prometheus Web 界面中,您可以在 Prometheus Query Language(PromQL) 中写入查询以选择和聚合收集的数据。

例如,您可以使用以下查询来选择 Prometheus 在最后五分钟内记录的、所有值为 http_requests_total 作为指标名称的所有时间序列数据:

http_requests_total[5m]

要进一步定义或过滤查询的结果,请为指标指定一个标签( key:value 对)。例如,您可以使用以下查询来选择 Prometheus 在最后五分钟内记录的所有值,所有时间序列数据都设为 http_requests_total,并将作业标签设置为 集成

http_requests_total{job="integration"}[5m]

2.5.1.2. 显示 Prometheus 数据的选项

您可以指定 Prometheus 如何处理查询的结果:

  • 在 Prometheus 的表达式浏览器中以表格数据的形式查看 Prometheus 数据。
  • 通过 Prometheus HTTP API 从外部系统消耗 Prometheus 数据。
  • 在图表中显示 Prometheus 数据。

    Prometheus 提供它收集的数据的默认图形视图。如果您希望使用更强大的图形仪表板来查看 Prometheus 数据,则 Grafana 是一个流行选择。

    注意

    Grafana 是社区支持的功能。部署 Grafana 来监控红帽产品不受 Red Hat 产品服务等级协议(SLA)的支持。

您还可以使用 PromQL 语言在 Prometheus 的 Alertmanager 工具中配置警报

2.5.2. 为 4.13 设置 Prometheus

要设置 Prometheus,请在集群中安装 Prometheus,然后将 Prometheus 添加到包含 Fuse 应用程序的 OpenShift 项目中。

先决条件

  • 具有 集群 admin 访问权限,可以访问 OpenShift 集群。
  • 您已通过在 OpenShift 镜像和模板上安装 Fuse 来准备 OpenShift 集群,如 OpenShift 中的 Fuse 指南中所述
  • 您已在集群中创建了 OpenShift 项目,并在其中添加一个 Fuse 应用。

流程

  1. 使用管理员权限登录到 OpenShift:

    oc login --user system:admin --token=my-token --server=https://my-cluster.example.com:6443
  2. 创建名为 cluster-monitoring-config.yml 的文件:

    apiVersion: v1
    kind: ConfigMap
    metadata:
     name: cluster-monitoring-config
     namespace: openshift-monitoring
    data:
     config.yaml: |
       enableUserWorkload: true
  3. 将集群监控配置应用到 "openshift-monitoring" 命名空间:

    oc create -f cluster-monitoring-config.yml -n openshift-monitoring

服务监控器 包含 Prometheus 在 OpenShift Container Platform 上收集给定服务和项目(命名空间)的指标的说明。

  1. 要安装服务监控器:+
oc process -f https://raw.githubusercontent.com/jboss-fuse/application-templates/application-templates-2.1.0.fuse-sb2-7_11_1-00016-redhat-00002/monitors/fuse-servicemonitor.yml -p NAMESPACE=<your-fuse-namespace> -p FUSE_SERVICE_NAME=<fuse-app-name> | oc apply -f -

+ 例如,带有名为 myproject 的 OpenShift 项目(命名空间),其中包含名为 myfuseapp 的 Fuse 应用程序:

+

示例

oc process -f https://raw.githubusercontent.com/jboss-fuse/application-templates/application-templates-2.1.0.fuse-sb2-7_11_1-00016-redhat-00002/monitors/fuse-servicemonitor.yml -p NAMESPACE=myproject -p FUSE_SERVICE_NAME=myfuseapp | oc apply -f -

  1. 打开 Prometheus 仪表板:

    1. 登录 OpenShift 控制台。
    2. 打开您向其添加 Prometheus 的项目。
    3. 在左侧窗格中,选择 Administrator 视图并打开 Observe -> Metrics
    4. 点 Prometheus Hostname URL 打开 Prometheus 仪表板。

有关 Prometheus 的更多信息,请参阅 Prometheus 文档

2.5.3. OpenShift 环境变量

要配置应用程序的 Prometheus 实例,您可以设置 表 2.2 “Prometheus 环境变量” 中列出的 OpenShift 环境变量。

表 2.2. Prometheus 环境变量

环境变量描述默认

AB_PROMETHEUS_HOST

要绑定的主机地址。

0.0.0.0

AB_PROMETHEUS_OFF

如果设置,禁用 Prometheus 激活( echoes a空值)。

Prometheus 已启用。

AB_PROMETHEUS_PORT

要使用的端口。

9779

AB_JMX_EXPORTER_CONFIG

使用文件(包括路径)作为 Prometheus 配置文件。

带有 Camel 指标的 /opt/prometheus/prometheus-config.yml 文件。

AB_JMX_EXPORTER_OPTS

附加到 JMX 导出器配置的其他选项。

不适用。

其他资源

有关为 pod 设置环境变量的详情,请参阅 OpenShift 开发人员指南 (https://access.redhat.com/documentation/zh-cn/openshift_container_platform/3.11/html/developer_guide/)。

2.5.4. 控制 Prometheus 监控并收集的指标

默认情况下,Prometheus 使用配置文件 https://raw.githubusercontent.com/jboss-fuse/application-templates/master/prometheus/prometheus-config.yml,其中包含由 Camel 公开的所有指标。

如果您在需要 Prometheus 来监控并收集的应用程序中有一个自定义指标(例如,应用程序进程的订购数量),您可以使用自己的配置文件。请注意,您可以识别的指标仅限于在 JMX 中提供的指标。

流程

要使用自定义配置文件来公开默认 Prometheus 配置未涵盖的 JMX Bean,请按照以下步骤操作:

  1. 创建自定义 Prometheus 配置文件。您可以使用默认文件内容(prometheus-config.yml https://raw.githubusercontent.com/jboss-fuse/application-templates/master/prometheus/prometheus-config.yml)作为格式的指南。

    您可以将任何名称用于自定义配置文件,例如: my-prometheus-config.yml

  2. 将 prometheus 配置文件(如 my-prometheus-config.yml)添加到应用程序的 src/main/jkube-includes 目录中。
  3. 在应用程序中创建 src/main/jkube/deployment.xml 文件,并为 AB_JMX_EXPORTER_CONFIG 环境变量添加一个条目,并将其值设为您的配置文件。例如:

    spec:
      template:
        spec:
          containers:
            -
              resources:
                requests:
                  cpu: "0.2"
                limits:
                  cpu: "1.0"
              env:
              - name: SPRING_APPLICATION_JSON
                value: '{"server":{"tomcat":{"max-threads":1}}}'
              - name: AB_JMX_EXPORTER_CONFIG
                value: "my-prometheus-config.yml"

    此环境变量适用于 pod 级别的应用程序。

  4. 重新构建并部署您的应用程序。