I need to get resource usage of Pods in a Kubernetes Cluster with kubernetes python client
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want live CPU and memory usage for Pods from Python, the normal Kubernetes core API is not enough by itself. Current usage comes from the Metrics API, which is typically served by metrics-server and exposed under the metrics.k8s.io API group.
That means the Python client can retrieve the data, but only if metrics are actually available in the cluster. The basic flow is: load config, call the metrics endpoint through CustomObjectsApi, and parse the returned CPU and memory quantities.
This distinction matters because many developers first inspect Pod status and assume usage should be there. In Kubernetes, desired resources and observed resources are exposed through different APIs on purpose.
Prerequisite: Metrics Server Must Exist
Before writing Python code, confirm that the metrics API is installed:
If that command fails, your Python code will fail for the same reason. kubectl top and the Python client both depend on the same metrics pipeline.
Query Pod Metrics with the Python Client
The Kubernetes Python client exposes arbitrary API groups through CustomObjectsApi. Pod metrics usually live at metrics.k8s.io/v1beta1.
If your code runs inside the cluster, replace load_kube_config() with load_incluster_config().
Parse CPU and Memory Quantities
The response contains per-container usage, with values such as 25m for CPU or 128Mi for memory. You usually want to sum container usage per Pod.
parse_quantity converts Kubernetes quantity strings into numeric values. That is much safer than hand-parsing suffixes yourself.
Get Metrics for One Pod
If you only need one Pod, list the namespace and filter by name:
The Metrics API is list-oriented, so filtering in Python is common. For large clusters, you may want to reduce scope to one namespace instead of asking for the whole cluster.
Understand What This Data Represents
These metrics are near-real-time usage, not resource requests or limits. If you need declared resources from the Pod spec, use CoreV1Api:
That gives you the configured resource envelope, while the Metrics API gives you observed usage. In production monitoring, you usually need both.
Common Pitfalls
- Using
CoreV1Apiand expecting current CPU and memory usage. The core API exposes specs and status, not usage metrics. - Forgetting to install
metrics-server, which makes the metrics API unavailable. - Summing per-container values incorrectly. Pod metrics are usually the total of all its containers.
- Confusing CPU millicores such as
250mwith whole cores. Quantity parsing matters. - Ignoring RBAC. A service account may be allowed to read Pods but not metrics endpoints.
Summary
- Live Pod usage comes from the Metrics API, not the standard core Pod API.
- Use
CustomObjectsApiagainstmetrics.k8s.io/v1beta1. - Confirm metrics availability first with
kubectl top. - Sum container usage to compute total Pod usage.
- Use
CoreV1Apiseparately when you need requests and limits instead of live consumption.

