Assigning and Managing CPU and Memory resources in the Kubernetes can be tricky and easy at the same time. Having done this task for numerous customers, I have decided to create a framework zone. I will show you what Kubernetes resources and limits are and how to manage them.

logo-header

The framework contains the following steps.

  • Infographic Guide shows what algorithms to follow to determine and assign the resources and limits.
  • Code templates allow applying those algorithms with minimal adaptation.
  • Algorithms and tools to gather the metrics about resource consumption and set up the limits.
  • Links to the official documentation where you can quickly grab some examples and read more detailed information.

What this article doesn’t contain.

My goal here is simplicity. So you won’t find detailed descriptions of how resources, limit ranges, and quotas work. There are plentiful articles written about it, as well as Kubernetes thorough documentation. Instead, here you will find information on how to quickly start adjusting Kubernetes resources in your projects.

Basics Guide on CPU and Memory Resources

Two cheat-sheets explaining what resources are in terms of Kubernetes, measurement units, the resource state workflow, and some rules on how to apply it.

logo-header

Basics Guide on CPU and Memory Resources

logo-header

Be aware that the CPU calculation formula is not applied to every setup or project. It is used as a starting point in the process of detecting and assigning the resource and limits.

Kubernetes deployment YAML template includes container, resource, and limits definitions.

More detailed information and code snippets:

Resources Quota Guide

logo-header

Two quota templates that contain quotas definition for persistent volume claims and resources in the backed namespace.

You can use kubectl “apply” command to set the quota constraints for a namespace.

kubectl apply -f resource-quota.yaml — namespace backend

Detailed explanation of quotas, metrics, and how to use quota expressions and code samples.

  • https://kubernetes.io/docs/concepts/policy/resource-quotas/

Limit Ranges Guide

logo-header

Two limit range code templates to limit CPU and Memory resources for the containers, and to limit the storage (Persistent Volume Claim). To set this constraint, you can also use kubectl apply -f path/to/file.yaml -n namespace

apiVersion: v1
kind: LimitRange
metadata:
  name: backend-limit-range
spec:
  limits:
  - default:
      memory: 110Mi
      cpu: 500m
    defaultRequest:
      memory: 20Mi
      cpu: 100m
    type: Container

--
apiVersion: v1
kind: LimitRange
metadata:
  name: backend-storage-limits
spec:
  limits:
  - type: PersistentVolumeClaim
    max:
      storage: 5Gi
    min:
      storage: 2Gi

More detailed information and code snippets.

Tools and Frameworks that Helps with Resource and Limits Routine

  • Popeye scans your cluster for potential issues with configuration, resources, and network holes and generates detailed reports with all issues.
  • Goldilocks scans pods for resource limits and creates reports with recommended resources.
  • Kube-advisor simple tool, from the Azure team, that scans pods for missing resources and limits requests.
  • K9s+benchmark — provides a command-line interface (CLI) that allows you to easily manage, monitor, and even benchmark your cluster in your favorite terminal software

Some useful free and open-source tools, which are easy to set up. You can also combine these tools with Datadog, Grafana+Prometeus, Azure Monitor to improve resource and limits monitoring.

Some useful free and open-source tools, which are easy to set up. You can also combine these tools with Datadog, Grafana+Prometeus, Azure Monitor to improve resource and limits monitoring.

Algorithm

  1. Setup Resource Requests. Get information about CPU and Memory usage of specific Application/Container.
  2. Setup Resource Limits. Setup Resource Requests. Run a load test to detect CPU and Memory of a container under the high load..
  3. Monitor containers CPU and Memory usage.
  4. Monitor persistent storage usage.
  5. Check if you can apply resource limit using limit ranges (if you have similar containers, storage set up)
  6. Apply limit range for storage if needed. Or use Quota (it is not recommended to apply Quota for production environment)

Conclusion

I have shared with you the framework I created and use on a daily basis while working with the Kubernetes requests and limits. I hope you will find it useful. Any ideas on how to make it better?