Easy and Fast Adjustment of Kubernetes CPU and Memory
Tags: kubernetes, cpumemory, aks
Published: 2021-02-17 13:39:12

---
title: Easy and Fast Adjustment of Kubernetes CPU and Memory
published: true
description: 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.
tags: kubernetes, cpumemory, aks
---

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](https://miro.medium.com/max/1350/1*0raPh3cQoHTCqatZDBQA5A.png "Log 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](https://miro.medium.com/max/1500/1*yjMSe7VDWmTxNogGcQxytQ.png "req-limits") 

# Basics Guide on CPU and Memory Resources

![logo-header](https://miro.medium.com/max/1500/0*OxT-5PQk8GyGxdsh "req-limits") 

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]("https://itnext.io/easy-and-fast-adjustment-of-kubernetes-cpu-and-memory-709394cc2cb1#73fc") includes container, resource, and limits definitions.

More detailed information and code snippets:

- [Kubernetes.io. Assign memory resource guide.](https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/)
- [Kubernetes.io. Assign CPU resource guide.](https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/)
- [Kubernetes best practices resource requests and limits](https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-resource-requests-and-limits)
- [Azure Kubernetes Services. Developer best practices resource-management](https://docs.microsoft.com/en-us/azure/aks/developer-best-practices-resource-management)

# Resources Quota Guide

![logo-header](https://miro.medium.com/max/1500/0*8GQuYKVTclvqe-R1 "req-limits") 

Two [quota templates]("https://itnext.io/easy-and-fast-adjustment-of-kubernetes-cpu-and-memory-709394cc2cb1#b59b") 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](https://miro.medium.com/max/1500/1*u4HOj5vwpS5PV6x8Y9caNg.png "Log 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.

- [https://kubernetes.io/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/](Manage resources. Cpu default namespace)
- [https://kubernetes.io/docs/tasks/administer-cluster/limit-storage-consumption/](Kubernetes io. Limit storage consumption)
- [https://www.howtoforge.com/limit-ranges-in-kubernetes](Kubernetes io. Limit ranges)

# Tools and Frameworks that Helps with Resource and Limits Routine

- [Popeye](https://github.com/derailed/popeye) scans your cluster for potential issues with configuration, resources, and network holes and generates detailed reports with all issues.
- [Goldilocks](https://github.com/FairwindsOps/goldilocks/) scans pods for resource limits and creates reports with recommended resources.
- [Kube-advisor](https://github.com/Azure/kube-advisor) simple tool, from the Azure team, that scans pods for missing resources and limits requests.
- [K9s+benchmark](https://github.com/derailed/k9s#benchmark-your-applications) — 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?

Please feel free to share your feedback and suggestions in the comments.