K8S集群监控方案之Prometheus+kube-state-metrics+Grafana

序言 | Prometheus 中文文档

方案简单架构图

一、部署kube-state-metrics

1、部署文件下载

地址

kube-state-metrics/examples/standard at main · kubernetes/kube-state-metrics · GitHub

2、修改下载的文件

2.1、修改镜像

原镜像可能下载不了,这里修改deployment.yaml镜像:bitnami/kube-state-metrics:latest

2.2、修改service

这里修改为NodePort类型,可按自己需求更改

3、本案例下载更改好后的部署文件

cluster-role.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.12.0name: kube-state-metrics
rules:
- apiGroups:- ""resources:- configmaps- secrets- nodes- pods- services- serviceaccounts- resourcequotas- replicationcontrollers- limitranges- persistentvolumeclaims- persistentvolumes- namespaces- endpointsverbs:- list- watch
- apiGroups:- appsresources:- statefulsets- daemonsets- deployments- replicasetsverbs:- list- watch
- apiGroups:- batchresources:- cronjobs- jobsverbs:- list- watch
- apiGroups:- autoscalingresources:- horizontalpodautoscalersverbs:- list- watch
- apiGroups:- authentication.k8s.ioresources:- tokenreviewsverbs:- create
- apiGroups:- authorization.k8s.ioresources:- subjectaccessreviewsverbs:- create
- apiGroups:- policyresources:- poddisruptionbudgetsverbs:- list- watch
- apiGroups:- certificates.k8s.ioresources:- certificatesigningrequestsverbs:- list- watch
- apiGroups:- discovery.k8s.ioresources:- endpointslicesverbs:- list- watch
- apiGroups:- storage.k8s.ioresources:- storageclasses- volumeattachmentsverbs:- list- watch
- apiGroups:- admissionregistration.k8s.ioresources:- mutatingwebhookconfigurations- validatingwebhookconfigurationsverbs:- list- watch
- apiGroups:- networking.k8s.ioresources:- networkpolicies- ingressclasses- ingressesverbs:- list- watch
- apiGroups:- coordination.k8s.ioresources:- leasesverbs:- list- watch
- apiGroups:- rbac.authorization.k8s.ioresources:- clusterrolebindings- clusterroles- rolebindings- rolesverbs:- list- watch

cluster-role-binding.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.12.0name: kube-state-metrics
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: kube-state-metrics
subjects:
- kind: ServiceAccountname: kube-state-metricsnamespace: kube-system

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.12.0name: 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.12.0spec:automountServiceAccountToken: truecontainers:- image: bitnami/kube-state-metrics:latestlivenessProbe: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


service-account.yaml

apiVersion: v1
automountServiceAccountToken: false
kind: ServiceAccount
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.12.0name: kube-state-metricsnamespace: kube-system

service.yaml

apiVersion: v1
kind: Service
metadata:labels:app.kubernetes.io/component: exporterapp.kubernetes.io/name: kube-state-metricsapp.kubernetes.io/version: 2.12.0name: kube-state-metricsnamespace: kube-system
spec:type: NodePortports:- name: http-metricsport: 8080targetPort: 8080nodePort: 31666- name: telemetryport: 8081targetPort: 8081selector:app.kubernetes.io/name: kube-state-metrics

4、部署kube-state-metrics 

[K8S@k8s-master kube-state-metrics]$ ll
总用量 20
-rw-rw-r-- 1 K8S K8S  418 5月  17 16:11 cluster-role-binding.yaml
-rw-rw-r-- 1 K8S K8S 1950 5月  17 16:11 cluster-role.yaml
-rw-rw-r-- 1 K8S K8S 1471 5月  17 16:11 deployment.yaml
-rw-rw-r-- 1 K8S K8S  270 5月  17 16:11 service-account.yaml
-rw-rw-r-- 1 K8S K8S  453 5月  17 16:11 service.yaml
[K8S@k8s-master kube-state-metrics]$ pwd
/home/K8S/k8s-project/monitor/kube-state-metrics
[K8S@k8s-master kube-state-metrics]$ kubectl apply -f ./

 查看部署结果:

