Course contentsShow
← Cloud and Infrastructure
Lesson 1447 of 2,98929. Kubernetes ObservabilityFree lesson

Resource Metrics API

Exploring the resource metrics API endpoints, querying pod and node metrics, and understanding metric aggregation.

Resource Metrics API

What you'll learn: How to query real-time resource usage data for pods and nodes through Kubernetes' standardized metrics endpoints.

What Is the Resource Metrics API?

The Resource Metrics API is a lightweight, standardized interface in Kubernetes that exposes CPU and memory usage data for nodes and pods. Think of it as a "live dashboard" that shows you what's happening right now with your cluster's resources—not historical data, just the current snapshot. This API powers tools like kubectl top and enables features like Horizontal Pod Autoscaling.

API Endpoints

The Metrics Server (which you learned to install in the previous lesson) implements two main endpoints:

  • /apis/metrics.k8s.io/v1beta1/nodes – Returns CPU and memory usage for all nodes
  • /apis/metrics.k8s.io/v1beta1/pods – Returns CPU and memory usage for all pods (namespace-scoped)

You can query specific resources by appending their name: /apis/metrics.k8s.io/v1beta1/nodes/{node-name} or /apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pods/{pod-name}.

Understanding Metric Aggregation

For multi-container pods, the API aggregates metrics across all containers. If a pod has three containers using 50m, 100m, and 150m CPU respectively, the pod-level metric shows 300m total. This makes it easy to see per-pod resource consumption without manually summing container metrics.

Metrics are scraped at regular intervals (typically 15-60 seconds) and represent a recent snapshot, not averages over time. The API doesn't store history—it only provides the latest known values.

Querying Metrics

When you run kubectl top nodes or kubectl top pods, you're actually querying these API endpoints under the hood. The same data is available programmatically for autoscalers and custom monitoring solutions.

Key Takeaway: The Resource Metrics API provides standardized, real-time CPU and memory snapshots for nodes and pods through simple endpoints, enabling observability tools and autoscaling features.