How can one control the maximum resources to ever be used (at a given moment) by everything running in a specific k8s namespace. (Max. memory, max. CPU)?
Asked
Active
Viewed 93 times
2 Answers
1
This can be accomplished via a ResourceQuota on the given namespace.
From the docs:
A resource quota, defined by a ResourceQuota object, provides constraints that limit aggregate resource consumption per namespace. It can limit the quantity of objects that can be created in a namespace by type, as well as the total amount of compute resources that may be consumed by resources in that project.
A resource quota is defined like so (from the k8s admin docs):
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-demo
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2Gi
Note: This information comes from the k8s v1.16 documentation

mcarlin
- 1,571
- 1
- 11
- 16
-
1Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. See: [How to anwser](https://stackoverflow.com/help/how-to-answer). – Eduardo Baitello Sep 26 '19 at 01:33
0
Define resource quota objects per namespace. you can set maximum compute resources per namespace and also define number of objects to be deployed per namespace. follow the below samples
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-demo
namespace: quota-mem-cpu-example
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2Gi
----
The ResourceQuota places these requirements on the quota-mem-cpu-example namespace:
Every Container must have a memory request, memory limit, cpu request, and cpu limit.
The memory request total for all Containers must not exceed 1 GiB.
The memory limit total for all Containers must not exceed 2 GiB.
The CPU request total for all Containers must not exceed 1 cpu.
The CPU limit total for all Containers must not exceed 2 cpu.
similarly define object quota
apiVersion: v1
kind: ResourceQuota
metadata:
name: object-quota-demo
namespace: quota-object-example
spec:
hard:
persistentvolumeclaims: "1"
services.loadbalancers: "2"
services.nodeports: "0"
Which implies that there can be at most one PersistentVolumeClaim, at most two Services of type LoadBalancer, and no Services of type NodePort.
reference: https://kubernetes.io/docs/tasks/administer-cluster/quota-api-object/

P Ekambaram
- 15,499
- 7
- 34
- 59