[K8S@k8s-master kube-state-metrics]$ kubectl get all -n kube-system

Running状态表示部署成功

 访问Service暴露的31666端口,能成功返回

二、部署Prometheus

1、创建名称空间

[K8S@k8s-master prometheus]$ kubectl create ns monitoring

2、编写配置文件 prometheus.yml

apiVersion: v1
kind: ServiceAccount
metadata:name: prometheusnamespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: prometheus
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
subjects:
- kind: ServiceAccountname: prometheusnamespace: monitoring
---
apiVersion: v1
kind: ConfigMap
metadata:name: prometheus-confignamespace: monitoring
data:prometheus.yml: |global:scrape_interval: 15s      # 数据采集时间间隔scrape_timeout: 10s       # 数据采集超时时间evaluation_interval: 1mscrape_configs:- job_name: "prometheus"static_configs:- targets: ["localhost:9090"]- job_name: "k8s-info"static_configs:- targets: ["172.19.3.240:31666"]  #配置kube-state-metrice的数据源地址---
apiVersion: apps/v1
kind: Deployment
metadata:name: prometheusnamespace: monitoring
spec:replicas: 1selector:matchLabels:app: prometheustemplate:metadata:labels:app: prometheusspec:serviceAccountName: prometheuscontainers:- name: prometheusimage: prom/prometheus:v2.31.1args:- --config.file=/etc/prometheus/prometheus.yml- --storage.tsdb.path=/prometheusports:- containerPort: 9090volumeMounts:- name: config-volumemountPath: /etc/prometheus- name: storage-volumemountPath: /prometheusvolumes:- name: config-volumeconfigMap:name: prometheus-configitems:- key: prometheus.ymlpath: prometheus.yml- name: storage-volumeemptyDir: {}
---
apiVersion: v1
kind: Service
metadata:name: prometheus-servicenamespace: monitoring
spec:type: NodePortports:- name: httpport: 9090targetPort: 9090nodePort: 30007selector:app: prometheus

创建资源 

[K8S@k8s-master prometheus]$ kubectl apply -f prometheus.yml 

查看状态为Running,表示正常启动

访问Service暴露的30007端口 http://172.19.3.240:30007/targets

成功读取到kube-state-metrics提供的数据

prometheus部署成功

三、部署Grafana

官网安装参考

Deploy Grafana on Kubernetes | Grafana documentation

1、编写grafana.yaml

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: grafana-pvcnamespace: monitoring
spec:accessModes:- ReadWriteOnceresources:requests:storage: 1GistorageClassName: managed-nfs-devops-storage
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: grafananame: grafananamespace: monitoring
spec:selector:matchLabels:app: grafanatemplate:metadata:labels:app: grafanaspec:securityContext:fsGroup: 472supplementalGroups:- 0containers:- name: grafanaimage: grafana/grafana:latestimagePullPolicy: IfNotPresentports:- containerPort: 3000name: http-grafanaprotocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /robots.txtport: 3000scheme: HTTPinitialDelaySeconds: 10periodSeconds: 30successThreshold: 1timeoutSeconds: 2livenessProbe:failureThreshold: 3initialDelaySeconds: 30periodSeconds: 10successThreshold: 1tcpSocket:port: 3000timeoutSeconds: 1resources:requests:cpu: 250mmemory: 750MivolumeMounts:- mountPath: /var/lib/grafananame: grafana-pvvolumes:- name: grafana-pvpersistentVolumeClaim:claimName: grafana-pvc
---
apiVersion: v1
kind: Service
metadata:name: grafananamespace: monitoring
spec:ports:- port: 3000protocol: TCPtargetPort: http-grafananodePort: 30008selector:app: grafanasessionAffinity: Nonetype: NodePort

2、执行命令创建资源

[K8S@k8s-master monitor]$ kubectl apply -f grafana.yaml 
persistentvolumeclaim/grafana-pvc created
deployment.apps/grafana created
service/grafana created

