One of the most essential parts of Kubernetes administration is to keep an eye on resource usage for your master and worker nodes. Checking how much CPU and RAM resources a pod is utilizing can tell you if further scaling needs to be considered in order to deal with the increased load, or if a new configuration or hardware upgrade is in order. In this tutorial, we will look at different ways to check CPU and RAM usage of a Kubernetes pod on a Linux system.
In this tutorial you will learn:
- How to execute commands on a pod to see resource usage
- How to install Metrics Server and PodMetrics
- How to use Metrics Server and PodMetrics to see pod usage
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Check CPU and RAM usage of a Kubernetes pod
There are several ways to check the CPU and RAM usage of a Kubernetes pod. We have split these methods up into different sections below. You can use whichever one seems most appropriate for your situation, or use all of them if you would like.
Method 1 – command execution
The first method will not require any metrics server or other third party tool. It involves executing commands directly on the pods.
Use the following command to identify the pod you wish to inspect:
$ kubectl get pods
Then, use the following command syntax to open a shell prompt for the desired pod:
$ kubectl exec -it [pod name] -n [namespace] -- /bin/bash
[pod name] with the full name of the pod, and
[namespace] with whatever namespace it is in.
Once you are at the shell prompt, you can execute the
Another handy command to run would be
freeto see current RAM usage:
$ free -h
Method 2 – metrics server
The second method involves installing metrics server and utilizing its tools to check CPU and RAM usage. Execute this command to install it:
$ kubectl create -f https://raw.githubusercontent.com/pythianarora/total-practice/master/sample-kubernetes-code/metrics-server.yaml
Now we can issue the following command to see CPU and RAM usage for all pods:
$ kubectl top pod NAME CPU(cores) MEMORY(bytes) my-wordpress-7fc46dfd4b-pjr7g 9m 366Mi my-wordpress-mariadb-0 10m 69Mi
Or for an individual pod, just specify the name:
$ kubectl top pod my-wordpress-7fc46dfd4b-pjr7g
--containers flag is a good idea in order to get a better idea of what your pod is running:
$ kubectl top pod --containers
Method 3 – pod metrics
Another option is with pod metrics:
$ kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
And then execute:
$ kubectl describe PodMetrics [pod name]
In this tutorial, we saw how to check the CPU and RAM usage of a Kubernetes pod on a Linux system. We are able to use native utilities and Linux commands to check them, but Kubernetes also offers alternatives that can be installed easily enough and then allow us to continually monitor the resource usage of all pods in the cluster. You can choose whichever method suits you best.