当前位置: 首页 > news >正文

【Kubernetes】在 K8s 上部署 Prometheus

如何在 Kubernetes 上高效部署 Prometheus 监控系统?下面将详细介绍如何部署,一起看看吧!

  • Kubernetes:v1.29.0
  • Prometheus:v3.5.0

1、创建命名空间

# 为监控组件创建一个专用命名空间:monitoring
# monitoring-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:name: monitoring# 应用
kubectl apply -f monitoring-namespace.yaml

2、部署 Prometheus

2.1、创建 ConfigMap

# prometheus-config.yaml
# 此处仅添加 prometheus 自己指标
apiVersion: v1
kind: ConfigMap
metadata:name: prometheus-confignamespace: monitoring
data:prometheus.yml: |global:scrape_interval:     15s evaluation_interval: 15sscrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']# 应用
kubectl apply -f prometheus-config.yaml

2.2、创建 ClusterRole 并绑定 ServiceAccount

# prometheus-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: prometheus
rules:
- apiGroups: [""]resources:- nodes- nodes/proxy- services- endpoints- podsverbs: ["get", "list", "watch"]
- apiGroups:- extensionsresources:- ingressesverbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]verbs: ["get"]
---
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: prometheus
subjects:
- kind: ServiceAccountname: prometheusnamespace: monitoring# 应用
kubectl apply -f prometheus-role.yaml

2.3、创建 Deployment

# 数据持久化时(data-volume)使用 PersistentVolume 而不是 emptyDir
# 生产环境中还需配置资源 limits 和 requests
# prometheus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: prometheusnamespace: monitoring
spec:replicas: 1selector:matchLabels:app: prometheustemplate:metadata:labels:app: prometheusspec:serviceAccountName: prometheusserviceAccount: prometheuscontainers:- name: prometheusimage: prom/prometheus:v3.5.0args:- '--config.file=/etc/prometheus/prometheus.yml'- '--web.enable-lifecycle'- '--no-storage.tsdb.wal-compression'ports:- containerPort: 9090protocol: TCPvolumeMounts:- name: prometheus-configmountPath: /etc/prometheus- name: data-volumemountPath: /prometheusvolumes:- name: prometheus-configconfigMap:name: prometheus-config- name: data-volumeemptyDir: {}# 应用
kubectl apply -f prometheus-deployment.yaml

2.4、创建 Service

# 生产环境中,建议使用 Ingress 而不是 NodePort 暴露服务
# prometheus-service.yaml
apiVersion: v1
kind: Service
metadata:name: prometheusnamespace: monitoringlabels:name: prometheus
spec:ports:- name: prometheusprotocol: TCPport: 9090targetPort: 9090selector:app: prometheustype: NodePort# 应用
kubectl apply -f prometheus-service.yaml

3、添加 Target-coredns 案例

  • CoreDNS 内置 Prometheus 监控指标支持(默认 http://<coredns-pod-ip>:9153/metrics
  • 通过配置使用 Prometheus 能获取到这些指标
# CoreDNS 通常已经创建对应的 Service(kube-dns)
# 可通过地址访问:http://kube-dns.kube-system.svc.cluster.local:9153/metrics
# 配置 Prometheus 的 prometheus.yml
apiVersion: v1
kind: ConfigMap
metadata:name: prometheus-confignamespace: monitoring
data:prometheus.yml: |global:scrape_interval:     15s evaluation_interval: 15sscrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']- job_name: 'coredns'metrics_path: '/metrics'static_configs:- targets: ['kube-dns.kube-system.svc.cluster.local:9153']# CoreDNS 提供的主要指标包括
# coredns_dns_requests_total - DNS 请求的总数量
# coredns_dns_request_duration_seconds - 请求处理时间
# coredns_dns_response_size_bytes - 响应大小
# coredns_plugin_enabled - 启用的插件信息
  • 浏览器访问 http:<node-ip>:<prometheus-NodePort>/targets 就可看到添加的 coredns

http://www.dtcms.com/a/338329.html

相关文章:

  • 1-Flask相关知识点
  • 恒创科技:日本服务器 ping 不通?从排查到解决的实用指南
  • 朝阳区24小时图书馆“焕新计划”启幕 文化讲座点亮夜间阅读之光
  • ST05跟踪MRP的运行(MD01)过程
  • 使用chmod 命令修改文件权限
  • 【完整源码+数据集+部署教程】空中目标检测系统源码和数据集:改进yolo11-UniRepLKNetBlock
  • mac 电脑安装类似 nvm 的工具,node 版本管理工具
  • 【机器人-基础知识】ROS2常用命令
  • Vue3 全新特性 defineModel 深度解析
  • CentOS Linux 7 (Core)上部署Oracle 11g、19C RAC详细图文教程
  • 【MySQL】超详细入门学习
  • vue3 + antd modal弹窗拖拽全局封装 使用useDraggable
  • LeetCode100 -- Day1
  • 嵌入式工程师常去的网址
  • 缺陷检测最新综述:针对现实世界工业缺陷检测的综合调查:挑战、方法与展望
  • C++对象的内存布局
  • 拓扑排序详解:从力扣 207 题看有向图环检测
  • 2025年最新美区Apple ID共享账号免费分享(持续更新)
  • 决策树(1)
  • 2025年秋招Java后端面试场景题+八股文题目
  • pandas基本数据
  • 开疆智能Profient转EtherCAT网关连接伦茨变频器配置案例
  • DeepSeek辅助编写的将ET格式文件转换为xls和xlsb格式程序
  • 数据结构:查找表
  • Unity爆炸力场实战指南
  • 94、23种设计模式之工厂方法模式
  • 循序渐进学 Spring (下):从注解、AOP到底层原理与整合实战
  • SpringBoot 自研运行时 SQL 调用树,3 分钟定位慢 SQL!
  • SpringBoot3整合OpenAPI3(Swagger3)完整指南
  • 王树森深度强化学习DRL(三)围棋AlphaGo+蒙特卡洛