查看资源,Running,安装成功

 3、访问暴露30008端口页面

默认登录账号密码 admin/admin,查看页面访问成功

四、Grafana通过页面配置k8s集群资源展示

1、配置数据源

步骤1

步骤2 

步骤3 

步骤4,配置Prometheus数据源 

 拉到最下边,点击保存测试

 2、配置展示模板

如图操作进入import页面

访问官网模板页面 Dashboards | Grafana Labs

按如下图搜索点击

进入页面点击拷贝id,或者点下边下载模板也可以

回到我们的grafana import页面,粘贴ID或者导入下载的json

 

自定义修改数据点击import 

查看资源模板页面,配置成功 

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

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

相关文章

苹果与OpenAI合作在即:iOS 18中的ChatGPT引发期待与担忧

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领…

互联网应用主流框架整合之数据库事务管理

在互联网系统中同时运行着成千上百个线程是十分常见的事情,尤其当一个热门出现时,用户几乎同时打开手机、电脑、平板灯设备进行访问,这样就会导致数据库处在一个多事务访问的环境中,从而引发数据丢失或者数据不一致的现象&#xf…

计算机考研|408开始的晚,如何规划复习?

408开始的比较晚不用怕 只需要6个月的复习时间,按照我的复习计划,你至少可以考100这个分数 我的计划是对于基础差的考生的,大佬们就可以绕道了,对于基础差的考生来说,408复习的难点其实在于学好数据结构和计算机组成…

rk3568_semaphore

文章目录 前言1 什么是信号量1.1 信号量API函数2、信号量实验2.1 实验目的2.2函数源码2.3 运行结果图前言 本文记录rk3568开发板的信号量实验 1 什么是信号量 信号量是同步的一种方式,常常用于控制对共享资源的访问。 举个例子:停车场的停车位有100个,这100个停车位就是共…

【Python】 如何将列表转换为字符串

基本原理 在Python中,列表(list)和字符串(string)是两种不同的数据类型。列表是一个有序的元素集合,可以包含不同类型的元素,而字符串则是一个字符序列。有时,我们可能需要将列表中…

【chagpt】广泛使用API之前:考虑成本和数据隐私

文章目录 一. 定价和标记限制二. 安全和隐私 在广泛使用API之前,应该考虑两个重要因素:成本和数据隐私。 一. 定价和标记限制 OpenAI在Pricing页面上列出了模型的定价。请注意,OpenAI不一定及时更新该页面上的定价信息,因此实际…

预训练模型语义相似性计算(十一) - M3E和BGE

M3E m3e由MokaAI 训练,开源和评测。 m3e的详细介绍可以看官方的github介绍。本文简要摘录其中一些点,以便后续的应用。 1.千万级 (2200w) 的中文句对数据(开源)。 2.支持同质相似句计算(s2s)和异质检索(s2p),后续支持代码检索。 3.m3e基座模…

C# 泛型函数

