k8s1.23 部署Prometheus-Operator集群监控

news2024/9/21 16:45:22

1. Prometheus-Operator介绍

Prometheus Operator 为 Kubernetes 提供了对 Prometheus 相关监控组件的本地部署和管理方案,该项目的目的是为了简化和自动化基于 Prometheus 的监控栈配置,主要包括以下几个功能:

  • kubernetes自定义资源:使用kubernetes CRD 来部署和管理Prometheus,Alertmanager和相关组件
  • 简化的部署配置:直接通过kubernetes资源清单配置Prometheus,比如版本,持久化,副本,保留策略等等配置
  • Prometheus监控目标配置:基于熟知的kubernetes标签查询自动生成监控目标配置,无需学习prometheus特地的配置

1.1. prometheus-operator组织架构

下图是 Prometheus-Operator 官方提供的架构图,各组件以不同的方式运行在 Kubernetes 集群中,其中 Operator 是最核心的部分,作为一个控制器,它会去创建 Prometheus、ServiceMonitor、AlertManager以及 PrometheusRule 等 CRD 资源对象,然后会一直 Watch 并维持这些资源对象的状态。

下面三个yaml文件 很好的表述了,prometheus 如何关联选择 servicemonitor,servicemonitor 如何关联选择目标service。

为了能让prom监控k8s内的应用,Prometheus-Operator通过配置servicemonitor匹配到由service对象自动填充的Endpoints,并配置prometheus监控这些Endpoints后端的pods,ServiceMonitor.Spec的Endpoints部分就是用于配置Endpoints的哪些端口将被scrape指标。

servicemonitor对象很巧妙,它解耦了“监控的需求”和“需求的实现方”。servicemonitor 只需要用到label-selector 这种简单又通用的方式声明一个 “监控需求”,也就是哪些Endpoints 需要搜集,怎么收集就行了。让用户只关心需求,这是一个非常好的关注点分离。当然servicemonitor 最后还是会被operator转化为原始的复 杂的scrape config,但这个复杂度已经完全被operator屏蔽了。

1.2. prometheus告警对接流程

下图很好的展现了prometheus在配置报警时需要操作哪些资源,及各资源起到的作用

首先通过配置servicemonitor/podmonitor来获取应用的监控指标;

Prometheus.spec.alerting字段会匹配Alertmanager中的配置,匹配到alertmanager实例

然后通过prometheusrule对监控到的指标配置报警规则;

最后配置告警接收器,配置alertmanagerconfig来配置如何处理告警,包括如何接收、路由、抑制和发送警报等;

1.3. 常见CRD

Prometheus:

定义了所需的 Prometheus 部署。

ServiceMonitor:

以声明方式指定应如何监控 Kubernetes 服务组。Operator 根据 API 服务器中对象的当前状态自动生成 Prometheus 抓取配置。

PodMonitor:

以声明方式指定应如何监控 pod 组。Operator 根据 API 服务器中对象的当前状态自动生成 Prometheus 抓取配置。

PrometheusRule:

定义了一组所需的 Prometheus 警报和/或记录规则。Operator 生成一个规则文件,可供 Prometheus 实 例使用。

Alertmanager:

定义了所需的 Alertmanager 部署。

AlertmanagerConfig:

以声明方式指定 Alertmanager 配置的子部分,允许将警报路由到自定义接收器并设置禁止规则。

Probe:

以声明方式指定应如何监视入口组或静态目标。Operator 根据定义自动生成 Prometheus scrape 配置。 配合blackbox exporter使用。

ThanosRuler:

定义了所需的 Thanos Ruler 部署。

2. 克隆Prometheus Operator

Prometheus-Operator对K8S集群的版本有要求,请参照集群版本选择对应Prometheus-Operator版本代码库:https://github.com/prometheus-operator/kube-prometheus

k8s1.23支持的最高版本是 kube-prometheus release-0.11

https://codeload.github.com/prometheus-operator/kube-prometheus/tar.gz/refs/tags/v0.11.0

[root@k8s231 kube-prometheus-0.11.0]# pwd
/script/k8s
[root@k8s231 k8s]# tar -xvf kube-prometheus-0.11.0.tar.gz
[root@k8s231 k8s]# cd kube-prometheus-0.11.0

3. 替换镜像路径

国内无法访问谷歌镜像,这里需要替换

