搜索

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

download PDF

2.5.1. 关于 Prometheus

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

重要

红帽对 Prometheus 的支持仅限于红帽产品文档中提供的设置和配置建议。

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

2.5.1.1. Prometheus 查询

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

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

http_requests_total[5m]

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

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

2.5.1.2. 显示 Prometheus 数据的选项

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

  • 在 Prometheus 的表达式浏览器中,将 Prometheus 数据视为表格数据。
  • 通过 Prometheus HTTP API 的外部系统使用 Prometheus 数据。
  • 显示图形中的 Prometheus 数据。

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

    注意

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

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

2.5.2. 为 4.14 设置 Prometheus

要设置 Prometheus,请在集群中安装 Prometheus operator 自定义资源定义,然后将 Prometheus 添加到包含 Fuse 应用的 OpenShift 项目中。

先决条件

  • 集群管理员 对 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-7_13_0-00014-redhat-00001/monitoring/fuse-servicemonitor.yml -p NAMESPACE=<YOUR NAMESPACE> -p FUSE_SERVICE_NAME=<YOUR FUSE SERVICE> | oc apply -f -

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

    Example

    oc process -f https://raw.githubusercontent.com/jboss-fuse/application-templates/application-templates-2.1.0.fuse-7_13_0-00014-redhat-00001/monitoring/fuse-servicemonitor.yml -p NAMESPACE=myproject -p FUSE_SERVICE_NAME=myfuseapp | oc apply -f -

  2. 打开 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 环境变量
环境变量描述default

AB_PROMETHEUS_HOST

要绑定的主机地址。

0.0.0.0

AB_PROMETHEUS_OFF

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

启用 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. 重新构建并部署应用程序。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.