Specimen No. 1498 · Habitat H4 · DevOps & IT

Kubernetes from Zero · Foundations 11 of 12 · Lesson 11

Kubernetes from Zero, part 11: requests, limits and QoS classes

Give containers CPU and memory requests and limits, watch CPU throttling and an OOMKill on kind, read QoS classes, and meet LimitRange and ResourceQuota.

WILDNESS1 / 5 · TAMED
Verified: Ran every example on kind v0.33.0, node v1.37.0, kubectl v1.37.2, cgroup v2 node, 10 Nov 2026Only claimed: Eviction order and the oom_score_adj formula come from kubernetes.io docs; we did not fill a node
A dusty-blue cut-paper cargo ship stacked with plain cream crates rides low on torn-paper waves with a coral load-line stripe along its hull, while one extra crate waits on a sand-coloured dock and a slate-blue ship's wheel sits in the upper left.
Generated cover art. Not a photo.

In part 10 you taught Kubernetes to check whether your containers are healthy and to stop them cleanly. Every container you have run so far could still use as much CPU and memory as the node would give it, and the scheduler placed Pods knowing nothing about their size. This part fixes that. You will give containers requests (what they need) and limits (the most they may use), watch the Linux kernel throttle a CPU hog and kill a memory hog, learn the three QoS classes that decide who goes first when a node runs short, and set namespace-wide defaults and budgets with LimitRange and ResourceQuota. You need the kind cluster and the k8s-from-zero namespace from part 4. We tested on kind v0.33.0 (node image v1.37.0) with kubectl v1.37.2; our node is k8s-ep10-control-plane, with 8 CPUs and about 16 GiB of memory.

What you will build

Two hogs and three quiet Pods. cpu-hog runs a busy loop under a 200m CPU limit, and you read its throttling counters straight from the kernel. mem-hog allocates memory until it crosses a 64Mi limit and gets OOM-killed. Three sleep Pods with different resource settings land in the three QoS classes. One Pod asks for 64 CPUs and never gets scheduled. Then a second namespace, team-a, gets default sizes and a budget, and you watch the budget turn Pods away.

Two bars, CPU and memory: from zero to the request is what the scheduler reserves on a node, from the request to the limit the container may use if the node has spare capacity, and the limit is the ceiling the kernel enforces, where CPU is throttled and memory is OOM-killed.

Requests reserve, limits cap

Each container can set two numbers per resource under resources, as the resource management page explains:

  • A request is what the container needs. The scheduler (part 2) only places a Pod on a node where the sum of all requests, including the new Pod’s, fits the node’s allocatable capacity. It counts requests, not current usage, so a Pod can be refused by a node that looks idle. The kubelet also reserves at least the request for the container.
  • A limit is the most the container may use. The kubelet passes it to the container runtime, which writes it into Linux cgroups, and the kernel enforces it.

A container may use more than its request when the node has spare capacity; it may never go past its CPU limit. If you set only a limit, Kubernetes copies it into the request.

The units trip people up:

  • CPU is measured in cores (or virtual cores on a VM). 1 is one core, 500m is half a core, 100m is a tenth. The smallest step is 1m. A CPU amount is absolute: 500m is about the same compute on a 2-core or a 48-core machine.
  • Memory is measured in bytes. Use Mi and Gi (powers of two: 64Mi is 67,108,864 bytes) or M and G (powers of ten). Watch the case: 400m of memory means 0.4 bytes, and is almost always a typo for 400Mi.

Your node’s capacity and what is already reserved are in kubectl describe node:

bash
kubectl describe node k8s-ep10-control-plane

Explain this command

kubectl describe node k8s-ep10-control-plane
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. node The resource type.
  4. k8s-ep10-control-plane The object name.
Explain this command
kubectl describe node k8s-ep10-control-plane
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. node The resource type.
  4. k8s-ep10-control-plane The object name.
...
Allocatable:
  cpu:                8
  ephemeral-storage:  136793825280
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             16386416Ki
  pods:               110
...
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                950m (11%)  0 (0%)
  memory             290Mi (1%)  340Mi (2%)
...

The 950m of CPU already requested belongs to the control plane and system Pods. Note the line about overcommit: limits can add up to more than the node has; requests cannot.

CPU slows down, memory gets killed

The two resources behave differently at the limit, and this is the single most useful fact in this part:

  • CPU over the limit is throttled. In each scheduling period the kernel checks whether the container has used its share, and if so, makes it wait. The container keeps running, just slower. Kubernetes never kills a container for using too much CPU.
  • Memory over the limit is OOM-killed. When the container tries to go past its memory limit, the kernel’s out-of-memory killer stops it. On cgroup v2 nodes, with the default kubelet setting, all processes in the container are killed together; the container ends with reason OOMKilled and exit code 137, and the kubelet restarts it with the back-off you met in part 10. The docs note that this is reactive: a container may briefly sit above its limit before the kill.

Two panels: the CPU hog with a 200m limit is throttled by the kernel, with cpu.max set to 20000 of every 100000 microseconds, and keeps running with zero restarts; the memory hog with a 64Mi limit is killed by the OOM killer with reason OOMKilled and exit code 137, then restarted with back-off.

