prometheus监控k8s服务并告警到钉钉

一、监控k8s集群

要监控k8s集群需要使用到以下服务用于收集监控的资源信息,node_exporter用于监控k8s集群节点的资源信息,kube-state-metrics用于监控k8s集群的deployment、statefulset、daemonset、pod等的状态,cadvisor用于监控k8s集群的pod资源信息

在k8s集群中创建monitoring的命名空间用于部署监控的容器

kubectl create namespace monitoring

在k8s集群中部署node_exporter容器服务

vi node-exporter.yaml

apiVersion: apps/v1
kind: DaemonSet      #使用daemonset控制器,使得集群中的每个节点都能部署一个pod
metadata:name: node-exporternamespace: monitoring labels:k8s-app: node-exporter
spec:selector:matchLabels:k8s-app: node-exportertemplate:metadata:labels:k8s-app: node-exporterspec:tolerations:          #配置容忍策略,使得pod能部署在master节点上- effect: NoSchedulekey: node-role.kubernetes.io/control-planecontainers:- image: prom/node-exporter      #配置node-exporter的镜像imagePullPolicy: IfNotPresentname: prometheus-node-exporterports:- containerPort: 9100     #配置容器端口hostPort: 9100         #配置绑定k8s主机节点的端口,用于提供对外访问的接口protocol: TCPname: metricshostNetwork: true          #使用hostNetwork: true是必要的,这样才能将Pod的网络栈绑定到宿主机上,以实现hostPort的功能

执行yaml生成node-exporter容器

kubectl apply -y node-exporter.yaml

查看容器

kubectl get pod -n monitoring -l k8s-app=node-exporter -o wide

 可以看到集群的每个节点都有一个node_exporter的pod服务

查看收集的数据

http://10.1.60.119:9100/metrics

 

在k8s集群中部署kube-state-metrics容器服务

部署kube-state-metrics服务需要去github上的项目拉取yaml

下载地址:https://github.com/kubernetes/kube-state-metrics/tree/v2.9.2

需要根据自己的k8s集群版本下载合适的kube-state-metrics版本,我的k8s版本是1.26.0所以我是下载了2.9.2版本的kube-state-metrics

 mkdir /opt/kube-state-metrics && cd /opt/kube-state-metrics

将下载的安装包放到该目录下解压

tar -zxvf kube-state-metrics-2.9.2.tar.gz 

将需要用到的yaml文件拷贝出来

