Sub topic 1
Three ways to scale: more Pods, bigger Pods, more nodes
Prerequisites
This chapter assumes you can do the following:
- Read a Deployment manifest and apply it with
kubectl. - Explain what a Pod, a Deployment and a Service are.
- Describe CPU and memory use of a container in rough terms.
You do not need any autoscaling experience. Later chapters cover each mechanism in detail. This chapter gives you the map.
What "scaling" means in Kubernetes
Scaling means changing the capacity of a running application so it can handle more or less load. Kubernetes gives you three separate dimensions to change. Each one acts on a different object, and each one has limits.
| Dimension | What changes | Typical object | Automatic tool |
|---|---|---|---|
| Horizontal | Number of Pod replicas | Deployment `replicas` | Horizontal Pod Autoscaler (HPA) |
| Vertical | CPU and memory of each Pod | Container `resources` | Vertical Pod Autoscaler (VPA) |
| Node | Number of machines in the cluster | Node pool or node group | Cluster autoscaler |
Horizontal scaling: more Pods
Horizontal scaling adds or removes identical Pod replicas. A Service spreads traffic across them. This is the most common form of scaling because it works for any stateless application, and it also improves availability: if one replica fails, the others keep serving.
You set the count with the replicas field:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
resources:
requests:
cpu: 200m
memory: 128Mi
limits:
memory: 256MiThe requests block matters here. The scheduler uses requests to decide which node has room for a Pod. The HPA also measures CPU use as a percentage of the request. Without requests, percentage-based autoscaling has nothing to compare against.
Vertical scaling: bigger Pods
Vertical scaling changes the requests and limits of each container. Use it when one replica needs more resources than you gave it, or when you gave it far more than it uses and are wasting capacity.
Changing resources on a standard Pod usually means the Pod is replaced, so the app restarts. The Vertical Pod Autoscaler automates this. It is not part of core Kubernetes. You install it separately.
Node scaling: more machines
Pods run on nodes. If you ask for more replicas than the nodes can hold, the extra Pods stay in the Pending state. Node scaling adds machines so those Pods can be scheduled. A cluster autoscaler watches for Pending Pods and adds nodes. It also removes nodes that are underused. This depends on your environment, such as a managed cloud service, and on the autoscaler you install.
How the three fit together
The dimensions depend on each other. More replicas need room on nodes. Correct requests let the scheduler place Pods and let the HPA compute utilization. Node scaling reacts to the Pods you create.
Choosing a dimension
- If the app is stateless and load varies, start with horizontal scaling.
- If a single Pod is too small or too large, adjust its requests, by hand or with the VPA.
- If Pods are Pending because no node has room, add node capacity.
Most production setups combine all three. The remaining chapters build them up in order, beginning with setting requests and limits correctly.
Exercise
A Deployment has 10 replicas requested, but 3 Pods are stuck in Pending with a message that no node has enough CPU. Which scaling dimension addresses this, and why would adding more replicas not help?
Show answer
Node scaling. The cluster has no room to schedule the Pods, so more nodes (for example through a cluster autoscaler) are needed. Adding replicas only creates more Pods that would also be Pending.
Exercise
Why does the Deployment in this chapter set a CPU request, and which two components rely on it?
Show answer
The request states how much CPU each Pod needs. The scheduler uses it to choose a node with enough room, and the HPA uses it as the base for CPU utilization percentages.