Day 4 of 5
⏱ ~60 minutes
Kubernetes in 5 Days — Day 4

Scaling and Resource Management

Set resource requests and limits, configure a Horizontal Pod Autoscaler, and understand how the scheduler places Pods.

Resource Requests and Limits

resources in a Deployment
containers:
- name: web
  image: nginx:alpine
  resources:
    requests:
      memory: '64Mi'   # guaranteed minimum
      cpu: '100m'      # 100 millicores = 0.1 CPU
    limits:
      memory: '128Mi'  # hard cap
      cpu: '500m'      # throttled if exceeded

# CPU units: 1000m = 1 core. 100m = 0.1 core
# Memory: Mi = mebibytes, Gi = gibibytes
Horizontal Pod Autoscaler
# HPA scales Pods based on CPU or memory usage
kubectl autoscale deployment web-app \
  --min=2 --max=10 --cpu-percent=50

# Or as YAML:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
Check HPA status
kubectl get hpa
kubectl describe hpa web-hpa
# Columns: TARGETS shows current/target CPU usage
# REPLICAS shows current pod count
💡
HPA requires the Metrics Server to be running. On minikube: minikube addons enable metrics-server. In production clusters, deploy it separately: kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml.
📝 Day 4 Exercise
Set Up Autoscaling
  1. A
  2. d
  3. d
  4. r
  5. e
  6. s
  7. o
  8. u
  9. r
  10. c
  11. e
  12. r
  13. e
  14. q
  15. u
  16. e
  17. s
  18. t
  19. s
  20. a
  21. n
  22. d
  23. l
  24. i
  25. m
  26. i
  27. t
  28. s
  29. t
  30. o
  31. y
  32. o
  33. u
  34. r
  35. D
  36. e
  37. p
  38. l
  39. o
  40. y
  41. m
  42. e
  43. n
  44. t
  45. .
  46. E
  47. n
  48. a
  49. b
  50. l
  51. e
  52. m
  53. e
  54. t
  55. r
  56. i
  57. c
  58. s
  59. -
  60. s
  61. e
  62. r
  63. v
  64. e
  65. r
  66. o
  67. n
  68. m
  69. i
  70. n
  71. i
  72. k
  73. u
  74. b
  75. e
  76. .
  77. C
  78. r
  79. e
  80. a
  81. t
  82. e
  83. a
  84. n
  85. H
  86. P
  87. A
  88. t
  89. a
  90. r
  91. g
  92. e
  93. t
  94. i
  95. n
  96. g
  97. 5
  98. 0
  99. %
  100. C
  101. P
  102. U
  103. .
  104. U
  105. s
  106. e
  107. a
  108. l
  109. o
  110. a
  111. d
  112. t
  113. o
  114. o
  115. l
  116. (
  117. h
  118. e
  119. y
  120. ,
  121. v
  122. e
  123. g
  124. e
  125. t
  126. a
  127. ,
  128. o
  129. r
  130. a
  131. s
  132. i
  133. m
  134. p
  135. l
  136. e
  137. b
  138. a
  139. s
  140. h
  141. l
  142. o
  143. o
  144. p
  145. )
  146. t
  147. o
  148. g
  149. e
  150. n
  151. e
  152. r
  153. a
  154. t
  155. e
  156. t
  157. r
  158. a
  159. f
  160. f
  161. i
  162. c
  163. a
  164. n
  165. d
  166. w
  167. a
  168. t
  169. c
  170. h
  171. t
  172. h
  173. e
  174. H
  175. P
  176. A
  177. s
  178. c
  179. a
  180. l
  181. e
  182. u
  183. p
  184. .

Day 4 Summary

  • Always set resource requests. Without them, the scheduler can't make good placement decisions.
  • Requests = guaranteed resources. Limits = hard cap. CPU throttles at limit; memory gets OOMKilled.
  • HPA watches metrics and adds/removes Pods to meet the target utilization.
  • Scaling requires the Metrics Server. Don't forget to enable it.
Finished this lesson?