如何通过Prometheus与Metric Server收集部署所需内存总量(无需Heapster)
Got it, let's break this down for you. Since Heapster is deprecated, you're right to shift to Metric Server + Prometheus—but the key thing to note here is that Metric Server only handles real-time resource usage metrics, not the static memory requests/limits defined in your deployments. To get those totals, you'll need to pair Prometheus with kube-state-metrics, which exposes metadata about Kubernetes objects (including deployment resource specs).
Step 1: Ensure kube-state-metrics is deployed
First, you need kube-state-metrics running in your cluster—it's the component that scrapes the Kubernetes API server for resource metadata and exposes it as Prometheus metrics. Most clusters don't have this installed by default, so you'll need to add it (via Helm, Kubernetes YAML manifests, or your cluster's package manager).
Step 2: Use PromQL queries to calculate totals
Once kube-state-metrics is up and Prometheus is scraping its metrics endpoint (usually http://kube-state-metrics.kube-system.svc.cluster.local:8080/metrics), you can use these queries to get the sums you need:
Total memory requests for a specific deployment
sum(kube_pod_container_resource_requests_memory_bytes{namespace="your-target-namespace", deployment="your-deployment-name"})
Total memory limits for a specific deployment
sum(kube_pod_container_resource_limits_memory_bytes{namespace="your-target-namespace", deployment="your-deployment-name"})
Group totals by deployment (for all deployments in a namespace)
If you want to see the sum per deployment instead of a single one:
sum by (deployment) (kube_pod_container_resource_requests_memory_bytes{namespace="your-target-namespace"})
Quick notes on the queries
- The metrics
kube_pod_container_resource_requests_memory_bytesandkube_pod_container_resource_limits_memory_bytesare exposed for every container in every Pod, with labels likedeployment,namespace, andpodto filter by. - Double-check the label keys if you're using an older version of
kube-state-metrics—some versions usekubernetes_deploymentinstead of justdeployment. You can verify this by searching for the metric directly in your Prometheus UI.
What about Metric Server?
As mentioned earlier, Metric Server doesn't store or expose the static requests/limits data—it's designed to provide real-time usage metrics (like container_memory_usage_bytes) to Kubernetes components like HPA. So you can't use it alone to calculate the total requested/limited memory, but it works great alongside Prometheus for tracking actual usage vs. allocated resources.
Hope this solves your problem! Let me know if you hit any snags with deploying kube-state-metrics or adjusting the queries.
内容的提问来源于stack exchange,提问作者Jxadro

