1. 概述

随着云原生技术的发展,Kubernetes 成为了容器化应用管理的首选平台。但平台能力越强,运维的责任也越大 —— 我们需要时刻关注应用的资源使用情况,确保其稳定运行。

Prometheus 作为一款开源的监控工具,能持续采集各类指标,帮助我们了解应用在 CPU 和内存等关键资源上的消耗情况。本文将介绍如何通过 Prometheus 监控 Kubernetes Pod 的 CPU 和内存使用情况。

✅ 目标读者:熟悉 Kubernetes 和 Prometheus 基本概念的开发人员或运维人员。


2. 在 Kubernetes 中部署 Prometheus

在 Kubernetes 中快速部署 Prometheus 的最简单方式之一是使用 Helm,类似于 Linux 中的 apt 或 yum。

安装步骤如下:

  1. 安装 Helm(略过,参考官方文档)
  2. 添加 Prometheus Helm 仓库:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  1. 更新 Helm 本地仓库:
helm repo update
  1. 创建自定义配置文件 values.yaml,用于启用对 Kubernetes Pod 的指标采集:
server:
  extraScrapeConfigs: |
    - job_name: 'kubernetes-pods'
      kubernetes_sd_configs:
        - role: pod
      relabel_configs:
        - source_labels: [__meta_kubernetes_pod_label_app]
          action: keep
          regex: .+
  1. 安装 Prometheus:
helm install prometheus -f values.yaml prometheus-community/prometheus

✅ 说明:该命令将根据自定义配置安装 Prometheus,使其能够采集集群中所有 Pod 的指标。


3. Prometheus 中的 CPU 与内存指标解析

3.1 CPU 指标

  • container_cpu_usage_seconds_total
    表示容器累计使用的 CPU 时间(单位:秒)。由于是累计值,通常配合 rate() 函数使用来计算单位时间内的 CPU 使用率。

3.2 内存指标

  • container_memory_working_set_bytes
    表示容器当前正在使用的内存大小(单位:字节),包括运行时所需内存和缓存。

3.3 资源限制指标

  • kube_pod_container_resource_limits
    表示容器的资源限制(CPU 和内存),可用于计算使用率占限制的百分比。

4. 在 Kubernetes 中部署一个 Web 应用并启用 Prometheus 监控

4.1 创建一个 NGINX Pod

我们以一个简单的 NGINX 应用为例,创建一个 Pod 并设置资源限制:

# web-app.yaml
apiVersion: v1
kind: Pod
metadata:
  name: web-app
  labels:
    app: web-app
spec:
  containers:
  - name: web-app
    image: nginx:latest
    ports:
    - containerPort: 80
    resources:
      limits:
        cpu: "1"
        memory: "512Mi"
      requests:
        cpu: "0.5"
        memory: "256Mi"

部署命令:

kubectl apply -f web-app.yaml

4.2 创建服务暴露 Pod

kubectl expose pod web-app --type=NodePort --name web-app-service --port=80

获取访问地址:

minikube service web-app-service

4.3 访问 Prometheus 界面

端口转发:

kubectl port-forward svc/prometheus-server 9090:80

访问地址:http://localhost:9090


5. 编写 Prometheus 查询语句监控 CPU 使用情况

5.1 基础 CPU 使用查询

rate(container_cpu_usage_seconds_total{pod="web-app"}[5m])

⚠️ 说明:rate() 函数用于计算单位时间内的 CPU 使用增长率。

5.2 CPU 使用百分比

100 * (
  sum(rate(container_cpu_usage_seconds_total{pod="web-app"}[5m])) by (pod)
  /
  sum(kube_pod_container_resource_limits{pod="web-app", resource="cpu"}) by (pod)
)

✅ 该查询将 CPU 使用率换算为占 Pod 限制的百分比,便于直观理解资源使用情况。


6. 编写 Prometheus 查询语句监控内存使用情况

6.1 基础内存使用查询

sum(container_memory_working_set_bytes{pod="web-app"})

⚠️ 说明:该查询返回当前 Pod 的内存使用总量(单位:字节)。

6.2 内存使用百分比

100 * (
  sum(container_memory_working_set_bytes{pod="web-app"}) by (pod)
  /
  sum(kube_pod_container_resource_limits{resource="memory", pod="web-app"}) by (pod)
)

✅ 该查询返回内存使用量占 Pod 限制的百分比,便于评估资源是否接近瓶颈。


7. 总结

本文介绍了如何使用 Prometheus 监控 Kubernetes Pod 的 CPU 和内存使用情况。通过 Prometheus 提供的指标和 PromQL 查询语言,我们可以:

  • 实时了解 Pod 的资源消耗
  • 分析资源使用趋势
  • 评估资源限制是否合理

这些信息对于优化 Kubernetes 部署、提升系统稳定性具有重要意义。


💡 小贴士:

  • Prometheus 抓取的指标依赖于 Kubernetes 的元数据标签,确保你的 Pod 有合理的 app 标签。
  • 如果你使用的是云厂商托管的 Kubernetes(如 EKS、GKE、ACK),Prometheus 的部署方式可能略有不同,但核心指标和查询逻辑保持一致。
  • 建议将关键查询保存为 Grafana 面板,方便长期监控与报警设置。

原始标题:How to Get the CPU and Memory Usage of a Kubernetes Pod Using Prometheus