Scaling Kubernetes Deployments
Scaling Up
To scale up a Deployment, you can increase the spec.replicas
field in the Deployment manifest and apply the changes. Kubernetes will automatically create new Pods to match the desired replica count.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 5
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: labex/my-app:v1
After applying the updated manifest, you can verify the new replica count using kubectl get deployment
:
$ kubectl get deployment my-app
NAME READY UP-TO-DATE AVAILABLE AGE
my-app 5/5 5 5 2m
Scaling Down
To scale down a Deployment, you can decrease the spec.replicas
field in the Deployment manifest and apply the changes. Kubernetes will automatically remove Pods to match the desired replica count.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: labex/my-app:v1
After applying the updated manifest, you can verify the new replica count using kubectl get deployment
:
$ kubectl get deployment my-app
NAME READY UP-TO-DATE AVAILABLE AGE
my-app 3/3 3 3 3m
Autoscaling Deployments
Kubernetes also supports automatic scaling of Deployments using the Horizontal Pod Autoscaler (HPA) resource. The HPA can monitor various metrics, such as CPU utilization or custom metrics, and automatically scale the Deployment based on the defined scaling policies.
Here's an example HPA configuration:
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
This HPA will automatically scale the "my-app" Deployment between 3 and 10 replicas based on the average CPU utilization of the Pods.