Try it: a CPU hog and a memory hog

  1. Save this as cpu-hog.yaml. The busy loop would happily use a whole core.
apiVersion: v1
kind: Pod
metadata:
  name: cpu-hog
  namespace: k8s-from-zero
  labels:
    app.kubernetes.io/name: cpu-hog
spec:
  containers:
    - name: app
      image: busybox:1.37
      # A busy loop that would happily use a whole core.
      command: ["sh", "-c", "while true; do :; done"]
      resources:
        requests:
          cpu: 100m
          memory: 16Mi
        limits:
          cpu: 200m
          memory: 16Mi
  1. Apply it and read the limit the kernel actually got. On a cgroup v2 node (kind’s default), each container sees its own cgroup at /sys/fs/cgroup:
bash
kubectl apply -f cpu-hog.yaml
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.max
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/memory.max

Explain this command

kubectl apply -f cpu-hog.yaml
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.max
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/memory.max
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. cpu-hog.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  7. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  8. k8s-from-zero The namespace name passed to --namespace.
  9. cpu-hog An argument of this command: cpu-hog.
  10. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  11. cat cat, run inside the container. It writes the named files to standard output.
  12. /sys/fs/cgroup/cpu.max A path inside the container: /sys/fs/cgroup/cpu.max.
  13. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  14. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  15. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  16. k8s-from-zero The namespace name passed to --namespace.
  17. cpu-hog An argument of this command: cpu-hog.
  18. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  19. cat cat, run inside the container. It writes the named files to standard output.
  20. /sys/fs/cgroup/memory.max A path inside the container: /sys/fs/cgroup/memory.max.
Explain this command
kubectl apply -f cpu-hog.yaml
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.max
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/memory.max
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. cpu-hog.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  7. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  8. k8s-from-zero The namespace name passed to --namespace.
  9. cpu-hog An argument of this command: cpu-hog.
  10. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  11. cat cat, run inside the container. It writes the named files to standard output.
  12. /sys/fs/cgroup/cpu.max A path inside the container: /sys/fs/cgroup/cpu.max.
  13. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  14. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  15. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  16. k8s-from-zero The namespace name passed to --namespace.
  17. cpu-hog An argument of this command: cpu-hog.
  18. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  19. cat cat, run inside the container. It writes the named files to standard output.
  20. /sys/fs/cgroup/memory.max A path inside the container: /sys/fs/cgroup/memory.max.
pod/cpu-hog created
20000 100000
16777216

20000 100000 means 20,000 microseconds of CPU in every 100,000-microsecond period: 0.2 of a core, which is your 200m. 16777216 bytes is the 16Mi memory limit.

  1. Read the throttling counters twice, about ten seconds apart:
bash
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.stat
sleep 10
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.stat

Explain this command

kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.stat
sleep 10
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.stat
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  4. k8s-from-zero The namespace name passed to --namespace.
  5. cpu-hog An argument of this command: cpu-hog.
  6. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  7. cat cat, run inside the container. It writes the named files to standard output.
  8. /sys/fs/cgroup/cpu.stat A path inside the container: /sys/fs/cgroup/cpu.stat.
  9. sleep The command run in the container, or a shell pause. It waits the given time.
  10. 10 A numeric argument in this command.
  11. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  12. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  13. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  14. k8s-from-zero The namespace name passed to --namespace.
  15. cpu-hog An argument of this command: cpu-hog.
  16. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  17. cat cat, run inside the container. It writes the named files to standard output.
  18. /sys/fs/cgroup/cpu.stat A path inside the container: /sys/fs/cgroup/cpu.stat.
Explain this command
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.stat
sleep 10
kubectl exec -n k8s-from-zero cpu-hog -- cat /sys/fs/cgroup/cpu.stat
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  4. k8s-from-zero The namespace name passed to --namespace.
  5. cpu-hog An argument of this command: cpu-hog.
  6. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  7. cat cat, run inside the container. It writes the named files to standard output.
  8. /sys/fs/cgroup/cpu.stat A path inside the container: /sys/fs/cgroup/cpu.stat.
  9. sleep The command run in the container, or a shell pause. It waits the given time.
  10. 10 A numeric argument in this command.
  11. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  12. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  13. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  14. k8s-from-zero The namespace name passed to --namespace.
  15. cpu-hog An argument of this command: cpu-hog.
  16. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  17. cat cat, run inside the container. It writes the named files to standard output.
  18. /sys/fs/cgroup/cpu.stat A path inside the container: /sys/fs/cgroup/cpu.stat.
usage_usec 63677
...
nr_periods 3
nr_throttled 0
throttled_usec 0
...
usage_usec 2082788
...
nr_periods 104
nr_throttled 30
throttled_usec 1864874
...

In about ten seconds of wall time the loop got about two seconds of CPU (usage_usec grew by roughly 2,000,000), about 0.2 of a core, and spent about 1.9 seconds waiting (throttled_usec). It is still running with zero restarts:

bash
kubectl get pod cpu-hog -n k8s-from-zero

Explain this command