[root@k8s231 kube-prometheus-0.11.0]# grep k8s.gcr.io manifests/*
manifests/kubeStateMetrics-deployment.yaml:        image: k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.5.0
manifests/prometheusAdapter-deployment.yaml:        image: k8s.gcr.io/prometheus-adapter/prometheus-adapter:v0.9.1

sed -i 's#k8s.gcr.io.*#bitnami/kube-state-metrics:latest#g' manifests/kubeStateMetrics-deployment.yaml
sed -i 's#k8s.gcr.io.*#v5cn/prometheus-adapter:v0.9.1#g' manifests/prometheusAdapter-deployment.yaml

4. 修改server为nodeport向外暴露服务

[root@k8s231 kube-prometheus-0.11.0]# cat manifests/grafana-service.yaml 
...
spec:
  type: NodePort #新增
  ports:
  - name: http
    port: 3000
    targetPort: http
    nodePort: 30100 #新增
...

[root@k8s231 kube-prometheus-0.11.0]# cat manifests/prometheus-service.yaml
...
spec:
  type: NodePort #新增
  ports:
  - name: web
    port: 9090
    targetPort: web
    nodePort: 30200  #新增
...

5. 安装

kubectl apply --server-side -f manifests/setup
#检查各资源是否正常安装
kubectl wait --for condition=Established --all CustomResourceDefinition --namespace=monitoring
kubectl apply -f manifests/

6. 卸载

注意:如果按照有问题,可以执行此步,再重新安装,若没错请不要执行此步骤,除非你真的想卸载不要了

kubectl delete --ignore-not-found=true -f manifests/ -f manifests/setup

7. 部署prometheus-operator

[root@k8s231 kube-prometheus-0.11.0]# kubectl create -f manifests/setup/
[root@k8s231 kube-prometheus-0.11.0]# kubectl apply -f manifests/

8. 检查部署情况

[root@k8s231 manifests]# kubectl -n monitoring get pods
NAME                                  READY   STATUS    RESTARTS   AGE
alertmanager-main-0                   2/2     Running   0          16m
alertmanager-main-1                   2/2     Running   0          16m
alertmanager-main-2                   2/2     Running   0          16m
blackbox-exporter-746c64fd88-dh9l8    3/3     Running   0          16m
grafana-5fc7f9f55d-kdjrx              1/1     Running   0          16m
kube-state-metrics-698467b7df-cm92d   3/3     Running   0          16m
node-exporter-2rdw4                   2/2     Running   0          16m
node-exporter-2sdjn                   2/2     Running   0          16m
node-exporter-8gpmr                   2/2     Running   0          16m
node-exporter-gvz82                   2/2     Running   0          16m
prometheus-adapter-5597544b8b-w7f2d   1/1     Running   0          98s
prometheus-adapter-5597544b8b-wtzm7   1/1     Running   0          98s
prometheus-k8s-0                      2/2     Running   0          16m
prometheus-k8s-1                      2/2     Running   0          16m
prometheus-operator-f59c8b954-tmk2d   2/2     Running   0          16m
[root@k8s231 kube-prometheus-0.11.0]# kubectl -n monitoring get svc
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE
alertmanager-main       ClusterIP   10.100.5.141     <none>        9093/TCP,8080/TCP               4m35s
alertmanager-operated   ClusterIP   None             <none>        9093/TCP,9094/TCP,9094/UDP      4m10s
blackbox-exporter       ClusterIP   10.100.237.159   <none>        9115/TCP,19115/TCP              4m35s
grafana                 NodePort    10.100.240.15    <none>        3000:30100/TCP                  4m34s
kube-state-metrics      ClusterIP   None             <none>        8443/TCP,9443/TCP               4m34s
node-exporter           ClusterIP   None             <none>        9100/TCP                        4m34s
prometheus-adapter      ClusterIP   10.100.55.29     <none>        443/TCP                         4m33s
prometheus-k8s          NodePort    10.100.122.67    <none>        9090:30200/TCP,8080:65345/TCP   4m33s
prometheus-operated     ClusterIP   None             <none>        9090/TCP                        4m9s
prometheus-operator     ClusterIP   None             <none>        8443/TCP                        4m33s

9. 访问UI页面

9.1. 访问grafana

地址:10.0.0.231:30100  账号密码:admin/admin

9.2. 访问prometheus

地址:10.0.0.231:30200

10. 案例:使用servicemonitor监控es

ES环境说明:当前已经存在一个运行在elk名称空间的ES集群,es的svc名称为es-svc-headless

这里servicemonitor和exporter处于monitoring的命名空间,和es集群处于不同空间

