Assigning and managing CPU and memory resources in 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 also how to manage them.
The framework contains the following steps:
My goal here is simplicity. You won’t find detailed descriptions of how resources, limit ranges, and quotas work. There are plentiful articles written about that, and the Kubernetes documentation goes over it thoroughly. Instead, here you will find information on how to quickly start adjusting Kubernetes resources in your projects.
Here are 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:
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.
The Kubernetes deployment YAML template includes container, resource, and limits definitions:
apiVersion: apps/v1
kind: Deployment
metadata:
name: aks-application
spec:
replicas: 1
selector:
matchLabels:
app: aks-application
template:
metadata:
labels:
app: aks-application
spec:
containers:
- name: aks-application
image: hubname/aks-application-image:1.0.0
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
More detailed information and code snippets:
Quota templates that contain the definition of quotas for persistent volume claims and resources in the backend namespace:
apiVersion: v1
kind: ResourceQuota
metadata:
name: backend-storage-quota
spec:
hard:
persistentvolumeclaims: "2"
requests.storage: "1Gi"
--
apiVersion: v1
kind: ResourceQuota
metadata:
name: backend-namespace-quota
spec:
hard:
request.cpu: 400m
request.memory: 9600Mi
limits.cpu: 1200m
limits.memory: 11600Mi
You can use the kubectl apply
command to set the quota constraints for a namespace.
kubectl apply -f resource-quota.yaml — namespace backend
A detailed explanation of quotas, metrics, and how to use quota expressions and code samples:
Two limit range code templates to limit CPU and memory resources for the containers and to limit the storage (Persistent Volume Claim) are shown below. 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:
The above are some useful free and open-source tools which are easy to set up. You can also combine these tools with Datadog, Grafana+Prometeus, or Azure Monitor to improve resource and limits monitoring.
Free Resources