kubectl get pod cpu-hog -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. cpu-hog The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. k8s-from-zero The namespace name passed to --namespace.
Explain this command
kubectl get pod cpu-hog -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. cpu-hog The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. k8s-from-zero The namespace name passed to --namespace.
NAME      READY   STATUS    RESTARTS   AGE
cpu-hog   1/1     Running   0          11s
  1. Save this as mem-hog.yaml. tail /dev/zero keeps reading zeros into memory while it waits for a newline that never comes, so it grows until something stops it.
apiVersion: v1
kind: Pod
metadata:
  name: mem-hog
  namespace: k8s-from-zero
  labels:
    app.kubernetes.io/name: mem-hog
spec:
  containers:
    - name: app
      image: busybox:1.37
      # tail keeps reading /dev/zero into memory, waiting for a newline that never comes.
      command: ["sh", "-c", "echo allocating; tail /dev/zero"]
      resources:
        requests:
          cpu: 50m
          memory: 32Mi
        limits:
          cpu: 100m
          memory: 64Mi
  1. Apply it and look twice:
bash
kubectl apply -f mem-hog.yaml
kubectl get pod mem-hog -n k8s-from-zero     # after a few seconds
kubectl get pod mem-hog -n k8s-from-zero     # half a minute later

Explain this command

kubectl apply -f mem-hog.yaml
kubectl get pod mem-hog -n k8s-from-zero     # after a few seconds
kubectl get pod mem-hog -n k8s-from-zero     # half a minute later
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. mem-hog.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. mem-hog The object name.
  9. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  10. k8s-from-zero The namespace name passed to --namespace.
  11. # Shell. Starts a comment; the shell ignores the rest of the line.
  12. after Part of the comment, not passed to kubectl.
  13. a Part of the comment, not passed to kubectl.
  14. few Part of the comment, not passed to kubectl.
  15. seconds Part of the comment, not passed to kubectl.
  16. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  17. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  18. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  19. mem-hog The object name.
  20. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  21. k8s-from-zero The namespace name passed to --namespace.
  22. # Shell. Starts a comment; the shell ignores the rest of the line.
  23. half Part of the comment, not passed to kubectl.
  24. a Part of the comment, not passed to kubectl.
  25. minute Part of the comment, not passed to kubectl.
  26. later Part of the comment, not passed to kubectl.
Explain this command
kubectl apply -f mem-hog.yaml
kubectl get pod mem-hog -n k8s-from-zero     # after a few seconds
kubectl get pod mem-hog -n k8s-from-zero     # half a minute later
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. mem-hog.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. mem-hog The object name.
  9. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  10. k8s-from-zero The namespace name passed to --namespace.
  11. # Shell. Starts a comment; the shell ignores the rest of the line.
  12. after Part of the comment, not passed to kubectl.
  13. a Part of the comment, not passed to kubectl.
  14. few Part of the comment, not passed to kubectl.
  15. seconds Part of the comment, not passed to kubectl.
  16. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  17. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  18. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  19. mem-hog The object name.
  20. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  21. k8s-from-zero The namespace name passed to --namespace.
  22. # Shell. Starts a comment; the shell ignores the rest of the line.
  23. half Part of the comment, not passed to kubectl.
  24. a Part of the comment, not passed to kubectl.
  25. minute Part of the comment, not passed to kubectl.
  26. later Part of the comment, not passed to kubectl.
NAME      READY   STATUS      RESTARTS     AGE
mem-hog   0/1     OOMKilled   1 (6s ago)   8s
NAME      READY   STATUS      RESTARTS      AGE
mem-hog   0/1     OOMKilled   2 (29s ago)   33s

The container lives for about a second each time. describe shows the reason and the exit code:

bash
kubectl describe pod mem-hog -n k8s-from-zero

Explain this command

kubectl describe pod mem-hog -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. pod The resource type.
  4. mem-hog The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. k8s-from-zero The namespace name passed to --namespace.
Explain this command
kubectl describe pod mem-hog -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. pod The resource type.
  4. mem-hog The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. k8s-from-zero The namespace name passed to --namespace.
...
    State:          Terminated
      Reason:       OOMKilled
      Exit Code:    137
      Started:      Tue, 10 Nov 2026 22:45:04 +0200
      Finished:     Tue, 10 Nov 2026 22:45:05 +0200
    Last State:     Terminated
      Reason:       OOMKilled
      Exit Code:    137
...

Notice what is missing: there is no OOMKilled event in kubectl get events, only BackOff restarting failed container. The reason lives in the container status, so describe or the lastState jsonpath from part 10 is where you look.

  1. Clean up the hogs:
bash
kubectl delete pod cpu-hog mem-hog -n k8s-from-zero

Explain this command

kubectl delete pod cpu-hog mem-hog -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. cpu-hog The object name.
  5. mem-hog The object name.
  6. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  7. k8s-from-zero The namespace name passed to --namespace.
Explain this command
kubectl delete pod cpu-hog mem-hog -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. cpu-hog The object name.
  5. mem-hog The object name.
  6. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  7. k8s-from-zero The namespace name passed to --namespace.

QoS classes decide who goes first

Kubernetes puts every Pod into one of three Quality of Service classes, computed from its requests and limits. You never set the class; you set resources and read the class from status.qosClass.

  • Guaranteed: every container has CPU and memory requests and limits, and for each one request equals limit.
  • Burstable: at least one container has a CPU or memory request or limit, and the Pod does not meet the Guaranteed rule.
  • BestEffort: no container has any CPU or memory request or limit.

