当前位置: 首页 > 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.lryc.cn/news/624246.html

相关文章:

  • C语言基础:变量与进制详解
  • K8s的命名空间需要创建吗
  • 工具集成强化学习:AI数学推理能力的新跃迁
  • Java基础(九):Object核心类深度剖析
  • 图神经网络分享系列-node2vec(二)
  • 基于51单片机WIFI心率计脉搏体温测量仪APP设计
  • HTML应用指南:利用POST请求获取全国华为旗舰店门店位置信息
  • 《若依》权限控制
  • 上下文切换及线程操作相关内容
  • 学习雪花算法
  • linux-高级IO(中)
  • 【BFS 动态规划】P12382 [蓝桥杯 2023 省 Python B] 树上选点|普及+
  • Redis面试精讲 Day 25:Redis实现分布式Session与购物车
  • 【前端】使用Vue3过程中遇到加载无效设置点击方法提示不存在的情况,原来是少加了一个属性
  • [激光原理与应用-296]:理论 - 非线性光学 - 线性光学与非线性光学对比
  • (第十九期)用 VS Code 管理项目:目录文件夹与根目录,一次讲清
  • Vulkan笔记(五)-逻辑层与队列
  • halcon基于透视的可变形模型匹配
  • C预备知识01:
  • 数字电视:技术演进与未来展望
  • 用户认证技术
  • MySQL 函数大赏:聚合、日期、字符串等函数剖析
  • 静配中心配药智能化:基于高并发架构的Go语言实现
  • CPP异常
  • 新手向:Java方向讲解
  • 数据挖掘 3.5 支持向量机——边界和正则化
  • C++ const
  • CSDN转PDF【无水印且免费!!!】
  • 计算机网络:2、TCP和UDP
  • 代码随想录刷题Day36