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.
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.
Basics Guide on CPU and Memory Resources
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:
- Kubernetes.io. Assign memory resource guide.
- Kubernetes.io. Assign CPU resource guide.
- Kubernetes best practices resource requests and limits
- Azure Kubernetes Services. Developer best practices resource-management
Resources Quota Guide
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.
Limit Ranges Guide
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/
- https://kubernetes.io/docs/tasks/administer-cluster/limit-storage-consumption/
- https://www.howtoforge.com/limit-ranges-in-kubernetes
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
- Setup Resource Requests. Get information about CPU and Memory usage of specific Application/Container.
- Setup Resource Limits. Setup Resource Requests. Run a load test to detect CPU and Memory of a container under the high load..
- Monitor containers CPU and Memory usage.
- Monitor persistent storage usage.
- Check if you can apply resource limit using limit ranges (if you have similar containers, storage set up)
- 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.
Top comments (0)