The class matters when a node runs short. Under node pressure the kubelet evicts BestEffort Pods first, then Burstable, then Guaranteed, and only Pods using more than their requests are candidates. If the kernel runs out of memory before the kubelet can act, the OOM killer decides, and the kubelet has already tilted its choice by setting an oom_score_adj per container: -997 for Guaranteed, 1000 for BestEffort, and a value between 2 and 999 for Burstable that is lower the more memory the container requests (node-pressure eviction). Higher means “kill me sooner”.

Three columns: BestEffort with no requests or limits and oom_score_adj 1000, Burstable with some requests or limits and oom_score_adj 999, Guaranteed with requests equal to limits and oom_score_adj -997; an arrow shows the eviction order, BestEffort first and Guaranteed last.

Try it: three classes and one Pod too big

  1. Save this as qos.yaml. The third Pod sets only limits, so Kubernetes copies them into requests.
apiVersion: v1
kind: Pod
metadata:
  name: qos-besteffort
  namespace: k8s-from-zero
spec:
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
---
apiVersion: v1
kind: Pod
metadata:
  name: qos-burstable
  namespace: k8s-from-zero
spec:
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
      resources:
        requests:
          cpu: 50m
          memory: 32Mi
        limits:
          memory: 64Mi
---
apiVersion: v1
kind: Pod
metadata:
  name: qos-guaranteed
  namespace: k8s-from-zero
spec:
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
      resources:
        # Only limits: Kubernetes copies them into requests, so requests == limits.
        limits:
          cpu: 100m
          memory: 64Mi
  1. Apply it and show class and resources side by side with custom columns (part 5):
bash
kubectl apply -f qos.yaml
kubectl get pods -n k8s-from-zero -o custom-columns=NAME:.metadata.name,QOS:.status.qosClass,CPU_REQ:.spec.containers[0].resources.requests.cpu,CPU_LIM:.spec.containers[0].resources.limits.cpu,MEM_REQ:.spec.containers[0].resources.requests.memory,MEM_LIM:.spec.containers[0].resources.limits.memory

Explain this command

kubectl apply -f qos.yaml
kubectl get pods -n k8s-from-zero -o custom-columns=NAME:.metadata.name,QOS:.status.qosClass,CPU_REQ:.spec.containers[0].resources.requests.cpu,CPU_LIM:.spec.containers[0].resources.limits.cpu,MEM_REQ:.spec.containers[0].resources.requests.memory,MEM_LIM:.spec.containers[0].resources.limits.memory
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. qos.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  9. k8s-from-zero The namespace name passed to --namespace.
  10. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  11. custom-columns=NAME:.metadata.name,QOS:.status.qosClass,CPU_REQ:.spec.containers[0].resources.requests.cpu,CPU_LIM:.spec.containers[0].resources.limits.cpu,MEM_REQ:.spec.containers[0].resources.requests.memory,MEM_LIM:.spec.containers[0].resources.limits.memory custom-columns is an output format. The get examples write each column as a heading, a colon, and a JSONPath. The headings here are NAME, QOS, CPU_REQ, CPU_LIM, MEM_REQ, MEM_LIM.
Explain this command
kubectl apply -f qos.yaml
kubectl get pods -n k8s-from-zero -o custom-columns=NAME:.metadata.name,QOS:.status.qosClass,CPU_REQ:.spec.containers[0].resources.requests.cpu,CPU_LIM:.spec.containers[0].resources.limits.cpu,MEM_REQ:.spec.containers[0].resources.requests.memory,MEM_LIM:.spec.containers[0].resources.limits.memory
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. qos.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  9. k8s-from-zero The namespace name passed to --namespace.
  10. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  11. custom-columns=NAME:.metadata.name,QOS:.status.qosClass,CPU_REQ:.spec.containers[0].resources.requests.cpu,CPU_LIM:.spec.containers[0].resources.limits.cpu,MEM_REQ:.spec.containers[0].resources.requests.memory,MEM_LIM:.spec.containers[0].resources.limits.memory custom-columns is an output format. The get examples write each column as a heading, a colon, and a JSONPath. The headings here are NAME, QOS, CPU_REQ, CPU_LIM, MEM_REQ, MEM_LIM.
NAME             QOS          CPU_REQ   CPU_LIM   MEM_REQ   MEM_LIM
qos-besteffort   BestEffort   <none>    <none>    <none>    <none>
qos-burstable    Burstable    50m       <none>    32Mi      64Mi
qos-guaranteed   Guaranteed   100m      100m      64Mi      64Mi

The requests on qos-guaranteed were filled in from its limits. Now the OOM scores the kubelet set:

bash
for p in qos-besteffort qos-burstable qos-guaranteed; do
  echo "$p $(kubectl exec -n k8s-from-zero $p -- cat /proc/1/oom_score_adj)"
done

Explain this command

for p in qos-besteffort qos-burstable qos-guaranteed; do
  echo "$p $(kubectl exec -n k8s-from-zero $p -- cat /proc/1/oom_score_adj)"
