Kubernetes Pod Resource Usage Checking

There are so many ways to check pod resource usage such as cpu and memory in kubernetes, kubectl top command or quering from prometheus. In this article, i will describe these two methods to check pod resource usage in kubernetes environment.

Using kubectl top command

beforing running this command, metric server should be ready on your cluster, or you will get the following error message:

W0205 15:14:47.248366    2767 top_pod.go:190] Metrics not available for pod default/podname, age: 190h57m1.248339485s
error: Metrics not available for pod default/podname, age: 190h57m1.248339485s

To intall metric server in your cluster, run the follwing command

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

To check the current CPU and memory usage of a specific pod, you can use the kubectl top pod command followed by the pod name:

kubectl top pod <pod-name>

This command will display the CPU and memory usage of the pod in a tabular format. The columns displayed are:

  • POD – the name of the pod
  • CPU – the CPU usage in cores
  • MEM – the memory usage in bytes

To check the resource usage for all pods in a namespace, you can use the kubectl top pods command:

kubectl top pods --namespace=<namespace-name>

check the resource usage for all pods in all namespaces by ommiting the namespace flag

kubectl top pods

check the resource usage for all pods in all namespaces and sort them by CPU usage using

kubectl top pods --sort-by=cpu

or sort them by memory usage

kubectl top pods --sort-by=memory

You can also use kubectl describe pod to get more detailed information about a pod, including its resource usage, as well as other information such as its IP address, status, and events.

It’s important to note that the kubectl top command does not provide historical data about pod resource usage, for that you need to use a monitoring solution like Prometheus, Grafana, etc.

Querying metrics from prometheus

Prometheus is an open-source monitoring and alerting system. It is designed to collect metrics from different systems and services, and provide a powerful query language for analyzing and visualizing the data. Prometheus is primarily used to monitor and alert on the performance and health of microservices and other distributed systems.

Prometheus has several key features:

  • A simple and efficient data model: Prometheus uses a time-series data model, where each metric is identified by a unique name and a set of key-value pairs called labels.
  • A powerful query language: Prometheus provides a query language called PromQL, which allows users to easily filter and aggregate metrics based on their labels.
  • Automatic service discovery: Prometheus can automatically discover and scrape metrics from services running in a cluster, making it easy to monitor new services as they come online.
  • Alerting: Prometheus includes an alerting system that can send notifications when predefined thresholds are breached.
  • Grafana integration: Prometheus is often used in conjunction with Grafana, a popular open-source tool for visualizing metrics. Prometheus can be used as a data source for Grafana, which allows users to create powerful and flexible dashboards.

Prometheus is widely used in cloud-native environments like Kubernetes, it can scrape metrics from pods, nodes, and even from the kubernetes API server

quering pod memory usage from prometheus with the following PromeSQL

sum(container_memory_working_set_bytes{pod='pod_name',namespace='pod_namespace',container='',}) BY (pod, namespace)

quering pod cpu usage from prometheus with the following PromeSQL

sum(container_cpu_usage_seconds_total{pod='pod_name',namespace='pod_namespace',container!='POD',}) BY (pod, namespace)

Leave a Reply

Your email address will not be published. Required fields are marked *