10.1. 创建一个elasticsearch exporter的pod及他的svc

cat > elastic-exporter.yaml <<EOF
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: elasticsearch-exporter
  name: elasticsearch-exporter
  namespace: monitoring 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch-exporter
  template:
    metadata:
      annotations:
        prometheus.io/scrape: 'true'  
        prometheus.io/port: '9114'
        prometheus.io/path: 'metrics'
      labels:
        app: elasticsearch-exporter
    spec:
      containers:
      - command:
        - '/bin/elasticsearch_exporter'       
        - --es.uri=http://elastic:Aa123456@es-svc-headless.elk:9200
        - --es.all
        - --es.indices
        - --es.shards
        image: prometheuscommunity/elasticsearch-exporter:v1.5.0
        imagePullPolicy: IfNotPresent
        name: elasticsearch-exporter
        ports:
        - containerPort: 9114

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: elasticsearch-exporter
  name: elasticsearch-exporter-svc
  namespace: monitoring 
spec:
  ports:
  - name: http
    port: 9114
    protocol: TCP
    targetPort: 9114
  selector:
    app: elasticsearch-exporter
  type: ClusterIP
EOF
kubectl apply -f elastic-exporter.yaml

10.2. 创建servicemonitor 关联exporter的svc

operator会自动添加同名称空间的servicemonitor

[root@k8s231 k8s]# cat > elastic-exporter-servicemonitor.yaml <<'EOF'
[root@k8s231 k8s]# cat elastic-exporter-servicemonitor.yaml 
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: elasticsearch-exporter
  name: elasticsearch-exporter
  namespace: monitoring 
spec:
  endpoints:
    - honorLabels: true
      interval: 1m
      path: /metrics
      port: http
      scheme: http
      params:
        target:
          - 'elasticsearch-exporter-svc'
      relabelings:
        - sourceLabels: [__param_target]
          targetLabel: instance
  namespaceSelector:
    matchNames:
      - monitoring 
  selector:
    matchLabels:
      app: elasticsearch-exporter
EOF
kubectl apply -f elastic-exporter-servicemonitor.yaml

10.3. 验证servicemonitor在没在prometheus里面出现

10.4. Grafana展示图

添加prometheus数据源,然导入官方推荐监控模板ID为:14191,然后数据源选择为prometheus

10.5. 给elasticsearch设置prometheus告警规则

prometheusRule规则配置,可以参考模板配置,模板网址如下:

https://awesome-prometheus-alerts.grep.to/rules#elasticsearch

cat > elastic-promhteus-alert.yaml <<'EOF'
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  labels:
    prometheus: k8s
    role: alert-rules
  name: elasticsearch-exporter-rules
  namespace: monitoring
spec:
  groups:
    - name: elasticsearch-exporter
      rules:
        - alert: es-ElasticsearchHealthyNodes
          expr: elasticsearch_cluster_health_number_of_nodes < 3
          for: 0m
          labels:
            severity: critical
          annotations:
            summary: Elasticsearch Healthy Nodes (instance {{ $labels.instance }})
            description: "Missing node in Elasticsearch cluster\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
        - alert: es-ElasticsearchClusterRed
          expr: elasticsearch_cluster_health_status{color="red"} == 1
          for: 0m
          labels:
            severity: critical
          annotations:
            summary: Elasticsearch Cluster Red (instance {{ $labels.instance }})
            description: "Elastic Cluster Red status\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
        - alert: es-ElasticsearchClusterYellow
          expr: elasticsearch_cluster_health_status{color="yellow"} == 1
          for: 0m
          labels:
            severity: warning
          annotations:
            summary: Elasticsearch Cluster Yellow (instance {{ $labels.instance }})
            description: "Elastic Cluster Yellow status\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
        - alert: es-ElasticsearchDiskOutOfSpace
          expr: elasticsearch_filesystem_data_available_bytes / elasticsearch_filesystem_data_size_bytes * 100 < 10
          for: 0m
          labels:
            severity: critical
          annotations:
            summary: Elasticsearch disk out of space (instance {{ $labels.instance }})
            description: "The disk usage is over 90%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
        - alert: es-ElasticsearchHeapUsageTooHigh
          expr: (elasticsearch_jvm_memory_used_bytes{area="heap"} / elasticsearch_jvm_memory_max_bytes{area="heap"}) * 100 > 90
          for: 2m
          labels:
            severity: critical
          annotations:
            summary: Elasticsearch Heap Usage Too High (instance {{ $labels.instance }})
            description: "The heap usage is over 90%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
        - alert: es-ElasticsearchHealthyDataNodes
          expr: elasticsearch_cluster_health_number_of_data_nodes < 3
          for: 0m
          labels:
            severity: critical
          annotations:
            summary: Elasticsearch Healthy Data Nodes (instance {{ $labels.instance }})
            description: "Missing data node in Elasticsearch cluster\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
