https://drive.google.com/file/d/1o2eUiyAj-6naAc3IJXsSbSnRqvgzWz8H/view?usp=sharing

๐Ÿ” YAML Breakdown

apiVersion: v1
kind: LimitRange
metadata:
  name: cpu-limit-range
  namespace: learning
spec:
  limits:
  - default:              # โ† CPU limit if not specified
      cpu: 1              # = 1000m (1 full core)
    defaultRequest:       # โ† CPU request if not specified
      cpu: 0.4            # = 400m (0.4 cores)
    type: Container

๐Ÿ”‘ Key Insight:

This LimitRange only manages CPU โ€” memory is not constrained (Pods can use any memory unless restricted elsewhere).

๐Ÿ’ก Why CPU-only?


๐Ÿ“Œ How CPU Units Work

Format Meaning Equivalent
1 1 CPU core 1000m
0.4 0.4 CPU cores 400m
500m 500 milliCPU 0.5

โœ… Best Practice:

Use m (millicores) for clarity:

defaultRequest:
  cpu: "400m"
default:
  cpu: "1000m"

โš ๏ธ Your YAML uses decimals (0.4, 1) โ†’ valid, but m is more explicit.


๐Ÿงช k3s Lab: Test CPU-Only LimitRange

๐Ÿ”ง Step 1: Create Namespace & Apply LimitRange

# Create namespace
kubectl create namespace learning

# Apply LimitRange
kubectl apply -f namespace-cpu-limitrange.yaml

# Verify
kubectl describe limitrange cpu-limit-range -n learning
# Type       Resource  Min  Max  Default Request  Default Limit
# Container  cpu       -    -    400m               1

๐Ÿ”ง Step 2: Deploy Pod with NO CPU Resources

# pod-no-cpu.yaml
apiVersion: v1
kind: Pod
meta
  name: no-cpu-pod
  namespace: learning
spec:
  containers:
  - name: nginx
    image: nginx

kubectl apply -f pod-no-cpu.yaml

# Check applied CPU resources
kubectl get pod no-cpu-pod -n learning -o jsonpath='{.spec.containers[0].resources}'
# โœ… Output:
# {"limits":{"cpu":"1"},"requests":{"cpu":"0.4"}}

๐Ÿ” Result:

LimitRange auto-applied CPU defaults โ†’ Pod has 400m request, 1000m limit

๐Ÿ”ง Step 3: Deploy Pod with Custom CPU (Within Implicit Max)

# pod-custom-cpu.yaml
apiVersion: v1
kind: Pod
meta
  name: custom-cpu-pod
  namespace: learning
spec:
  containers:
  - name: nginx
    image: nginx
    resources:
      requests:
        cpu: "600m"
      limits:
        cpu: "1500m"   # > default limit, but no max defined โ†’ allowed!