Pod: Resources Management

In Kubernetes, we can specify CPU and memory requests and limits for each containers using the resources field in your Deployment or Pod specification.

Why is it important?

If we don't specify a CPU and Memory resource limit, The Container has no upper bound on the resources it can use. The Container could use all of the CPU and Memory resources available on the Node where it is running.

If The Container is running in a namespace that has a default CPU or Memory limit. The Container is automatically assigned the default limit. Cluster administrators can use a LimitRange to specify a default value for the CPU and Memory limit.

Imagine that we have multiple services, and one of our service currently have DDoS attack. If we are not limiting the resources that service's pods will consume all available resource and disrupting other services that we have on that node.

Enabling Metrics Server

First we need to enable minikube metric server by using this command.

➜ minikube addons enable metrics-server
πŸ’‘  metrics-server is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS
    β–ͺ Using image registry.k8s.io/metrics-server/metrics-server:v0.7.2
🌟  The 'metrics-server' addon is enabled

Wait few second until the metrics data populated. We can check the pod resource used by using this command.

➜ kubectl top pod 
NAME                         CPU(cores)   MEMORY(bytes)   
simple-go-685974475c-7484x   1m           2Mi             
simple-go-685974475c-87ccm   1m           2Mi             
simple-go-685974475c-d2kdv   1m           2Mi

Specify Resources Limit

You define resource requests and limits in the container spec within a Pod or Deployment. Let's open our deployment file deployment.yaml. Add resources section in containers server like this.

resources.requests

  • Kubernetes guarantees this amount of resources to the container.

  • Used for Pod scheduling (i.e., to decide which node can run the Pod).

resources.limits

  • Defines the maximum resources the container can use.

  • If the container tries to exceed these limits:

    • Memory: The container is terminated with an OutOfMemory (OOM) error.

    • CPU: The container is throttled (limited to the specified CPU).

Apply and Validate

Let's apply our deployment again using kubectl apply -f deployment.yaml. New sets of pods should be running. An then we can validate if our pods has correct limit by using describe command.

We can see above that resources request and limit are properly set. Next we will learn about horizontal pod scaling (HPA) so our service can spin up new pods if we got increasing traffic that existing pod cannot handle.

Pod Resource

In Kubernetes version v1.32 (still in alpha) we can specify resource allocation in Pod level instead of Container. This is very useful if we have multiple containers, so we only need to specify resource allocation once on Pod level. You can read more about it here.

References

Last updated