K8S 部署peometheus + grafana 监控

news2024/12/25 9:33:26

安装说明

如果有下载不下来的docker镜像可以私信我免费下载。
系统版本为 Centos7.9
内核版本为 6.3.5-1.el7
K8S版本为 v1.26.14
动态存储:部署文档
GitHub地址

下载yaml 文件

## 因为我的K8S 版本比较新,我下载的是当前的最新版本,你的要是老版本K8集群最好看一下那个版本是支持你的集群的。
git clone -b release-0.13 https://github.com/prometheus-operator/kube-prometheus.git

安装operator

cd ./kube-prometheus-release-0.13/manifests/setup
kubectl create -f ./

cd /root/kube-prometheus-release-0.13/manifests
kubectl create -f ./

注意事项

默认的 yaml 文件里面有网络隔离配置,配置文件名字如下

[root@master01 manifests]#ls *networkPolicy.yaml
alertmanager-networkPolicy.yaml      grafana-networkPolicy.yaml           nodeExporter-networkPolicy.yaml       prometheus-networkPolicy.yaml
blackboxExporter-networkPolicy.yaml  kubeStateMetrics-networkPolicy.yaml  prometheusAdapter-networkPolicy.yaml  prometheusOperator-networkPolicy.yaml

正常按照默认的部署是不能访问这个监控的,需要修改配置,
举例:以 grafana-networkPolicy.yaml 配置文件为例(ingress-nginx 方式暴露服务,所以添加ingress-nginx 命名空间的pod允许访问Grafana即可。)

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  labels:
    app.kubernetes.io/component: grafana
    app.kubernetes.io/name: grafana
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 11.1.0
  name: grafana
  namespace: monitoring
spec:
  egress:
  - {}
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app.kubernetes.io/name: prometheus
    - namespaceSelector: ## 添加允许访问策略
        matchLabels:
          name: ingress-nginx
    ports:
    - port: 3000
      protocol: TCP
  podSelector:
    matchLabels:
      app.kubernetes.io/component: grafana
      app.kubernetes.io/name: grafana
      app.kubernetes.io/part-of: kube-prometheus
  policyTypes:
  - Egress
  - Ingress

保存后重新部署,其他的网络隔离也是这样修改,此处不再一一举例。

kubectl replace -f grafana-networkPolicy.yaml

或者嫌麻烦可以直接删除 所有 networkPolicy

## 谨慎使用,当前命令会删除指定命名空间中所有networkPolicy 配置。
kubectl delete netpol -n monitoring --all

修改 prometheus-prometheus.yaml 文件

默认这个是没有持久化存储的并且prometheus 监控数据只能保存24小时,下面这个是我修改过的,修改监控数据保存时间,添加动态存储。

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  labels:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 2.46.0
  name: k8s
  namespace: monitoring
spec:
  alerting:
    alertmanagers:
    - apiVersion: v2
      name: alertmanager-main
      namespace: monitoring
      port: web
  enableFeatures: []
  externalLabels: {}
  image: quay.io/prometheus/prometheus:v2.46.0
  nodeSelector:
    kubernetes.io/os: linux
  podMetadata:
    labels:
      app.kubernetes.io/component: prometheus
      app.kubernetes.io/instance: k8s
      app.kubernetes.io/name: prometheus
      app.kubernetes.io/part-of: kube-prometheus
      app.kubernetes.io/version: 2.46.0
  podMonitorNamespaceSelector: {}
  podMonitorSelector: {}
  probeNamespaceSelector: {}
  probeSelector: {}
  replicas: 1
  resources:
    requests:
      memory: 1000Mi
  ruleNamespaceSelector: {}
  ruleSelector: {}
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  ## 在这里添加持久化存储,我这里用的是动态存储。
  storage:
    volumeClaimTemplate:
      metadata:
        name: prometheus-k8s-db
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: rook-ceph-rbd
        resources:
          requests:
            storage: 50Gi
## 修改默认保存的监控数据为180天
  retention: 180d   
  version: 2.46.0

修改好以后重新部署

