CKAD — Resource Management Lab

2 minute read

Lab 1: Resource Requests and Limits

# Create a Pod with specific resources
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: resource-demo
spec:
  containers:
  - name: app
    image: nginx:1.27
    resources:
      requests:
        cpu: 100m
        memory: 64Mi
      limits:
        cpu: 200m
        memory: 128Mi
EOF

# Check the QoS class
kubectl get pod resource-demo -o jsonpath='{.status.qosClass}'
# Expected: Burstable (because requests ≠ limits)

# Check resource allocation
kubectl describe pod resource-demo | grep -A6 Resources

Lab 2: LimitRange and ResourceQuota

# Create a namespace
kubectl create namespace quota-lab

# Create a LimitRange
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: LimitRange
metadata:
  name: default-limits
  namespace: quota-lab
spec:
  limits:
  - type: Container
    default:
      cpu: 200m
      memory: 128Mi
    defaultRequest:
      cpu: 100m
      memory: 64Mi
EOF

# Create a ResourceQuota
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ResourceQuota
metadata:
  name: ns-quota
  namespace: quota-lab
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 512Mi
    limits.cpu: "2"
    limits.memory: 1Gi
    pods: "5"
EOF

# Create a Pod without specifying resources — gets defaults from LimitRange
kubectl run test-pod --image=nginx -n quota-lab
kubectl describe pod test-pod -n quota-lab | grep -A6 Resources
# Expected: Gets default requests and limits from LimitRange

# Check quota usage
kubectl describe resourcequota ns-quota -n quota-lab

Practice Questions

Q1: A container has requests.memory: 256Mi and limits.memory: 256Mi. What QoS class does this Pod get?

Show Answer **Guaranteed** — When all containers in a Pod have equal requests and limits for both CPU and memory, the Pod gets the Guaranteed QoS class. If CPU is not specified, only memory needs to match. Actually, for Guaranteed class, requests and limits must be set and equal for **all** resource types that are specified. If only memory is set with equal request and limit (and CPU has the same), it is Guaranteed.

Q2: What happens when a container exceeds its CPU limit vs its memory limit?

Show Answer - **CPU limit exceeded:** The container is **throttled** — it is slowed down but not killed. The CPU is artificially limited by the kernel's CFS scheduler. - **Memory limit exceeded:** The container is **OOMKilled** (Out of Memory Killed). Kubernetes terminates the container process. If the restart policy allows, the container restarts.

Q3: Create a ResourceQuota in the staging namespace that allows a maximum of 10 Pods, 2 CPU requests, and 4Gi memory requests.

Show Answer ```yaml apiVersion: v1 kind: ResourceQuota metadata: name: staging-quota namespace: staging spec: hard: pods: "10" requests.cpu: "2" requests.memory: 4Gi ``` ```bash kubectl apply -f staging-quota.yaml ```

Q4: You have a namespace with a ResourceQuota set, but Pods fail to create with “must specify requests.cpu.” Why?

Show Answer When a ResourceQuota that tracks `requests.cpu` or `limits.cpu` is present in a namespace, **every Pod must specify resource requests and limits**. Kubernetes rejects Pods without them because it cannot track quota usage. **Fix:** Either add resource requests/limits to every Pod spec, or create a **LimitRange** that provides default values automatically.

Q5: What is the LimitRange defaultRequest field used for?

Show Answer The `defaultRequest` field specifies the **default resource requests** that are automatically applied to containers that do not specify their own requests. This ensures every container has at least minimum resources for scheduling, even if the developer did not specify them. ```yaml spec: limits: - type: Container defaultRequest: cpu: 100m # Applied if no request specified memory: 128Mi default: cpu: 300m # Applied if no limit specified memory: 256Mi ```

← Previous: Monitoring Resource Usage Next: Security Contexts & ServiceAccounts →

Updated: