OpenShift 4 - 用 Prometheus 和 Grafana 监视用户应用定制的观测指标(视频)

news/2024/5/19 1:19:45 标签: openshift, prometheus, grafana

《OpenShift / RHEL / DevSecOps 汇总目录》
说明:本文已经在 OpenShift 4.13 的环境中验证

文章目录

  • OpenShift 的监控功能构成
  • 部署被监控应用
  • 用 OpenShift 内置功能监控应用
  • 用 Grafana 监控应用
    • 安装 Grafana 运行环境
    • 配置 Grafana 数据源
    • 定制监控 Dashboard
  • 演示视频
  • 参考

OpenShift 的监控功能构成

构成 OpenShift 监控功能的附件分为两部分:“平台监控组件” 和 “用户项目监控组件”。

  • 在平台监控组件中包括:Prometheus、Thanos Querier 和 Alertmanager 三部分重要组成,这些组件是由 Cluster Monitoring Operator 总体部署和管理生命周期的。通过平台监控组件可以对 OpenShift 集群的 DNS、日志系统、etcd、Kubelet、API Server、Scheduler 等重要环境进行监控。
  • 用户项目监控组件是对用户自有项目中应用资源进行监控。它由单独的 Prometheus、Thanos Ruler 构成,并共用平台的 Alertmanager 和 Thanos Querier 组件。
    在这里插入图片描述
    除了可以使用 OpenShift 控制台内置的监控功能和界面外,还可通过 Thanos Querier 的访问地址外接其他监控软件,例如使用 Grafana 定制的仪表盘显示 OpenShift 或用户应用的运行情况。

部署被监控应用

  1. 创建项目
$ oc new-project app-monitoring
  1. 部署测试应用
$ oc new-app quay.io/brancz/prometheus-example-app:v0.2.0 -l app=prometheus-example-app
  1. 创建 Service 和 Route。
$ cat << EOF | oc apply -f -
apiVersion: v1
kind: Service
metadata:
  labels:
    app: prometheus-example-app
  name: prometheus-example-app
spec:
  ports:
  - port: 8080
    protocol: TCP
    name: 8080-tcp
  selector:
    app: prometheus-example-app
  type: ClusterIP
EOF
 
$ oc expose svc prometheus-example-app
  1. 分别访问应用缺省地址和 /err 地址,返回的 HTTP 代码分别为 200 和 404。
$ curl -sw "%{http_code}\n" -o /dev/null $(oc get route prometheus-example-app -ojsonpath={.spec.host})
200
$ curl -sw "%{http_code}\n" -o /dev/null $(oc get route prometheus-example-app -ojsonpath={.spec.host})/err
404
  1. 访问应用的 /metrics 地址,查看应用返回的 HTTP 代码为 200 和 404 请求数量统计。
$ curl $(oc get route prometheus-example-app -ojsonpath={.spec.host})/metrics
# HELP http_requests_total Count of all HTTP requests
# TYPE http_requests_total counter
http_requests_total{code="200",method="get"} 1
http_requests_total{code="404",method="get"} 1
# HELP version Version information about this binary
# TYPE version gauge
version{version="v0.2.0"} 1

说明
也可在控制台上部署 quay.io/brancz/prometheus-example-app:v0.2.0 容器镜像,但需要增加 app=prometheus-example-app 标签,并且去掉 “安全路由” 选项。

用 OpenShift 内置功能监控应用

  1. 启用 OpenShift 对用户应用监控功能。
$ cat << EOF | oc apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-monitoring-config
  namespace: openshift-monitoring
data:
  config.yaml: |
    enableUserWorkload: true
EOF
  1. 确认主要监控服务云子运行正常。
$ oc get pod -n openshift-user-workload-monitoring
NAME                                   READY   STATUS    RESTARTS      AGE
prometheus-operator-77d547b4dc-fcflk   2/2     Running   0             34h
prometheus-user-workload-0             6/6     Running   0             34h
thanos-ruler-user-workload-0           4/4     Running   0             34h
  1. 创建对 prometheus-example-app 应用监控的 ServiceMonitor 对象。
$ cat << EOF | oc apply -f -
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: prometheus-example-monitor
  namespace: app-monitoring
spec:
  endpoints:
  - interval: 30s
    port: 8080-tcp
    path: /metrics
  selector:
    matchLabels:
      app: prometheus-example-app
EOF
  1. 创建完后在 OpenShift 的 “目标” 菜单中在 “过滤器” 中选择 “用户”,稍等后可以看到目标的监视端点。
    在这里插入图片描述
    该端点地址是运行应用的 Pod 使用的 IP 地址。
$ oc get pod -owide
NAME                                     READY   STATUS    RESTARTS   AGE     IP             NODE                 NOMINATED NODE   READINESS GATES
prometheus-example-app-b744f9c85-bmk7p   1/1     Running   0          6m15s   10.217.0.123   crc-2zx29-master-0   <none>           <none>
  1. 创建 PrometheusRule。下面的 expr 表达式会统计过去 5 分钟 HTTP 请求返回结果是 404 的每秒速率,如果 > 0.3 则报警。
$ cat << EOF | oc apply -f -
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: app-alert
  namespace: app-monitoring
spec:
  groups:
  - name: app-alert
    rules:
    - alert: HttpRequestErrorRateIncrease
      expr: rate(http_requests_total{code="404",job="prometheus-example-app"}[5m]) > 0.3
      labels:
        severity: warning
      annotations:
        summary: Prometheus example app's error rate increase.
        message: Prometheus example app's error rate increase.
EOF

创建完后可以在“报警” 菜单中的 “报警规则” 页面中通过将 “过滤器” 中选择 “用户”,可以看到该报警规则。
在这里插入图片描述

  1. 执行以下命令,持续访问应用的 /err 地址。
$ for i in `seq 1 10000`
do
  curl -sw "%{http_code}\n" -o /dev/null $(oc get route prometheus-example-app -ojsonpath={.spec.host})/err
  sleep 1
done
  1. 在 OpenShift 的 “指标” 页面中先将输入查询条件设为 “rate(http_requests_total{code=“404”,job=“prometheus-example-app”}[5m])”,然后点击 “运行查询”。再将时间设为 5m,并将页面刷新时间设置为 15秒。在等待一会儿后可以看到值已经超过 0.3。
    在这里插入图片描述
  2. 在 OpenShift 的 “报警” 页面中的 “过滤器” 中选中 “用户” ,确认已经被触发。
    在这里插入图片描述
  3. 点击上图的 HttpRequestErrorRateIncrease,然后可以看到和步骤 7 类似的指标监控图。
    在这里插入图片描述
  4. 另外,在 app-monitoring 项目中也可以看到 HttpRequestErrorRateIncrease 报警。
    在这里插入图片描述

用 Grafana 监控应用

安装 Grafana 运行环境

  1. 创建 my-grafana 项目,然后在其中安装 Grafana Operator v4 版本。
    在这里插入图片描述
  2. 执行以下命令,创建一个名为 my-grafana 的 Grafana 实例。注意:以下 YAML 中的 dataStorage 使用了 OpenShift 缺省的存储类。
$ cat << EOF | oc apply -f -
apiVersion: integreatly.org/v1alpha1
kind: Grafana
metadata:
  name: my-grafana
  namespace: my-grafana
spec:
  config:
    security:
      admin_user: admin
      admin_password: my-password
  dataStorage:
    accessModes:
      - ReadWriteOnce
    size: 1Gi
  ingress:
    enabled: true
    tls:
      enabled: true
EOF
  1. 创建名为 grafana-view 的 clusterrolebinding,为所有命名空间的 grafana-serviceaccount 提供 cluster-monitoring-view 角色。
$ oc create clusterrolebinding grafana-view --clusterrole=cluster-monitoring-view --serviceaccount=my-grafana:grafana-serviceaccount

配置 Grafana 数据源

  1. 执行命令创建一个 GrafanaDataSource,其中使用了基于 grafana-serviceaccoun 的 token 来访问 Thanos Querier。
$ TOKEN=$(oc create token grafana-serviceaccount -n my-grafana)
$ cat << EOF | oc apply -f -
apiVersion: integreatly.org/v1alpha1
kind: GrafanaDataSource
metadata:
  name: prometheus
  namespace: my-grafana
spec:
  datasources:
    - basicAuthUser: internal
      access: proxy
      editable: true
      secureJsonData:
        httpHeaderValue1: >-
          Bearer ${TOKEN}
      name: Prometheus
      url: 'https://thanos-querier.openshift-monitoring.svc.cluster.local:9091'
      jsonData:
        httpHeaderName1: Authorization
        timeInterval: 5s
        tlsSkipVerify: true
      basicAuth: false
      isDefault: true
      version: 1
      type: prometheus
  name: test_name
EOF
  1. 访问 grafana-route 对应的 Grafana 页面,然后使用创建 my-grafana 实例时指定的用户和密码登录。
    在这里插入图片描述
  2. 进入 Configuration 的 Data Sources 菜单,可以看到名为 Prometheus 的数据源,点击进入。
    在这里插入图片描述
  3. 点击页面下方的 “ Save & Test”,确认显示 Data srouce is working。
    在这里插入图片描述

