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)?
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/
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