codeaprendiz.github.io

DevOps Essentials

View on GitHub

Configure Default Memory Requests and Limits for a Namespace

administer-cluster/manage-resources/memory-default-namespace/

$ kubectl create namespace default-mem-example

namespace/default-mem-example created
apiVersion: v1
kind: LimitRange
metadata:
  name: mem-limit-range
spec:
  limits:
    - default:
        memory: 512Mi
      defaultRequest:
        memory: 256Mi
      type: Container
$ kubectl apply -f . --namespace=default-mem-example
limitrange/mem-limit-range created
apiVersion: v1
kind: Pod
metadata:
  name: default-mem-demo
spec:
  containers:
    - name: default-mem-demo-ctr
      image: nginx
$ kubectl apply -f . --namespace=default-mem-example
pod/default-mem-demo created
limitrange/mem-limit-range unchanged
$ kubectl get pod default-mem-demo -o yaml --namespace=default-mem-example | egrep -i "resources:" -A 4 | egrep -v "f:"
--
    resources:
      limits:
        memory: 512Mi
      requests:
        memory: 256Mi

What if you specify a Container’s limit, but not its request?

apiVersion: v1
kind: Pod
metadata:
  name: default-mem-demo-2
spec:
  containers:
    - name: default-mem-demo-2-ctr
      image: nginx
      resources:
        limits:
          memory: "1Gi"
$ kubectl apply -f . --namespace=default-mem-example
limitrange/mem-limit-range unchanged
pod/default-mem-demo-2 created
pod/default-mem-demo created
$ kubectl get pod default-mem-demo-2 -o yaml --namespace=default-mem-example | egrep -i "resources:" -A 4 | egrep -v "f:|{|-"
    resources:
      limits:
        memory: 1Gi
      requests:
        memory: 1Gi

What if you specify a Container’s request, but not its limit?

apiVersion: v1
kind: Pod
metadata:
  name: default-mem-demo-3
spec:
  containers:
    - name: default-mem-demo-3-ctr
      image: nginx
      resources:
        requests:
          memory: "128Mi"
$ kubectl apply -f . --namespace=default-mem-example                                                                         
limitrange/mem-limit-range unchanged
pod/default-mem-demo-2 unchanged
pod/default-mem-demo unchanged
pod/default-mem-demo-3 created
$ kubectl get pod default-mem-demo-3 -o yaml --namespace=default-mem-example | egrep -i "resources:" -A 4 | egrep -v "f:|{|-"
    resources:
      limits:
        memory: 512Mi
      requests:
        memory: 128Mi
$ kubectl delete -f . --namespace=default-mem-example                                                                        
limitrange "mem-limit-range" deleted
pod "default-mem-demo-2" deleted
pod "default-mem-demo" deleted
pod "default-mem-demo-3" deleted