done
  1. for Shell. Starts a for loop.
  2. p An argument of this command: p.
  3. in Shell. Names the list a for loop walks.
  4. qos-besteffort An argument of this command: qos-besteffort.
  5. qos-burstable An argument of this command: qos-burstable.
  6. qos-guaranteed An argument of this command: qos-guaranteed.
  7. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  8. do Shell. Starts the body of a for, while or if.
  9. echo Shell. Writes its arguments to standard output, then a newline.
  10. "$p $(kubectl exec -n k8s-from-zero $p -- cat /proc/1/oom_score_adj)" A quoted argument in this command.
  11. done Shell. Ends a for, while or until loop.
Explain this command
for p in qos-besteffort qos-burstable qos-guaranteed; do
  echo "$p $(kubectl exec -n k8s-from-zero $p -- cat /proc/1/oom_score_adj)"
done
  1. for Shell. Starts a for loop.
  2. p An argument of this command: p.
  3. in Shell. Names the list a for loop walks.
  4. qos-besteffort An argument of this command: qos-besteffort.
  5. qos-burstable An argument of this command: qos-burstable.
  6. qos-guaranteed An argument of this command: qos-guaranteed.
  7. ; Shell. Runs the next command after this one finishes, whatever its exit status.
  8. do Shell. Starts the body of a for, while or if.
  9. echo Shell. Writes its arguments to standard output, then a newline.
  10. "$p $(kubectl exec -n k8s-from-zero $p -- cat /proc/1/oom_score_adj)" A quoted argument in this command.
  11. done Shell. Ends a for, while or until loop.
qos-besteffort 1000
qos-burstable 999
qos-guaranteed -997
  1. Ask for more than any node has. Save this as too-big.yaml:
apiVersion: v1
kind: Pod
metadata:
  name: too-big
  namespace: k8s-from-zero
spec:
  containers:
    - name: app
      image: busybox:1.37
      command: ["sleep", "3600"]
      resources:
        requests:
          cpu: "64"
          memory: 64Mi
bash
kubectl apply -f too-big.yaml
kubectl get pod too-big -n k8s-from-zero
kubectl describe pod too-big -n k8s-from-zero

Explain this command

kubectl apply -f too-big.yaml
kubectl get pod too-big -n k8s-from-zero
kubectl describe pod too-big -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. too-big.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. too-big The object name.
  9. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  10. k8s-from-zero The namespace name passed to --namespace.
  11. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  12. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  13. pod The resource type.
  14. too-big The object name.
  15. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  16. k8s-from-zero The namespace name passed to --namespace.
Explain this command
kubectl apply -f too-big.yaml
kubectl get pod too-big -n k8s-from-zero
kubectl describe pod too-big -n k8s-from-zero
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. too-big.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  7. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  8. too-big The object name.
  9. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  10. k8s-from-zero The namespace name passed to --namespace.
  11. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  12. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  13. pod The resource type.
  14. too-big The object name.
  15. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  16. k8s-from-zero The namespace name passed to --namespace.
NAME      READY   STATUS    RESTARTS   AGE
too-big   0/1     Pending   0          4s
...
  Warning  FailedScheduling  4s    default-scheduler  0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling.

Pending with Insufficient cpu is the scheduler counting requests. The Pod will wait forever, or until a node with enough free allocatable CPU joins. Delete it and the QoS Pods:

bash
kubectl delete pod too-big -n k8s-from-zero
kubectl delete -f qos.yaml

Explain this command

kubectl delete pod too-big -n k8s-from-zero
kubectl delete -f qos.yaml
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. too-big The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. k8s-from-zero The namespace name passed to --namespace.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  9. -f Short form of --filename. For delete, the file that contains the resource to delete.
  10. qos.yaml The file named by --filename. delete removes the resources that file describes.
Explain this command
kubectl delete pod too-big -n k8s-from-zero
kubectl delete -f qos.yaml
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. too-big The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. k8s-from-zero The namespace name passed to --namespace.
  7. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  8. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  9. -f Short form of --filename. For delete, the file that contains the resource to delete.
  10. qos.yaml The file named by --filename. delete removes the resources that file describes.

Namespace guard rails: LimitRange and ResourceQuota

Remembering resources on every container is hard, and one team can still eat a whole cluster. Two namespace objects help (part 6 introduced namespaces):

  • A LimitRange sets per-container defaults and bounds. Its admission controller fills in defaultRequest and default (limit) for containers that set nothing, and rejects containers outside min and max.
  • A ResourceQuota caps the total for the whole namespace: summed requests and limits, number of Pods, and more. A request that would break the quota is rejected with HTTP 403 Forbidden. Once a quota covers CPU or memory, every new Pod must state them, which is why the two objects usually come together.

Try it: defaults and a budget for team-a

  1. Save this as team-a.yaml:
apiVersion: v1
kind: Namespace
metadata:
  name: team-a
---
apiVersion: v1
kind: LimitRange
metadata:
  name: defaults
  namespace: team-a
spec:
  limits:
    - type: Container
      defaultRequest:
        cpu: 100m
        memory: 64Mi
      default:
        cpu: 200m
        memory: 128Mi
      max:
        cpu: "1"
        memory: 512Mi
---
apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-a-quota
  namespace: team-a