mv kube-state-metrics-2.9.2/examples/standard/* /opt/kube-state-metrics

ls

 更改一下yaml文件

vi deployment.yaml

apiVersion: apps/v1
kind: Deployment    #使用deployment控制器,将pod部署在工作节点即可
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.9.2name: kube-state-metricsnamespace: kube-system
spec:replicas: 1selector:matchLabels:app.kubernetes.io/name: kube-state-metricstemplate:metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.9.2spec:automountServiceAccountToken: truecontainers:- image: bitnami/kube-state-metrics:2.9.2   #更改镜像地址,原本的镜像在国外拉不下来livenessProbe:httpGet:path: /healthzport: 8080initialDelaySeconds: 5timeoutSeconds: 5name: kube-state-metricsports:- containerPort: 8080name: http-metrics- containerPort: 8081name: telemetryreadinessProbe:httpGet:path: /port: 8081initialDelaySeconds: 5timeoutSeconds: 5securityContext:allowPrivilegeEscalation: falsecapabilities:drop:- ALLreadOnlyRootFilesystem: truerunAsNonRoot: truerunAsUser: 65534seccompProfile:type: RuntimeDefaultnodeSelector:kubernetes.io/os: linuxserviceAccountName: kube-state-metrics

关于镜像的问题可以使用docker命令查一下镜像

docker search  kube-state-metrics

 

vi service.yaml

apiVersion: v1
kind: Service
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.9.2name: kube-state-metricsnamespace: kube-system
spec:type: NodePortclusterIP:ports:- name: http-metricsport: 8080nodePort: 30080            #原本的端口值比较大,超过了nodeport的端口范围targetPort: http-metricsprotocol: TCP- name: telemetryport: 8081 nodePort: 30081            #原本的端口值比较大,超过了nodeport的端口范围targetPort: telemetryprotocol: TCPselector:app.kubernetes.io/name: kube-state-metrics

其它的yaml保持默认即可

执行yaml创建kube-state-metrics服务

kubectl apply -f /opt/kube-state-metrics/

查看pod、svc服务

kubectl get pod,svc -n kube-system

 查看收集的数据

http://10.1.60.119:30080/metrics

在k8s集群中部署cadvisor容器服务

vi cadvisor.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:name: cadvisornamespace: monitoring
spec:selector:matchLabels:k8s-app: cadvisortemplate:metadata:labels:k8s-app: cadvisorspec:tolerations:- effect: NoSchedulekey: node-role.kubernetes.io/control-planehostNetwork: truerestartPolicy: Alwayscontainers:- name: cadvisorimage: google/cadvisorimagePullPolicy: IfNotPresentports:- containerPort: 8080hostPort: 8080protocol: TCPname: metrics        

执行yaml生成cadvisor容器

kubectl apply -y cadvisor.yaml

查看容器

kubectl get pod -n monitoring -l k8s-app=cadvisor -o wide

 

 可以看到集群的每个节点都有一个cadvisor的pod服务

 查看收集的数据

http://10.1.60.119:8080/metrics

 

二、Prometheus获取监控服务的数据并使用grafana展示

部署prometheus

参考:prometheus部署_Apex Predator的博客-CSDN博客

部署grafana

参考: grafana部署_Apex Predator的博客-CSDN博客

配置prometheus 

编辑Prometheus配置文件

vi /opt/prometheus/prometheus/prometheus.yml

global:scrape_interval: 15s evaluation_interval: 15s 
#alerting:    #关于告警组件的配置先忽略# alertmanagers:#   - static_configs:#      - targets:#          - 10.1.60.118:9093
#rule_files:     #关于告警规则的配置先忽略
#  - "/opt/prometheus/prometheus/rule/*.yml"
scrape_configs:- job_name: "prometheus"static_configs:- targets: ["localhost:9090"]- job_name: "k8s_node_exporter"  #配置k8s集群node_exporter监控数据服务的接口static_configs:- targets: ["10.1.60.119:9100","10.1.60.120:9100","10.1.60.121:9100","10.1.60.122:9100","10.1.60.123:9100"]- job_name: "k8s_pod_cadvisor"   #配置k8s集群cadvisor监控数据服务的接口static_configs:- targets: ["10.1.60.119:8080","10.1.60.120:8080","10.1.60.121:8080","10.1.60.122:8080","10.1.60.123:8080"]- job_name: "kube-state-metrics"   #配置k8s集群kube-state-metrics监控数据服务的接口static_configs:- targets: ["10.1.60.119:30081"]- job_name: "kube-state-telemetry"static_configs:- targets: ["10.1.60.119:30080"]

 重启prometheus服务

systemctl restart prometheus

查看prometheus监控接口的情况

http://10.1.60.118:9090

配置grafana

配置prometheus为数据源

 

 配置数据展示的dashboard

在以下网页中找到需要的模板

地址:Dashboards | Grafana Labs

 node_exporter服务的模板我们就使用id为1860的模板

 kube-state-metrics服务的模板我们就使用id为13332的模板

 cadvisor服务的模板我们就使用id为1860的模板

配置grafana应用模板

 

 

 

其余两个也是一样找到模板id后进行配置即可,这里就不再展示了

三、Prometheus配置告警规则和告警服务实现钉钉告警 

要实现钉钉告警需要部署alertmanager和prometheus-webhook-dingtalk服务

部署参考:prometheus告警发送组件部署_Apex Predator的博客-CSDN博客

配置prometheus告警规则

关于prometheus的告警规则可以在以下网站中找,里面有很多的告警规则

参考:Awesome Prometheus alerts | Collection of alerting rules

我这里就配置k8s集群主机节点的告警规则和pod的一些告警规则 

mkdir /opt/prometheus/prometheus/rule && cd /opt/prometheus/prometheus/rule

 vi node_exporter.yml

groups:
- name: 服务器资源监控rules:- alert: 内存使用率过高expr: 100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 80for: 3mlabels:severity: 严重告警annotations:summary: "{{ $labels.instance }} 内存使用率过高, 请尽快处理!"description: "{{ $labels.instance }}内存使用率超过80%,当前使用率{{ $value }}%."- alert: 服务器宕机expr: up == 0for: 1slabels:severity: 严重告警annotations:summary: "{{$labels.instance}} 服务器宕机, 请尽快处理!"description: "{{$labels.instance}} 服务器延时超过3分钟,当前状态{{ $value }}. "- alert: CPU高负荷expr: 100 - (avg by (instance,job)(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90for: 5mlabels:severity: 严重告警annotations:summary: "{{$labels.instance}} CPU使用率过高,请尽快处理!"description: "{{$labels.instance}} CPU使用大于90%,当前使用率{{ $value }}%. "- alert: 磁盘IO性能expr: avg(irate(node_disk_io_time_seconds_total[1m])) by(instance,job)* 100 > 90for: 5mlabels:severity: 严重告警annotations:summary: "{{$labels.instance}} 流入磁盘IO使用率过高,请尽快处理!"description: "{{$labels.instance}} 流入磁盘IO大于90%,当前使用率{{ $value }}%."- alert: 网络流入expr: ((sum(rate (node_network_receive_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance,job)) / 100) > 102400for: 5mlabels:severity: 严重告警annotations:summary: "{{$labels.instance}} 流入网络带宽过高,请尽快处理!"description: "{{$labels.instance}} 流入网络带宽持续5分钟高于100M. RX带宽使用量{{$value}}."- alert: 网络流出expr: ((sum(rate (node_network_transmit_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance,job)) / 100) > 102400for: 5mlabels:severity: 严重告警annotations:summary: "{{$labels.instance}} 流出网络带宽过高,请尽快处理!"description: "{{$labels.instance}} 流出网络带宽持续5分钟高于100M. RX带宽使用量{$value}}."- alert: TCP连接数expr: node_netstat_Tcp_CurrEstab > 10000for: 2mlabels:severity: 严重告警annotations:summary: " TCP_ESTABLISHED过高!"description: "{{$labels.instance}} TCP_ESTABLISHED大于100%,当前使用率{{ $value }}%."- alert: 磁盘容量expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes {fstype=~"ext4|xfs"}*100) > 90for: 1mlabels:severity: 严重告警annotations:summary: "{{$labels.mountpoint}} 磁盘分区使用率过高,请尽快处理!"description: "{{$labels.instance}} 磁盘分区使用大于90%,当前使用率{{ $value }}%."

vi kube-state-metrics.yml 

groups:           #用于定义一个或多个告警规则分组
- name: k8s容器服务监控    #告警规则分组的名称,用于标识一组相关的告警规则rules:                  #规则列表,每个规则定义了一个具体的告警条件和处理方式- alert: KubernetesNodeNotReady      #告警规则的名称,用于标识告警规则expr: kube_node_status_condition{condition="Ready",status="true"} == 0  #定义告警的条件for: 10m        #告警规则的持续时间配置,规定了节点状态满足告警条件的持续时间达到 10 分钟时触发告警labels:severity: 严重告警     #告警规则标签annotations:summary: "Kubernetes node not ready (instance {{ $labels.instance }})"description: "Node {{ $labels.node }} has been unready for a long time\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: KubernetesOutOfCapacityexpr: sum by (node) ((kube_pod_status_phase{phase="Running"} == 1) + on(uid) group_left(node) (0 * kube_pod_info{pod_template_hash=""})) / sum by (node) (kube_node_status_allocatable{resource="pods"}) * 100 > 90for: 2mlabels:severity: 严重告警annotations:summary: "Kubernetes out of capacity (instance {{ $labels.instance }})"description: "{{ $labels.node }} is out of capacity\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: KubernetesContainerOomKillerexpr: (kube_pod_container_status_restarts_total - kube_pod_container_status_restarts_total offset 10m >= 1) and ignoring (reason) min_over_time(kube_pod_container_status_last_terminated_reason{reason="OOMKilled"}[10m]) == 1for: 0mlabels:severity: 严重告警annotations:summary: "Kubernetes container oom killer (instance {{ $labels.instance }})"description: "Container {{ $labels.container }} in pod {{ $labels.namespace }}/{{ $labels.pod }} has been OOMKilled {{ $value }} times in the last 10 minutes.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: KubernetesVolumeOutOfDiskSpaceexpr: kubelet_volume_stats_available_bytes / kubelet_volume_stats_capacity_bytes * 100 < 10for: 2mlabels:severity: 严重告警annotations:summary: "Kubernetes Volume out of disk space (instance {{ $labels.instance }})"description: "Volume is almost full (< 10% left)\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: KubernetesPersistentvolumeErrorexpr: kube_persistentvolume_status_phase{phase=~"Failed|Pending", job="kube-state-metrics"} > 0for: 0mlabels:severity: 严重告警annotations:summary: "Kubernetes PersistentVolume error (instance {{ $labels.instance }})"description: "Persistent volume is in bad state\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: pod不健康expr: sum by (namespace, pod) (kube_pod_status_phase{phase=~"Pending|Unknown|Failed"}) > 0for: 15mlabels:severity: 严重告警annotations:summary: "Kubernetes Pod not healthy (instance {{ $labels.instance }})"description: "Pod has been in a non-ready state for longer than 15 minutes.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: pod循环重启expr: increase(kube_pod_container_status_restarts_total[2m]) > 1for: 0mlabels:severity: 严重告警annotations:summary: "Kubernetes pod crash looping (instance {{ $labels.instance }})"description: "Pod {{ $labels.pod }} is crash looping\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: deployment部署失败未回滚expr: kube_deployment_status_observed_generation != kube_deployment_metadata_generationfor: 10mlabels:severity: 严重告警annotations:summary: "Kubernetes Deployment generation mismatch (instance {{ $labels.instance }})"description: "A Deployment has failed but has not been rolled back.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"- alert: k8s证书临期警告expr: apiserver_client_certificate_expiration_seconds_count{job="apiserver"} > 0 and histogram_quantile(0.01, sum by (job, le) (rate(apiserver_client_certificate_expiration_seconds_bucket{job="apiserver"}[5m]))) < 7*24*60*60for: 0mlabels:severity: 严重告警annotations:summary: "Kubernetes client certificate expires next week (instance {{ $labels.instance }})"description: "A client certificate used to authenticate to the apiserver is expiring next week.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

重启Prometheus服务

systemctl restart prometheus

访问prometheus查看规则是否生效

http://10.1.60.118:9090

 

告警测试,关闭node_exporter服务,看看是否会告警

kubectl delete -f node_exporter.yaml

通过prometheus我们可以看到告警规则首先是变成了pending状态

 然后等了一会后转变为firing状态,这是因为配置了for,当触发条件满足一段时间后才会完全转化为触发告警

 等待30s后将会收到钉钉告警,这是因为alertmanager配置了group_wait,当一组告警被触发后,在这个时间段内,其他属于同一组的告警也会被等待。这可以用于在一定时间内收集同一组告警,以便一次性发送通知

 现在来将服务恢复一下,看多久会告警

kubectl apply -f node_exporter.yaml

可以看到是间隔时间几分钟后才告警恢复, 这是因为alertmanager配置了group_interval,一旦一个告警组的首个告警触发了通知,等待指定的间隔时间后,即使组内有其他告警,也会重新触发通知。这可以避免过于频繁地发送通知

其他的告警规则服务我就不一个一个测试了,都是没有问题的

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

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

相关文章

JVM内存管理

文章目录 1、运行时数据区域1.1 程序计数器&#xff08;线程私有&#xff09;1.2 JAVA虚拟机栈&#xff08;线程私有&#xff09;1.3 本地方法栈1.4 Java堆&#xff08;线程共享&#xff09;1.5 方法区&#xff08;线程共享&#xff09;1.6 直接内存&#xff08;非运行时数据区…

vue动态生成行

vue代码 <el-table :data"form.lineInfos" :bordertrue style"width: 99.99%;"> <el-table-column type"index" label"序号" width"50"></el-table-column> <el-table-column prop"unitPrice&qu…

golang协程池(goroutine池)ants库实践

golang中goroutine由运行时管理&#xff0c;使用go关键字就可以方便快捷的创建一个goroutine,受限于服务器硬件内存大小&#xff0c;如果不对goroutine数量进行限制&#xff0c;会出现Out of Memory错误。但是goroutine泄漏引发的血案&#xff0c;想必各位gopher都经历过&#…

在Qt中使用LoadLibrary无法加载DLL

Qt系列文章目录 文章目录 Qt系列文章目录前言一、问题分析 前言 最近因项目需要使用qt做开发&#xff0c;之前使用LoadLibrary加载dll成功&#xff0c;很庆幸&#xff0c;当一切都那么顺风顺水的时候&#xff0c;测试同事却发现&#xff0c;在windows平台上个别电脑上加载dll会…

Redis BitMap/HyperLogLog/GEO/布隆过滤器案例

面试问题&#xff1a; 抖音电商直播&#xff0c;主播介绍的商品有评论&#xff0c;1个商品对应了1系列的评论&#xff0c;排序展现取前10条记录用户在手机App上的签到打卡信息&#xff1a;1天对应1系列用户的签到记录&#xff0c;新浪微博、钉钉打卡签到&#xff0c;来没来如何…

【Grafana】中文界面配置 v10.0.3

比如通过 docker run -d -p 3000:3000 -v /e/code/monitor/grafana/grafana.ini.txt:/etc/grafana/grafana.ini grafana/grafana运行一个容器&#xff08;最新是v10.0.3&#xff09;。 在 /admin/settings 可以看到 users 部分有一个 default_language 配置。 所以在挂载到 …

php代码审计,php漏洞详解

文章目录 1、输入验证和输出显示2、命令注入(Command Injection)3、eval 注入(Eval Injection)4、跨网站脚本攻击(Cross Site Scripting, XSS)5、SQL 注入攻击(SQL injection)6、跨网站请求伪造攻击(Cross Site Request Forgeries, CSRF)7、Session 会话劫持(Session Hijacking…

自动化实践-全量Json对比在技改需求提效实践

1 背景 随着自动化测试左移实践深入&#xff0c;越来越多不同类型的需求开始用自动化测试左移来实践&#xff0c;在实践的过程中也有了新的提效诉求&#xff0c;比如技改类的服务拆分项目或者BC流量拆分的项目&#xff0c;在实践过程中&#xff0c;这类需求会期望不同染色环境…

SpringCloud源码探析(九)- Sentinel概念及使用

1.概述 在微服务的依赖调用中&#xff0c;若被调用方出现故障&#xff0c;出于自我保护的目的&#xff0c;调用方会主动停止调用&#xff0c;并根据业务需要进行对应处理&#xff0c;这种方式叫做熔断&#xff0c;是微服务的一种保护方式。为了保证服务的高可用性&#xff0c;…

rknn3588如何查看npu使用情况

cat /sys/kernel/debug/rknpu/load

windows环境下编译OpenJDK12

环境&#xff1a;Windows11 目录&#xff1a; 1、下载OpenJDK12源码 下载地址&#xff1a; https://hg.openjdk.org/jdk/jdk12 点击zip下载到本地。 解压到本地。 Tip&#xff1a;注意本地路径中最好不要包含中文或空格。 2、阅读一遍doc/building.html 如果只是想构建J…

白帽黑帽与linux安全操作

目录 白帽黑帽 Linux安全 白帽黑帽 白帽&#xff08;White Hat&#xff09;和黑帽&#xff08;Black Hat&#xff09;通常用于描述计算机安全领域中的两种不同角色。白帽黑客通常被认为是合法的安全专家&#xff0c;他们通过合法途径寻找和修复安全漏洞&#xff0c;帮助企业和…

Linux/centos上如何配置管理samba服务器?

Linux/centos上如何配置管理samba服务器&#xff1f; 1 samba服务相关知识1.1 SMB协议1.2 samba工作原理1.2.1 相关进程1.2.2 samba工作流程1.2.3 samba功能 2 samba服务器安装2.1 利用光驱安装2.2 利用光盘映射文件 3 启动与停止samba服务4 配置samba服务器4.1 samba主配置文件…

解读百威亚太2023上半年财报:啤酒大年百威如何重塑高端化之路?

随着消费者的需求提升&#xff0c;啤酒行业向高端化发展&#xff0c;其中知名度较高的百威亚太、华润啤酒、青岛啤酒、燕京啤酒、嘉士伯等品牌在高端市场持续鏖战&#xff0c;实际成果如何也可以从业绩一探究竟。 以百威亚太为例。8月3日&#xff0c;百威亚太发布2023年上半年…

记录一次electron打包提示文件找不到的解决方法

没有配置files选项 files的作用是配置打包到应用程序的构建资源 就是说如果你想使用项目那个目录下的文件 就得通过files配置一下不然就会报错 json文件或者yml文件会报的错 格式是这样的 "files": ["dist-electron", "dist"],electron打包配…

【C++学习】STL容器——list

目录 一、list的介绍及使用 1.1 list的介绍 1.2 list的使用 1.2.1 list的构造 1.2.2 list iterator的使用 1.2.3 list capacity 1.2.4 list element access 1.2.5 list modifiers 1.2.6 list 迭代器失效 二、list的模拟实现 2.1 模拟实现list 三、list和vector的对比…

【算法挨揍日记】day01——双指针算法_移动零、 复写零

283.移动零 283. 移动零https://leetcode.cn/problems/move-zeroes/ 题目&#xff1a; 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。 …

生信豆芽菜-火山图绘制使用说明

网站&#xff1a;http://www.sxdyc.com/visualsVolcano 一、火山图简介 火山图是散点图的一种&#xff0c;它将统计测试中的统计显著性量度&#xff08;如p value&#xff09;和变化幅度&#xff08;logFC&#xff09;相结合&#xff0c;能够快速直观地识别那些变化幅度较大且具…

13个Python最佳编程技巧,越早知道越好

每天我们都会面临许多需要高级编码的编程挑战。你不能用简单的 Python 基本语法来解决这些问题。在本文中&#xff0c;我将分享 13 个高级 Python 脚本&#xff0c;它们可以成为你项目中的便捷工具。如果你目前还用不到这些脚本&#xff0c;你可以先添加收藏&#xff0c;以备留…

springMVC 程序开发

目录 一. 认识 springMVC spring&#xff0c;springBoot&#xff0c;springMVC的关系 二. springMVC 的连接和获取参数 1. 注解分析&#xff08;不带参数&#xff09; 2. 获取参数 3. 获取对象参数 4. 重命名功能 5. 获取 JSON 对象 6. 通过 path 文件路径来传递参数…