1.非约束 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace MyGeneirc {public class GeneircMethod{/// <summary>/// 泛型方法解决&#xff0c;一个方法&#xff0c;满足不同参数类型…

JVM学习-彻底搞懂Java自增++

从字节码角度分析i和i的区别 public void method6() {int i 10;i; //在局部变量表上直接加1}public void method7() {int i 10;i; //字节码同i}public void method8() {int i 10;int a i; //通过下图可以看出先将局部变量表中的值push到操作数栈&#xff0c;然…

springboot项目部署到linux服务器

springboot后端 修改前 修改后 vue前端 修改前 将地址中的 localhost改为 ip 重新生成war包 war上传到linux的tomcat的webapps下 其他环境配置和macOS大差不差 Tomcat安装使用与部署Web项目的三种方法_tomcat部署web项目-CSDN博客

rk3568_mutex

文章目录 前言1、什么是mutex?1.1mutex互斥体API函数二、实验2.1实验目的2.2源码2.3结果图前言 本文记录的是rk3568开发板基础上做的mutex实验 1、什么是mutex? mutex是互斥体,它是比信号量semaphore更加专业的机制。 在我们编写Linux驱动的时候遇到需要互斥的地方建议使用…

Matlab读取Swarm球谐系数,并绘制EWH全球格网图(存在疑问)

ICGEM官网下载 COST-G发布的4040的球谐系数 close all; clearvars -except; % addpath(E:\Code\Tool\Function\GRACE_functions); dir_degree_1 E:\Code\GRACE_data\Degree_1\deg1_coef.txt; dir_c20 E:\Code\GRACE_data\Degree_2\C20_RL06.txt; myDir_Swarm E:…

深入理解栏目函数:从基础结构到实际应用

新书上架~&#x1f447;全国包邮奥~ python实用小工具开发教程http://pythontoolsteach.com/3 欢迎关注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目录 一、栏目函数的基本结构解析 代码案例 二、栏目函数的参数理解与返回值探究 参数取值与返…

2024年上半年软件设计师试题及答案(回忆版)

目录 基础知识选择题案例题1.缺陷识别的数据流图2.球队、球员、比赛记录的数据库题3.用户、老师、学生、课程用例图4.算法题5.程序设计题基础知识选择题 树的节点,度为4的有4个,度为3的有8个,度为2个有6个,度为1的有10个,问有几个叶子结点 二位数组,一个元素2个字节,A0…

php TP8 阿里云短信服务SDKV 2.0(跳大坑)

安装&#xff1a;composer require alibabacloud/dysmsapi-20170525 2.0.24 官方文档&#xff1a;短信服务_SDK中心-阿里云OpenAPI开发者门户 (aliyun.com) 特别注意&#xff1a;传入参数获得值形式 这样也不行 $sendSmsRequest new SendSmsRequest($addData); 还有一个大坑…

Mesa软件框架以及重要数据结构分析

Mesa软件框架以及重要数据结构分析 引言 Mesa的实现比较复杂&#xff0c;其中还有许多的数据结构之间的关系逻辑还不是很清楚。感觉分析了又没有分析一样&#xff0c;这里我们再理一理&#xff01; 1.1 Mesa下EGL/GL核心数据结构和层级关系 MESA的核心数据结构很多很复杂&#…

10.RedHat认证-Linux文件系统(上)

10.RedHat认证-Linux文件系统(上) ⽂件系统&#xff0c;顾名思义&#xff0c;是⼀个组织⽂件的“系统(system)”。file system ⽂件系统是⽤来组织⽂件的&#xff0c;通俗⼀点理解的话&#xff0c;⽂件系统是⽤来存储⽂件的。 硬盘是不能直接存放⽂件或数据。 我们通过将硬…

mybatis关联查询使用resultMap查询到了多条,结果返回一条。

今天在写代码时候&#xff0c;遇到了一个很让我费解的问题&#xff0c;在使用关联查询的时候&#xff0c;在明明数据库里面&#xff0c;已经查到了两条数据&#xff0c;结果resultMap这个集合里面&#xff0c;就只返回一条数据。 数据库的SQL&#xff1a; mybatis的xml里面的r…

【Python编程实战】基于Python语言实现学生信息管理系统

&#x1f3a9; 欢迎来到技术探索的奇幻世界&#x1f468;‍&#x1f4bb; &#x1f4dc; 个人主页&#xff1a;一伦明悦-CSDN博客 ✍&#x1f3fb; 作者简介&#xff1a; C软件开发、Python机器学习爱好者 &#x1f5e3;️ 互动与支持&#xff1a;&#x1f4ac;评论 &…

C++的第一道门坎:类与对象(二)

一.类中生成的默认成员函数详解 0.类的6个默认成员函数 编译器会给类生成六个默认成员函数&#xff0c;在类中即使我们什么都不做&#xff0c;也会自动生成。 默认成员函数&#xff1a;用户没有显式实现&#xff0c;编译器会自动生成的成员函数称为默认成员函数。 下面我们逐…