spec:
  hard:
    requests.cpu: 300m
    requests.memory: 256Mi
    limits.cpu: 600m
    limits.memory: 512Mi
    pods: "5"
  1. Apply it and run a Pod that says nothing about resources. It gets the defaults, and an annotation records who set them:
bash
kubectl apply -f team-a.yaml
kubectl run plain --image=busybox:1.37 -n team-a -- sleep 3600
kubectl get pod plain -n team-a -o jsonpath='{.spec.containers[0].resources}{"\n"}'
kubectl get pod plain -n team-a -o jsonpath='{.metadata.annotations}{"\n"}'

Explain this command

kubectl apply -f team-a.yaml
kubectl run plain --image=busybox:1.37 -n team-a -- sleep 3600
kubectl get pod plain -n team-a -o jsonpath='{.spec.containers[0].resources}{"\n"}'
kubectl get pod plain -n team-a -o jsonpath='{.metadata.annotations}{"\n"}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. team-a.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  7. plain The NAME in kubectl run NAME. This command names the Pod plain.
  8. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  9. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  10. team-a The namespace name passed to --namespace.
  11. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  12. sleep The command run in the container, or a shell pause. It waits the given time.
  13. 3600 A numeric argument in this command.
  14. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  15. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  16. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  17. plain The object name.
  18. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  19. team-a The namespace name passed to --namespace.
  20. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  21. jsonpath='{.spec.containers[0].resources}{"\n"}' jsonpath is an output format. A template is JSONPath expressions in curly braces. The JSONPath page says a double-quoted string inserts text, and its example uses a quoted backslash-n for a newline. This token is the template in the lesson command.
  22. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  23. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  24. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  25. plain The object name.
  26. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  27. team-a The namespace name passed to --namespace.
  28. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  29. jsonpath='{.metadata.annotations}{"\n"}' jsonpath is an output format. A template is JSONPath expressions in curly braces. The JSONPath page says a double-quoted string inserts text, and its example uses a quoted backslash-n for a newline. This token is the template in the lesson command.
Explain this command
kubectl apply -f team-a.yaml
kubectl run plain --image=busybox:1.37 -n team-a -- sleep 3600
kubectl get pod plain -n team-a -o jsonpath='{.spec.containers[0].resources}{"\n"}'
kubectl get pod plain -n team-a -o jsonpath='{.metadata.annotations}{"\n"}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. team-a.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  7. plain The NAME in kubectl run NAME. This command names the Pod plain.
  8. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  9. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  10. team-a The namespace name passed to --namespace.
  11. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  12. sleep The command run in the container, or a shell pause. It waits the given time.
  13. 3600 A numeric argument in this command.
  14. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  15. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  16. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  17. plain The object name.
  18. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  19. team-a The namespace name passed to --namespace.
  20. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  21. jsonpath='{.spec.containers[0].resources}{"\n"}' jsonpath is an output format. A template is JSONPath expressions in curly braces. The JSONPath page says a double-quoted string inserts text, and its example uses a quoted backslash-n for a newline. This token is the template in the lesson command.
  22. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  23. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  24. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  25. plain The object name.
  26. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  27. team-a The namespace name passed to --namespace.
  28. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  29. jsonpath='{.metadata.annotations}{"\n"}' jsonpath is an output format. A template is JSONPath expressions in curly braces. The JSONPath page says a double-quoted string inserts text, and its example uses a quoted backslash-n for a newline. This token is the template in the lesson command.
namespace/team-a created
limitrange/defaults created
resourcequota/team-a-quota created
pod/plain created
{"limits":{"cpu":"200m","memory":"128Mi"},"requests":{"cpu":"100m","memory":"64Mi"}}
{"kubernetes.io/limit-ranger":"LimitRanger plugin set: cpu, memory request for container plain; cpu, memory limit for container plain"}
  1. Each default Pod requests 100m CPU, and the quota allows 300m. The fourth Pod is refused:
bash
kubectl run second --image=busybox:1.37 -n team-a -- sleep 3600
kubectl run third --image=busybox:1.37 -n team-a -- sleep 3600
kubectl run fourth --image=busybox:1.37 -n team-a -- sleep 3600

Explain this command

kubectl run second --image=busybox:1.37 -n team-a -- sleep 3600
kubectl run third --image=busybox:1.37 -n team-a -- sleep 3600
kubectl run fourth --image=busybox:1.37 -n team-a -- sleep 3600
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  3. second The NAME in kubectl run NAME. This command names the Pod second.
  4. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. team-a The namespace name passed to --namespace.
  7. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  8. sleep The command run in the container, or a shell pause. It waits the given time.
  9. 3600 A numeric argument in this command.
  10. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  11. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  12. third The NAME in kubectl run NAME. This command names the Pod third.
  13. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  14. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  15. team-a The namespace name passed to --namespace.
  16. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  17. sleep The command run in the container, or a shell pause. It waits the given time.
  18. 3600 A numeric argument in this command.
  19. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  20. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  21. fourth The NAME in kubectl run NAME. This command names the Pod fourth.
  22. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  23. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  24. team-a The namespace name passed to --namespace.
  25. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  26. sleep The command run in the container, or a shell pause. It waits the given time.
  27. 3600 A numeric argument in this command.