添加 Prometheus Ingress访问

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: prometheus-ingress
  namespace: monitoring
  labels:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 2.46.0
    operator.prometheus.io/mode: server
spec:
  ingressClassName: nginx
  rules:
  - host: prometheus.demo.cn
    http:
      paths:
      - backend:
          service:
            name: prometheus-k8s
            port:
              name: web
        path: /
        pathType: Prefix
  tls:
  - hosts:
    - prometheus.demo.cn
    secretName: prometheus.demo.cn

配置好host 直接用域名访问

查看prometheus Targets

没问题的话就继续。
在这里插入图片描述

metrics-server 服务异常

[root@master01 data]# kubectl top node
error: Metrics API not available

解决办法

部署 Grafana 监控面板

修改 Grafana yaml 文件 添加存储,默认的配置是没有存储的。

## 创建PVC存储给 Grafana 使用
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-data-pvc
  namespace: monitoring
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: rook-ceph-rbd
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/component: grafana
    app.kubernetes.io/name: grafana
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 9.5.3
  name: grafana
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/component: grafana
      app.kubernetes.io/name: grafana
      app.kubernetes.io/part-of: kube-prometheus
  template:
    metadata:
      annotations:
        checksum/grafana-config: 5c598ba58d9b65011bdbb3864138399a
        checksum/grafana-dashboardproviders: c9c1743868aa1c3dab60d2c402e2dcf0
        checksum/grafana-datasources: 5ef0e6acaa5b4e8603740fbad440717d
      labels:
        app.kubernetes.io/component: grafana
        app.kubernetes.io/name: grafana
        app.kubernetes.io/part-of: kube-prometheus
        app.kubernetes.io/version: 9.5.3
    spec:
      automountServiceAccountToken: false
      containers:
      - env: []
        image: grafana/grafana:9.5.3
        name: grafana
        ports:
        - containerPort: 3000
          name: http
        readinessProbe:
          httpGet:
            path: /api/health
            port: http
        resources:
          limits:
            cpu: 200m
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
          readOnlyRootFilesystem: true
          seccompProfile:
            type: RuntimeDefault
        volumeMounts:
        - mountPath: /var/lib/grafana
          name: grafana-storage
          readOnly: false
        - mountPath: /etc/grafana/provisioning/datasources
          name: grafana-datasources
          readOnly: false
        - mountPath: /etc/grafana/provisioning/dashboards
          name: grafana-dashboards
          readOnly: false
        - mountPath: /tmp
          name: tmp-plugins
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/alertmanager-overview
          name: grafana-dashboard-alertmanager-overview
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/apiserver
          name: grafana-dashboard-apiserver
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/cluster-total
          name: grafana-dashboard-cluster-total
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/controller-manager
          name: grafana-dashboard-controller-manager
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/grafana-overview
          name: grafana-dashboard-grafana-overview
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-cluster
          name: grafana-dashboard-k8s-resources-cluster
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-multicluster
          name: grafana-dashboard-k8s-resources-multicluster
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-namespace
          name: grafana-dashboard-k8s-resources-namespace
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-node
          name: grafana-dashboard-k8s-resources-node
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-pod
          name: grafana-dashboard-k8s-resources-pod
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-workload
          name: grafana-dashboard-k8s-resources-workload
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/k8s-resources-workloads-namespace
          name: grafana-dashboard-k8s-resources-workloads-namespace
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/kubelet
          name: grafana-dashboard-kubelet
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/namespace-by-pod
          name: grafana-dashboard-namespace-by-pod
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/namespace-by-workload
          name: grafana-dashboard-namespace-by-workload
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/node-cluster-rsrc-use
          name: grafana-dashboard-node-cluster-rsrc-use
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/node-rsrc-use
          name: grafana-dashboard-node-rsrc-use
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/nodes-darwin
          name: grafana-dashboard-nodes-darwin
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/nodes
          name: grafana-dashboard-nodes
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/persistentvolumesusage
          name: grafana-dashboard-persistentvolumesusage
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/pod-total
          name: grafana-dashboard-pod-total
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/prometheus-remote-write
          name: grafana-dashboard-prometheus-remote-write
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/prometheus
          name: grafana-dashboard-prometheus
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/proxy
          name: grafana-dashboard-proxy
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/scheduler
          name: grafana-dashboard-scheduler
          readOnly: false
        - mountPath: /grafana-dashboard-definitions/0/workload-total
          name: grafana-dashboard-workload-total
          readOnly: false
        - mountPath: /etc/grafana
          name: grafana-config
          readOnly: false
      nodeSelector:
        kubernetes.io/os: linux
      securityContext:
        fsGroup: 65534
        runAsNonRoot: true
        runAsUser: 65534
      serviceAccountName: grafana
      volumes:
      ## 添加PVC存储
      - name: grafana-storage
        persistentVolumeClaim:
          claimName: grafana-data-pvc
      - name: grafana-datasources
        secret:
          secretName: grafana-datasources
      - configMap:
          name: grafana-dashboards
        name: grafana-dashboards
      - emptyDir:
          medium: Memory
        name: tmp-plugins
      - configMap:
          name: grafana-dashboard-alertmanager-overview
        name: grafana-dashboard-alertmanager-overview
      - configMap:
          name: grafana-dashboard-apiserver
        name: grafana-dashboard-apiserver
      - configMap:
          name: grafana-dashboard-cluster-total
        name: grafana-dashboard-cluster-total
      - configMap:
          name: grafana-dashboard-controller-manager
        name: grafana-dashboard-controller-manager
      - configMap:
          name: grafana-dashboard-grafana-overview
        name: grafana-dashboard-grafana-overview
      - configMap:
          name: grafana-dashboard-k8s-resources-cluster
        name: grafana-dashboard-k8s-resources-cluster
      - configMap:
          name: grafana-dashboard-k8s-resources-multicluster
        name: grafana-dashboard-k8s-resources-multicluster
      - configMap:
          name: grafana-dashboard-k8s-resources-namespace
        name: grafana-dashboard-k8s-resources-namespace
      - configMap:
          name: grafana-dashboard-k8s-resources-node
        name: grafana-dashboard-k8s-resources-node
      - configMap:
          name: grafana-dashboard-k8s-resources-pod
        name: grafana-dashboard-k8s-resources-pod
      - configMap:
          name: grafana-dashboard-k8s-resources-workload
        name: grafana-dashboard-k8s-resources-workload
      - configMap:
          name: grafana-dashboard-k8s-resources-workloads-namespace
        name: grafana-dashboard-k8s-resources-workloads-namespace
      - configMap:
          name: grafana-dashboard-kubelet
        name: grafana-dashboard-kubelet
      - configMap:
          name: grafana-dashboard-namespace-by-pod
        name: grafana-dashboard-namespace-by-pod
      - configMap:
          name: grafana-dashboard-namespace-by-workload
        name: grafana-dashboard-namespace-by-workload
      - configMap:
          name: grafana-dashboard-node-cluster-rsrc-use
        name: grafana-dashboard-node-cluster-rsrc-use
      - configMap:
          name: grafana-dashboard-node-rsrc-use
        name: grafana-dashboard-node-rsrc-use
      - configMap:
          name: grafana-dashboard-nodes-darwin
        name: grafana-dashboard-nodes-darwin
      - configMap:
          name: grafana-dashboard-nodes
        name: grafana-dashboard-nodes
      - configMap:
          name: grafana-dashboard-persistentvolumesusage
        name: grafana-dashboard-persistentvolumesusage
      - configMap:
          name: grafana-dashboard-pod-total
        name: grafana-dashboard-pod-total
      - configMap:
          name: grafana-dashboard-prometheus-remote-write
        name: grafana-dashboard-prometheus-remote-write
      - configMap:
          name: grafana-dashboard-prometheus
        name: grafana-dashboard-prometheus
      - configMap:
          name: grafana-dashboard-proxy
        name: grafana-dashboard-proxy
      - configMap:
          name: grafana-dashboard-scheduler
        name: grafana-dashboard-scheduler
      - configMap:
          name: grafana-dashboard-workload-total
        name: grafana-dashboard-workload-total
      - name: grafana-config
        secret:
          secretName: grafana-config