EOF
kubectl apply -f elastic-promhteus-alert.yaml

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2085360.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

day15JS-es6的基础语法

1. 严格模式 1.1 严格模式的使用方法 使用方法1&#xff1a;"use strict"; 开启严格模式。 使用方法2&#xff1a;<script type"moaule"></script> 当设置script标签为模块化时&#xff0c;自动启用严格模式。 1.2 严格模式的限制 1. 要求变量…

Android U 多任务启动分屏——整体流程介绍

序 原生的分屏功能是在多任务中&#xff0c;点击应用图标选择分屏&#xff0c;在选择多任务中的其他应用进行分屏 整体流程 层级结构 #1 DefaultTaskDisplayArea typeundefined modefullscreen override-modefullscreen requested-bounds[0,0][0,0] bounds[0,0][1440,2960…

【系统架构师软考】重难点划分及学习要点(一)

目录 引言 综合知识 案例分析 案例分析题型及选择策略 历年案例分析题型及分值分布 学习与应试策略 论文 考试规则 写作要求与技巧 忌讳与注意事项 常见题目与考查方向 引言 系统架构师软考是许多IT从业人员进阶的必经之路。面对这门考试&#xff0c;如何有效划分重难…

spring揭秘14-JdbcTemplate概述与使用操作对象访问数据

文章目录 【README】【1】JdbcTemplate概述【1.1】Jdbc原生api操作数据库【1.1.1】基于JdbcPreparedStatement批量更新 【1.2】JdbcTemplate概述【1.2.1】JdbcTemplate类图【1.2.2】使用DataSourceUtils管理Connection【1.2.3】设置Statement参数&#xff08;控制行为&#xff…

Shopee联盟营销案例对saas行业的启示

在跨境电商的浩瀚海洋中&#xff0c;Shopee以其独特的联盟营销&#xff08;AMS&#xff09;策略&#xff0c;为众多品牌商家开辟了新的增长航道。作为深耕SaaS企业渠道分销多年的林叔&#xff0c;我今天想和大家分享一个来自Shopee的成功营销案例&#xff0c;并从中提炼出对Saa…

每日OJ_牛客_抄送列表(切割字符串)

目录 牛客_抄送列表&#xff08;切割字符串&#xff09; 解析代码 牛客_抄送列表&#xff08;切割字符串&#xff09; 抄送列表__牛客网 解析代码 本题是在第一行的人名中&#xff0c;查找第二行的人名是否存在。牵涉一个全字匹配的问题。步骤&#xff1a; 通过getiine(ci…

HarmonyOS(AIP12 Beta5版)鸿蒙开发:选择条件渲染和显隐控制

开发者可以通过条件渲染或显隐控制两种方式来实现组件在显示和隐藏间的切换。本文从两者原理机制的区别出发&#xff0c;对二者适用场景分别进行说明&#xff0c;实现相应适用场景的示例并给出性能对比数据。 原理机制 条件渲染 if/else条件渲染是ArkUI应用开发框架提供的渲…

软考通过率真的很低吗?

一、软考通过率多少&#xff1f; 首先要说的是&#xff0c;软考办并没有公布过全国考试通过率。但我们可以根据官方公布的报名人数和合格人数做一个预估。 浙江软考办官方公布&#xff0c;浙江2022年下半年软考合格人数为4780人(其中初级779人、中级2392人、高级1609人)。 以…

【附解决方法】由于找不到vcruntime140_1.dll 无法继续执行代码如何处理

准备使用photoshop &#xff0c;结果弹出这个 提示“由于找不到 VCRUNTIME140_1.dll&#xff0c;无法继续执行代码。重新安装程序可能会解决此问题。”&#xff0c;这一般是什么原因导致了这个问题&#xff0c;我们要如何解决? 原因&#xff1a; vcruntime140_1.dll文件即动…

【推荐】9款适合中小企业的知识库管理系统