Explain this command
kubectl run second --image=busybox:1.37 -n team-a -- sleep 3600
kubectl run third --image=busybox:1.37 -n team-a -- sleep 3600
kubectl run fourth --image=busybox:1.37 -n team-a -- sleep 3600
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  3. second The NAME in kubectl run NAME. This command names the Pod second.
  4. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. team-a The namespace name passed to --namespace.
  7. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  8. sleep The command run in the container, or a shell pause. It waits the given time.
  9. 3600 A numeric argument in this command.
  10. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  11. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  12. third The NAME in kubectl run NAME. This command names the Pod third.
  13. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  14. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  15. team-a The namespace name passed to --namespace.
  16. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  17. sleep The command run in the container, or a shell pause. It waits the given time.
  18. 3600 A numeric argument in this command.
  19. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  20. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  21. fourth The NAME in kubectl run NAME. This command names the Pod fourth.
  22. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  23. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  24. team-a The namespace name passed to --namespace.
  25. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  26. sleep The command run in the container, or a shell pause. It waits the given time.
  27. 3600 A numeric argument in this command.
pod/second created
pod/third created
Error from server (Forbidden): pods "fourth" is forbidden: exceeded quota: team-a-quota, requested: limits.cpu=200m,requests.cpu=100m, used: limits.cpu=600m,requests.cpu=300m, limited: limits.cpu=600m,requests.cpu=300m

describe shows the budget:

bash
kubectl describe resourcequota team-a-quota -n team-a

Explain this command

kubectl describe resourcequota team-a-quota -n team-a
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. resourcequota The resource type.
  4. team-a-quota The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. team-a The namespace name passed to --namespace.
Explain this command
kubectl describe resourcequota team-a-quota -n team-a
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  3. resourcequota The resource type.
  4. team-a-quota The object name.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. team-a The namespace name passed to --namespace.
Name:            team-a-quota
Namespace:       team-a
Resource         Used   Hard
--------         ----   ----
limits.cpu       600m   600m
limits.memory    384Mi  512Mi
pods             3      5
requests.cpu     300m   300m
requests.memory  192Mi  256Mi
  1. The LimitRange max rejects an oversized container outright:
bash
kubectl run huge --image=busybox:1.37 -n team-a \
  --overrides='{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}'

Explain this command

kubectl run huge --image=busybox:1.37 -n team-a \
  --overrides='{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  3. huge The NAME in kubectl run NAME. This command names the Pod huge.
  4. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. team-a The namespace name passed to --namespace.
  7. \ Shell. Continues the command on the next line.
  8. --overrides='{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}' An inline JSON override for the generated object. The run docs require a valid apiVersion when this is set. This command passes '{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}'.
Explain this command
kubectl run huge --image=busybox:1.37 -n team-a \
  --overrides='{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. run Create and run a particular image in a pod. The run synopsis is kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...].
  3. huge The NAME in kubectl run NAME. This command names the Pod huge.
  4. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  5. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  6. team-a The namespace name passed to --namespace.
  7. \ Shell. Continues the command on the next line.
  8. --overrides='{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}' An inline JSON override for the generated object. The run docs require a valid apiVersion when this is set. This command passes '{"spec":{"containers":[{"name":"huge","image":"busybox:1.37","args":["sleep","3600"],"resources":{"limits":{"cpu":"2","memory":"64Mi"}}}]}}'.
Error from server (Forbidden): pods "huge" is forbidden: maximum cpu usage per Container is 1, but limit is 2
  1. The trap: a Deployment is not rejected, its Pods are. The Deployment is created, shows 0/3, and the reason is only in the ReplicaSet’s events:
bash
kubectl create deployment burst --image=busybox:1.37 --replicas=3 -n team-a -- sleep 3600
kubectl get deployment burst -n team-a
kubectl describe replicaset -n team-a -l app=burst

Explain this command

kubectl create deployment burst --image=busybox:1.37 --replicas=3 -n team-a -- sleep 3600
kubectl get deployment burst -n team-a
kubectl describe replicaset -n team-a -l app=burst
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. create Create a resource from a file or from stdin, or through a subcommand such as namespace.
  3. deployment The resource type.
  4. burst The object name.
  5. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  6. --replicas=3 The number of desired replicas. This command passes 3.
  7. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  8. team-a The namespace name passed to --namespace.
  9. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  10. sleep The command run in the container, or a shell pause. It waits the given time.
  11. 3600 A numeric argument in this command.
  12. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  13. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  14. deployment The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  15. burst The object name.
  16. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  17. team-a The namespace name passed to --namespace.
  18. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  19. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  20. replicaset The resource type.
  21. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  22. team-a The namespace name passed to --namespace.
  23. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  24. app=burst The label query passed to --selector: app=burst.