修改好以后重新部署

添加 Grafana Ingress 访问

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: grafana-ingress
  namespace: monitoring
  labels:
    app.kubernetes.io/component: grafana
    app.kubernetes.io/name: grafana
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 9.5.3
spec:
  ingressClassName: nginx
  rules:
  - host: grafana.demo.cn
    http:
      paths:
      - backend:
          service:
            name: grafana
            port:
              number: 3000
        path: /
        pathType: Prefix
  tls:
  - hosts:
    - grafana.demo.cn
    secretName: grafana.demo.cn

查看 Grafana 页面

默认账号密码都是admin,首次登陆需要修改密码。部署好以后会有一个默认的配置比如已经添加好了数据源和Dashboard等。
在这里插入图片描述
到此全部部署完成,监控页面有些课鞥呢需要自己自定义配置这里就不再叙述了。

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

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

相关文章

【10.PIE-Engine案例———— 加载Terra星全球1km地表温度和发射率每日天合成产品(MOD11A1 V61)数据集】

加载Terra星全球1km地表温度和发射率每日天合成产品(MOD11A1 V61)数据集 原始路径 欢迎大家登录航天宏图官网查看本案例原始来源 最终结果 具体代码 /*** File : MOD11A1* Time : 2020/7/21* Author : piesat* Version : 1.0* Contact : 400-890-0662* Li…

6.3 面向对象技术-设计模式

设计模式 创建型模式 结构型模式

【cuda】在老服务器上配置CUDA+cmake开发环境

在老服务器上配置CUDA+cmake开发环境 服务器x86_64,系统是centos8,cmake版本是2.8.10 背景 不能更换服务器系统无法下载CUDA安装包解决思路 使用可以至此CUDA开发的较老的cmake直接移植CUDA环境配置环境中遇到的问题 服务器无法编译cmake移植CUDA编译器及部分库,代码无法…

Linux学习笔记 --- 环境配置

在成功装载Ubuntu系统后我们需要设置其与windows系统的共享文件夹,按照以下步骤操作 设置完共享文件夹后在终端执行以下命令查看是否成功设置 此时下方出现设置的共享文件夹名称则为成功设置 如果未显示可以尝试进行重新安装VMware tools,步骤如下&…

[论文笔记]一种超越思维链的提示策略——少到多提示

引言 今天带来另一种提示策略论文笔记:LEAST-TO-MOST PROMPTING ENABLES COMPLEX REASONING IN LARGE LANGUAGE MODELS。 思维链提示在各种自然语言推理任务中表现出了显著的性能。然而,在需要解决比提示中示例更难的问题时,它的表现往往较…

1.STM32CubeMX软件安装及工程创建

软件安装及工程创建 1. 安装STM32CubeMX2.创建工程 1. 安装STM32CubeMX 链接:https://pan.baidu.com/s/1kubaMuiOBe8Vg6mQsFMYMw?pwdchdj 提取码:chdj 安装STM32cubemx(中间可能会出现一些需要下载的情况,等待完成即可&#xf…

论文精读(保姆级解析)—— FreeDoM: Training-Free Energy-Guided Conditional Diffusion Model

目录 0 前言1 文章摘要2 引言3 相关工作3.1 需要训练的扩散方法3.2 无需训练的扩散方法 4 预备知识4.1基于分数的扩散模型4.2 条件分数函数4.3 能量扩散引导 5 提出方法5.1 近似时间相关能量5.2 有效的时间旅行策略5.3 能量函数构造5.3.1 单一条件引导5.3.2 多条件引导5.3.3 潜…

嵌入式C++、STM32、MySQL、GPS、InfluxDB和MQTT协议数据可视化:智能物流管理系统设计思路流程(附代码示例)

目录 项目概述 系统设计 硬件设计 软件设计 系统架构图 代码实现 1. STM32微控制器与传感器代码 代码讲解 2. MQTT Broker设置 3. 数据接收与处理 代码讲解 4. 数据存储与分析 5. 数据分析与可视化 代码讲解 6. 数据可视化 项目总结 项目概述 随着电子商务的快…

Week 3 DAY 5:

Game on Ranges - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) Game on Ranges 这道题题目意思有点难理解,加上是英文提面,这里解释一下,有两个人A和B, 一开始给定一个n,是1到n的排列,A人负责选取区间,…

数模打怪(八)之图论模型

一、作图 图的数学语言描述: G( V(G), E(G) ),G(graph):图,V(vertex):顶点集,E(edge):边集 1、在线作图 https://csac…

第G4周:CGAN|生成手势图像 | 可控制生成

本文为🔗365天深度学习训练营 中的学习记录博客 原作者:K同学啊 理论知识: 条件生成对抗网络(CGAN)是在生成对抗网络(GAN)的基础上进行了一些改进。对于原始GAN的生成器而言,其生成的…

C#基础——类、构造函数和静态成员

类 类是一个数据类型的蓝图。构成类的方法和变量称为类的成员,对象是类的实例。类的定义规定了类的对象由什么组成及在这个对象上可执行什么操作。 class 类名 { (访问属性) 成员变量; (访问属性) 成员函数; } 访问属性:public(公有的&…

MinIO对象生命周期

Object Management — MinIO Object Storage for Windowshttps://min.io/docs/minio/windows/administration/object-management.html 1.概念 MinIO 对象生命周期是指对存储在 MinIO 中的对象进行自动管理的一套策略。这些策略可以用于自动删除旧对象、转移对象到不同的存储类别…

Golang高效合并(拼接)多个gzip压缩文件

有时我们可能会遇到需要把多个 gzip 文件合并成单个 gzip 文件的场景,最简单最容易的方式是把每个gzip文件都先解压,然后合并成一个文件后再次进行压缩,最终得到我们想要的结果,但这种先解压后压缩的方式显然效率不高,…

day08:订单状态定时处理、来单提醒和客户催单

文章目录 Spring Task介绍cron表达式入门案例 订单状态定时处理需求分析代码开发扩展 WebSocket介绍入门案例特点 来单提醒需求分析和设计代码实现 客户催单需求分析和设计代码实现 Spring Task 介绍 Spring Task 是Spring框架提供的任务调度工具,可以按照约定的时…

爬虫提速!用Python实现多线程下载器!

✨ 内容: 在网络应用中,下载速度往往是用户体验的关键。多线程下载可以显著提升下载速度,通过将一个文件分成多个部分并行下载,可以更高效地利用带宽资源。今天,我们将通过一个实际案例,学习如何用Python实…

C++ | Leetcode C++题解之第292题Nim游戏

题目: 题解: class Solution { public:bool canWinNim(int n) {return n % 4 ! 0;} };

如何使用API快速打造健康医疗系统?

在数字医疗市场,数据是人们经常谈及的一个话题。当前,消费者医疗和健康应用收集的数据越来越多,电子健康记录的实施也创造出了大量有关病人的电子信息。 API接口在智慧医院跨网、跨机构之间的业务协同和数据共享交换中得到数据共享。支撑了医…

鸿蒙APP架构及开发入门

1.鸿蒙系统 1.1 什么是鸿蒙 鸿蒙是一款面向万物互联时代的、全新的分布式操作系统。 在传统的单设备系统能力基础上,鸿蒙提出了基于同一套系统能力、适配多种终端形态的分布式理念,能够支持手机、平板、智能穿戴、智慧屏、车机、PC、智能音箱、耳机、…

【数学建模】权重生成与评价模型(上)

文章目录 权重生成与评价模型(上)1. 层次分析法1.1 层次分析法的原理构建判断矩阵权重向量计算一致性检验 1.2 层次分析法的案例1. 建立层次结构2. 构建判断矩阵3. 计算权重向量4. 一致性检验5. 计算综合权重 1.3 另一种得出综合得分的方法例子计算步骤完…