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复习的难点其实在于学好数据结构和计算机组成…

python对一组数据进行描述性统计

描述性统计 描述性统计是一种统计学方法,用于总结和描述数据的主要特征。它可以帮助我们了解数据的中心位置、分散程度和形状等信息。以下是一些常用的描述性统计量: 中心趋势的度量: 1、均值(Mean):所有…

rk3568_semaphore

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

C语言:创建简单的流媒体服务器来播放.flv文件

编写 httpFlv_server.cpp 如下 #include <iostream> #include <stdint.h> #include <stdlib.h> #include <string.h> #include <WinSock2.h> #include <WS2tcpip.h> #pragma comment(lib, "ws2_32.lib")char* get_filename(c…

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

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

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

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

【AI学习】什么是token?

在近期举办的Stripe Sessions用户大会上&#xff0c;移动支付巨头Stripe的联合创始人兼CEO帕特里克克里森&#xff08;Patrick Collison&#xff09;&#xff0c;与英伟达CEO黄仁勋进行了一场炉边对话。 黄仁勋提到&#xff1a;“我们正在经历一场前所未有的工业革命。这次工业…

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

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

MySQL 满足条件函数中使用查询最大值函数

在实际的数据库操作中&#xff0c;我们常常需要根据某些条件找到最大值并据此进行下一步的操作。例如&#xff0c;在一个包含订单信息的表中&#xff0c;可能需要找到特定客户的最大订单金额&#xff0c;并据此进行某些统计或决策。MySQL 提供了多种函数和查询方法&#xff0c;…

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驱动的时候遇到需要互斥的地方建议使用…

css3 笔记01

目录 01 css3选择器 02 伪类选择器 03 伪元素选择器 04 文章分割列 05 浏览器hack写法 06 css3中的2D转换 01 css3选择器 选择器选择器{} 表示选择下一个兄弟 选择器1~选择器2{} 表示选中选择1后面所有包含选择器2的元素. [属性名]{} 包含属性名的选择器 选择器[属性名]…

Linux用户管理指令004

1、useradd 是用于创建新用户的命令。以下是 useradd 命令的一些常用选项及其简要说明&#xff0c;以清晰的分点方式表示&#xff1a; -u, --uid UID指定用户的用户ID&#xff08;UID&#xff09;。如果不指定&#xff0c;系统通常会分配一个未使用的最小UID。 -g, --gid GROUP…

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…