Finding Per-Core Usage Statistics for a Kubernetes Pod on a Linux Host
In a Kubernetes cluster, it is often important to monitor and analyze the resource usage of individual pods. One key metric that can be useful is per-core usage statistics. This information can help you understand how your applications are utilizing the available resources and optimize performance accordingly.
Using mpstat to Find Per-Core Usage Statistics
One tool that can be used to find per-core usage statistics on a Linux host is mpstat. This command-line utility is part of the sysstat package and provides detailed information about the system's CPU usage.
To use mpstat to find per-core usage statistics for a Kubernetes pod, you will first need to identify the host on which the pod is running. This can be done using the kubectl describe pod command, which will display information about the pod, including the name of the node it is running on.
Once you have identified the host, you can use the following command to find per-core usage statistics for the pod:
mpstat -P ALL
Where is the process ID of the pod, is the time between each sample (in seconds), and is the number of samples to take.
For example, to take 10 samples with a 1-second interval for a pod with a process ID of 1234, you would use the following command:
mpstat -P ALL 1234 1 10
This will produce output similar to the following:
Linux 4.15.0-101-generic (node01) 01/15/2023 _x86_64_ (4 CPU)
01:11:29 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
01:11:29 PM all 0.02 0.00 0.02 0.00 0.00 0.00 0.00 0.00 99.96
01:11:29 PM 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
01:11:29 PM 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
01:11:29 PM 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
01:11:29 PM 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
This output shows the per-core usage statistics for the system, with the first line showing the overall usage and the following lines showing the usage for each individual core.
Assigning CPU Resources to a Pod
Once you have gathered per-core usage statistics for a pod, you may want to assign specific CPU resources to the pod. This can be done using the requests and limits fields in the pod's specification.
For example, to assign 2.5 CPUs to a pod, you would use the following specification:
{
"spec": {
"template": {
"spec": {
"containers": [
{
"name": "my-pod",
"resources": {
"requests": {
"cpu": "2.5"
},
"limits": {
"cpu": "2.5"
}
}
}
]
}
}
}
This will ensure that the pod always has access to 2.5 CPUs, and will not be able to use more than this amount.
In this article, we have discussed how to find per-core usage statistics for a Kubernetes pod on a Linux host using the mpstat command. We have also covered how to assign specific CPU resources to a pod using the requests and limits fields in the pod's specification.