一、什么是知识库管理系统 知识库管理系统&#xff08;Knowledge Base Management System, KBMS&#xff09;&#xff0c;又称数字资产管理系统&#xff08;Digital Asset Management System&#xff09;&#xff0c;是专门用于管理企业知识文档、图纸、视频、音频等信息内容的…

中级测试工程师面试题

很多软件测试工程师在面试的时候都会遇到考官给的各种各样的面试题&#xff0c;这也反应了测试工程师对企业的重要性&#xff0c;面试通常分为以下几个方面&#xff0c;由于篇幅有限&#xff0c;在这里就只给大家分享一些比较常见的问题。 一、自我介绍 这里我不分享如何自我介…

FPGA——VGA协议

VGA协议 VGA简介接口及引脚定义显示原理显示电路原理VGA协议电压标准数字信号转化标准模拟信号方案数字信号时序标准 模块设计 VGA简介 VGA&#xff0c;英文全称“Video Graphics Array”&#xff0c;译为视频图形阵列&#xff0c;是一种使用模拟信号进行视频传输的标准协议&a…

观测云广告全国登陆,携手华为云引领企业迈向数智化新纪元

随着数字化转型的浪潮席卷全球&#xff0c;企业如何紧跟时代步伐&#xff0c;实现转型升级&#xff0c;已成为业界热议的焦点。华为云828 B2B企业节&#xff0c;作为业界瞩目的年度盛事&#xff0c;已于8月27日在贵阳盛大开幕。与此同时&#xff0c;观测云与华为云强强联手&…

基于 web教学管理系统设计与实现

3 总体设计 3.1 系统软件体系结构 系统采用B/S结构&#xff0c;统一管理数据库和Web服务器。在这种结构下&#xff0c;用户界面完全通过WWW浏览器实现&#xff0c;一部分事务逻辑在前端实现&#xff0c;但是主要事务逻辑在服务器端实现&#xff0c;形成所谓3-tier结构,第一…

沈阳网站建设手机能看的网站

在当今信息化的时代&#xff0c;网站已经成为企业展示形象、推广产品和服务的重要工具。尤其是在中国的沈阳&#xff0c;随着智能手机的普及&#xff0c;越来越多的用户选择通过移动设备浏览网站。因此&#xff0c;建设一个能够在手机上良好展示的网站显得尤为重要。本文将探讨…

vivado中定点类型Binary point的含义

vivado中&#xff0c;ILA或仿真波形显示的定点数&#xff0c;可以设置为有符号或无符号数&#xff0c;其中小数点位置通过Binary point设置&#xff0c;这个设置的数值&#xff0c;表示小数点后的二进制位数 参考&#xff1a; https://people-ece.vse.gmu.edu/coursewebpages/E…

【pandas2】表格数据的行列操作、查询指定的数据内容、数据类型处理、 缺失值处理和透视表、分组与聚合、数据的纵向合并(扩展数据)、数据的横向合并(连接表)

1 表格数据的行列操作 2 查询指定的数据内容 3 数据类型处理 4 缺失值处理 5 透视表 6 分组与聚合 7 统计NBA夺冠次数 8 数据的纵向合并(扩展数据) 9 数据的横向合并(连接表) 1 表格数据的行列操作 # 增 df[info] 这些车都很好 df[desc] df[Sec_price] * df[Km(W)] df.inser…

深度解读SGM41511电源管理芯片I2C通讯协议REG0B寄存器解释

REG0B 是 SGM41511 的第十二个寄存器 也是最后一个寄存器&#xff0c;地址为 0x0B。这个寄存器包含了只读&#xff08;R&#xff09;和可读写&#xff08;R/W&#xff09;的位。上电复位值&#xff08;PORV&#xff09;为 000101xx&#xff0c;其中 x 表示不确定的初始状态。这…

实时数仓,站上产业潮头

在这场新的数据驱动战场里&#xff0c;谁能更好的对数据进行智能、准确、迅速、高性价比的体系化处理&#xff0c;谁能以更低的成本、更高效的能力构建底层的PaaS、IaaS组件&#xff0c;谁就能在如今的市场竞争中构建更具竞争力的业务模型&#xff0c;成为新的弄潮儿。 对Byt…

C#开发中ImageComboBox控件数据源实时变换

在C#开发中&#xff0c;我们如何将控件的数据源实时变换&#xff0c;当然我们可以在窗口实例化的时候指定固定的数据源&#xff0c;但是这样对于用户来说数据源永远固定&#xff0c;并不利于我们对于用户的数据存储&#xff0c;优化用户的操作&#xff0c;遇到这种问题&#xf…