🌐 什么是 Prometheus?

Prometheus 是一个开源的监控与告警系统,专为云原生环境设计,支持动态指标采集和实时可视化。在 Kubernetes 集群中,它常用于监控节点、容器、服务等资源。

🧩 核心特性

  • 自动发现服务:通过 Service Discovery 动态抓取目标
  • 灵活查询语言:使用 PromQL 进行复杂指标分析
  • 告警规则引擎:支持自定义阈值和通知策略
  • 时序数据库:高效存储和查询历史数据

🛠 安装 Prometheus

方法一:使用 Helm Chart

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/prometheus

🔗 [了解更多 Helm 安装细节](/community/abc_compute_forum/learning/k8s/ helm)

方法二:直接部署 YAML

apiVersion: v1
kind: Namespace
metadata:
  name: monitoring
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prometheus/prometheus:v2.34.0
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: prometheus-config
          mountPath: /etc/prometheus
          subPath: prometheus.yml
      volumes:
      - name: prometheus-config
        configMap:
          name: prometheus-config

📈 配置监控目标

示例配置

global:
  scrape_interval: 30s

scrape_configs:
  - job_name: 'kubernetes-apiservers'
    kubernetes_sd_configs:
    - role: endpoints
    scheme: https
    bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
    relabel_configs:
    - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
      target_label: __address__
      regex: "(.+)|(.+)|(.+);9090"

🧪 实战场景

监控 Kubernetes 节点

# 查看节点 CPU 使用率
node_cpu_seconds_total{mode="idle"} 

# 查看内存使用情况
node_memory_MemTotal_bytes node_memory_MemFree_bytes

# 查看磁盘 I/O
node_disk_read_bytes_total{device!=""} 

📖 推荐学习路径

🔗 深入理解 Kubernetes 监控体系
🔗 Prometheus 高级查询技巧
🔗 Grafana 可视化配置教程

Kubernetes_Prometheus_Architecture
Prometheus_Monitoring_Dashboard