Scaling Kubernetes Deployment Horizontally
Horizontal scaling in Kubernetes Deployments refers to the process of adjusting the number of replicas (Pods) running your application. This is useful when you need to handle increased or decreased workloads by adding or removing instances of your application.
Updating the Replica Count
To scale a Kubernetes Deployment horizontally, you need to update the replicas
field in the Deployment specification. Here's an example:
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
ports:
- containerPort: 8080
In this example, the Deployment will create and manage five replicas of the labex/my-app:v1
container image.
Applying Horizontal Scaling
To apply the horizontal scaling changes, you can use the kubectl scale
command:
kubectl scale deployment my-app --replicas=10
This will scale the Deployment to 10 replicas.
Alternatively, you can update the Deployment manifest and apply the changes using kubectl apply
:
kubectl apply -f deployment.yaml
Kubernetes will then gradually roll out the changes, ensuring that the new replica count is achieved without disrupting the application's availability.
Autoscaling with Kubernetes Horizontal Pod Autoscaler (HPA)
For more advanced scaling scenarios, you can use the Kubernetes Horizontal Pod Autoscaler (HPA) to automatically scale your Deployment based on various metrics, such as CPU utilization or custom metrics. The HPA will automatically adjust the replica count to maintain the desired performance targets.
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.