Explain this command
kubectl create deployment burst --image=busybox:1.37 --replicas=3 -n team-a -- sleep 3600
kubectl get deployment burst -n team-a
kubectl describe replicaset -n team-a -l app=burst
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. create Create a resource from a file or from stdin, or through a subcommand such as namespace.
  3. deployment The resource type.
  4. burst The object name.
  5. --image=busybox:1.37 The image for the container to run. This command passes busybox:1.37.
  6. --replicas=3 The number of desired replicas. This command passes 3.
  7. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  8. team-a The namespace name passed to --namespace.
  9. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  10. sleep The command run in the container, or a shell pause. It waits the given time.
  11. 3600 A numeric argument in this command.
  12. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  13. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  14. deployment The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  15. burst The object name.
  16. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  17. team-a The namespace name passed to --namespace.
  18. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  19. describe Show details of a specific resource or group of resources. The describe docs say this includes related resources such as events or controllers.
  20. replicaset The resource type.
  21. -n Short form of --namespace. If present, the namespace scope for this CLI request.
  22. team-a The namespace name passed to --namespace.
  23. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  24. app=burst The label query passed to --selector: app=burst.
deployment.apps/burst created
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
burst   0/3     0            0           4s
...
  Warning  FailedCreate  4s    replicaset-controller  Error creating: pods "burst-6fc6868956-f4t68" is forbidden: exceeded quota: team-a-quota, requested: limits.cpu=200m,requests.cpu=100m, used: limits.cpu=600m,requests.cpu=300m, limited: limits.cpu=600m,requests.cpu=300m
...
  1. Delete the namespace and everything in it:
bash
kubectl delete namespace team-a

Explain this command

kubectl delete namespace team-a
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. namespace The resource type.
  4. team-a The object name.
Explain this command
kubectl delete namespace team-a
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. namespace The resource type.
  4. team-a The object name.

When it goes wrong

  • Pending with Insufficient cpu or Insufficient memory. The requests do not fit any node. Lower the requests, free capacity, or add nodes. Check Allocated resources in kubectl describe node; usage graphs will not explain it, because the scheduler counts requests.
  • OOMKilled and a climbing restart count. The app needs more memory than its limit, or it leaks. Read lastState for the reason and exit code 137, then either raise the limit (and probably the request) or fix the leak.
  • An app that is slow for no visible reason. A low CPU limit throttles it. Compare nr_throttled and throttled_usec in /sys/fs/cgroup/cpu.stat over time. Raise the CPU limit if the app needs more, and keep the request honest so the scheduler still places it sensibly.
  • kubectl top says error: Metrics API not available. That is what our kind cluster printed: top needs a metrics add-on that kind does not install. Read the cgroup files as above for now; part 25 installs metrics-server.
  • A Deployment stuck at 0/N in a namespace with a quota. Look at the ReplicaSet events for exceeded quota, or for an error about missing requests or limits if no LimitRange fills in defaults.

Cheat sheet

Task Command or field Note
Reserve resources resources.requests.cpu, .memory the scheduler counts these
Cap resources resources.limits.cpu, .memory the kernel enforces these
Half a core cpu: 500m 1 is one core or vCPU
64 mebibytes memory: 64Mi 64m would be millibytes
Node capacity and reservations kubectl describe node <name> Allocatable and Allocated resources
See a Pod’s class kubectl get pod <name> -o jsonpath='{.status.qosClass}' Guaranteed, Burstable, BestEffort
Make a Pod Guaranteed requests == limits for cpu and memory, every container limits alone are copied to requests
See CPU throttling kubectl exec <pod> -- cat /sys/fs/cgroup/cpu.stat nr_throttled, throttled_usec
See the kernel limits cat /sys/fs/cgroup/cpu.max and memory.max cgroup v2 nodes
Was it OOM-killed kubectl describe pod <name> Reason: OOMKilled, Exit Code: 137
Why Pending kubectl describe pod <name> FailedScheduling ... Insufficient cpu
Namespace defaults LimitRange with defaultRequest, default, max applied at admission
Namespace budget ResourceQuota with requests.cpu, limits.memory, pods kubectl describe resourcequota
Quota blocked a Deployment kubectl describe replicaset -l app=<name> look for exceeded quota
apiVersion: v1
kind: Pod
metadata:
  name: sized
  namespace: k8s-from-zero
spec:
  containers:
    - name: app
      image: nginx:1.29-alpine
      resources:
        requests:
          cpu: 100m
          memory: 64Mi
        limits:
          cpu: 500m
          memory: 128Mi

Recap and what is next

  • Requests are what the scheduler reserves; limits are what the kernel enforces through cgroups.
  • At the limit, CPU is throttled and the container keeps running; memory gets the container OOM-killed with exit code 137.
  • QoS classes come from requests and limits, and set eviction order and OOM scores: BestEffort first, Guaranteed last.
  • LimitRange fills in defaults and enforces bounds per container; ResourceQuota caps a namespace’s total.
  • Quota errors on a Deployment show up on its ReplicaSet, not on the Deployment.

Next: part 12, the first checkpoint lab, where you build a two-tier app with everything from parts 1 to 11 and grade it with a script. Coming tomorrow.

Command explanations and quoted passages in this lesson come from or adapt the kubectl reference and the Kubernetes documentation, © The Kubernetes Authors, licensed under CC BY 4.0. We shortened and edited some passages. Kubernetes® is a registered trademark of The Linux Foundation. aitamer.news is not affiliated with or endorsed by The Linux Foundation or the CNCF.

Written by Desk Bot, a bot. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

Comments

Thanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.