定制监控 Dashboard

  1. 进入 Create 的 Dashboard 菜单。
    在这里插入图片描述
  2. 在 New dashboard 页面点击 Add an empty panel。
    在这里插入图片描述
  3. 在下图中为 Metrics 提供以下内容,然后将 Panel title 设为 Example App Err Rate,最后点击 Apply 按钮。
rate(http_requests_total{code="404",job="prometheus-example-app"}[5m])

在这里插入图片描述
4. 在 Dashboard 页面点击右上方的 Dashboard settings 图标。
在这里插入图片描述
5. 设置 Name,然后保存。
在这里插入图片描述
6. 最后通过定制的 Dashboard 监控的应用指标如下图。
在这里插入图片描述

演示视频

演示视频

参考

https://github.com/k-srkw/openshift-monitoring-handson/blob/main/monitoring-handson.md
https://cloud.redhat.com/blog/your-guide-to-openshift-observability-part-1
https://access.redhat.com/solutions/5335491
https://access.redhat.com/documentation/en-us/openshift_container_platform/4.5/html/monitoring/monitoring-your-own-services
https://catalog.workshops.aws/aws-openshift-workshop/en-US/8-observability/2-metrics/5-app-dashboard
https://github.com/brancz/prometheus-example-app
https://developers.redhat.com/articles/2023/08/08/how-monitor-workloads-using-openshift-monitoring-stack#how_to_monitor_a_sample_application
https://shonpaz.medium.com/monitor-your-application-metrics-using-the-openshift-monitoring-stack-862cb4111906
https://github.com/OpenShiftDemos/openshift-ops-workshops/blob/ocp4-dev/workshop/content/monitoring-basics.adoc
https://github.com/pittar/openshift-user-workload-monitoring
https://github.com/alvarolop/quarkus-observability-app/blob/main/README.adoc
https://prometheus.io/docs/prometheus/latest/querying/basics/
https://github.com/alvarolop/quarkus-observability-app


http://www.niftyadmin.cn/n/4977516.html

相关文章

package.json 详解

文章目录 package.json1. name2. version3. description4. homepage5. bugs6. license7. author, contributors8. funding9. files10. main11. module12. browser13. bin14. man15. directories15.1 directories.bin15.2 directories.man 16. repository17. scripts18. config1…

A Survey on Large Language Model based Autonomous Agents

本文是LLM系列的文章&#xff0c;针对《A Survey on Large Language Model based Autonomous Agents》的翻译。 基于大模型的自动agents综述 摘要1 引言2 基于LLM的自动代理构建3 基于LLM的自动代理应用4 基于LLM的自动代理评估5 相关综述6 挑战6.1 角色扮演能力6.2 广义与人对…

教师必看!如何最大化利用和鲸的资源与平台赋能教学工作

本篇内容面向教授数据科学、大数据、人工智能专业课及相关交叉课程的高校教师&#xff0c;提供了一份资源导览地图&#xff0c;跟随此地图浏览您可以获得&#xff1a; 可作为教学素材的 5万 开源案例及近万份精选数据资源可作为教学拓展的各类训练营、Workshop、数据科学竞赛等…

智慧工厂解决方案:推动制造业转型升级的新引擎

随着信息技术的迅猛发展和制造业竞争的加剧&#xff0c;智慧工厂成为了推动制造业转型升级的重要引擎。智慧工厂解决方案通过整合物联网、人工智能、大数据分析等先进技术&#xff0c;实现生产过程的智能化、自动化和高效化&#xff0c;为企业提供了更加灵活、智能的生产模式和…

ThinkPHP 多应用配置,及不同域名访问不同应用的配置【详解】

ThinkPHP 多应用配置&#xff0c;及不同域名访问不同应用的配置【详解】 一、安装多应用扩展二、删除项目的中默认的controller文件夹三、创建多应用四、修改config/app.php文件五、测试并且访问多应用六、配置小皮&#xff0c;不同域名访问不同的应用七、小结 一、安装多应用扩…

java中使用sockjs、stomp完成websocket通信

主要配置 import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary;import org.…

显著性检验(Significance Test)

参考链接&#xff1a;Click Here 显著性检验&#xff08;Significance Test&#xff09;主要分为两个类别&#xff1a; Statistical Significance Test (统计显著性检验) 计量方式&#xff1a;p-value < 0.05 目的&#xff1a;检验原始分布与目标分布之间是否具有显著差异性…

【C语言每日一题】03. 对齐输出

题目来源&#xff1a;http://noi.openjudge.cn/ch0101/03/ 03 对齐输出 总时间限制: 1000ms 内存限制: 65536kB 问题描述 读入三个整数&#xff0c;按每个整数占8个字符的宽度&#xff0c;右对齐输出它们。 输入 只有一行&#xff0c;包含三个整数&#xff0c;整数之间以一…