codeaprendiz.github.io

DevOps Essentials

View on GitHub

Configure Default CPU Requests and Limits for a Namespace

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

$ kubectl create namespace default-cpu-example
namespace/default-cpu-example created
apiVersion: v1
kind: LimitRange
metadata:
  name: cpu-limit-range
spec:
  limits:
    - default:
        cpu: 1
      defaultRequest:
        cpu: 0.5
      type: Container
apiVersion: v1
kind: Pod
metadata:
  name: default-cpu-demo
spec:
  containers:
  - name: default-cpu-demo-ctr
    image: nginx
$ kubectl apply -f . --namespace=default-cpu-example
limitrange/cpu-limit-range created
pod/default-cpu-demo created
$ kubectl get pod default-cpu-demo --output=yaml --namespace=default-cpu-example | egrep -i "resources:" -A 4 | egrep -v "f:|{|-"
    resources:
      limits:
        cpu: "1"
      requests:
        cpu: 500m

The output shows that the Pod’s Container has a CPU request of 500 millicpus and a CPU limit of 1 cpu. These are the default values specified by the LimitRange.

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

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

The output shows that the Container’s CPU request is set to match its CPU limit. Notice that the Container was not assigned the default CPU request value of 0.5 cpu.

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

apiVersion: v1
kind: Pod
metadata:
  name: default-cpu-demo-3
spec:
  containers:
  - name: default-cpu-demo-3-ctr
    image: nginx
    resources:
      requests:
        cpu: "0.75"
$ kubectl apply -f . --namespace=default-cpu-example                                                                               
limitrange/cpu-limit-range configured
pod/default-cpu-demo-2 unchanged
pod/default-cpu-demo unchanged
pod/default-cpu-demo-3 created
$ kubectl get pod default-cpu-demo-3 --output=yaml --namespace=default-cpu-example | egrep -i "resources:" -A 4 | egrep -v "f:|{|-"
    resources:
      limits:
        cpu: "1"
      requests:
        cpu: 750m
$ kubectl delete -f . --namespace=default-cpu-example                                                                              
limitrange "cpu-limit-range" deleted
pod "default-cpu-demo-2" deleted
pod "default-cpu-demo" deleted
pod "default-cpu-demo-3" deleted