Resource Optimisation Made Simple in Kubernetes Clusters


TL; DR: I made a shell script which prints out the CPU and memory usage vs allocation for each pod in a Kubernetes cluster. Then it’s easy to tell where to optimise. Here it is:

# list all pods and read namespace and pod name as input
kubectl get pods -A --no-headers|while read ns pod a; do
    # gather cpu and memory real time usage using kubectl top command
    # gather cpu and memory allocations using kubectl get pod with jsonpath selected
    # join the 2 line by line by pod name using paste command
	paste <(kubectl top -n $ns pod $pod --containers --no-headers|sort) <(kubectl get pod -n $ns $pod -o jsonpath='{range .spec.containers[*]}{.name}{"\t"}{.resources.requests.cpu}{"\t"}{.resources.requests.memory}{"\n"}{end}'|sort)
	echo ---
done

# sample output
# pod name                   container.      cpu, memory usage          cpu, memory allocation
my-service-8d9fb4b98-nbm7s   my-service      2m    177Mi   	my-service	100m	256Mi
my-service-8d9fb4b98-nbm7s   istio-proxy     10m   57Mi    	istio-proxy	50m	    64Mi

Just in case, you need the metrics-server for the kubectl top command to function.

🙂

,