When traffic rises, an application needs more capacity to serve users; when traffic falls, excess capacity costs money. Scalability means being able to adjust that capacity without waiting for someone to intervene during every spike. section 6.0
In Kubernetes, two kinds of capacity must grow together:
- Pods run copies of the application. Increase replicas to serve more traffic, either by changing a Deployment’s replicas field or by using kubectl scale. section 6.1
- Nodes provide the compute resources those Pods need. More Pods without room to run may remain Pending; more nodes without Pods provide only empty capacity. section 6.1
Adding replicas is horizontal scaling; giving each replica more resources is vertical scaling. A Horizontal Pod Autoscaler (HPA) can add or remove replicas against a target metric, within configured minimum and maximum counts. For a CPU target, it compares average utilization across Pods with the target, using their CPU resource requests. Cooldown periods limit repeated changes. Cluster autoscaling can add nodes when the provider supports it, completing the other side of the capacity adjustment. section 6.2 section 6.5
Listing 6.2 Chapter06/6.2_HPA/hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: timeserver
spec:
minReplicas: 1 #1
maxReplicas: 10 #2
metrics:
- resource:
name: cpu
target:
averageUtilization: 20 #3
type: Utilization
type: Resource
scaleTargetRef: #4
apiVersion: apps/v1 #4
kind: Deployment #4
name: timeserver #4
This HPA configuration targets a Deployment, sets a 20% average CPU-utilization target, and bounds scaling between one and ten replicas. section 6.2
Test a traffic spike
Add a CPU-intensive /pi endpoint, generate requests with Apache Bench, then watch CPU use and replica counts with kubectl. If requested Pods stay Pending, check whether the cluster has enough node capacity rather than assuming the HPA failed. section 6.2 section 6.1
Plan for horizontal scaling while building the application, not after an unexpected surge. Where parts of an application need different amounts of capacity, microservices or multiple deployments can provide separate scaling groups. Placeholder Pods can also provide capacity headroom while autoscaling responds. section 6.4 section 6.5


























