Hands-on: Updating a Kubernetes Deployment
In this hands-on section, we'll walk through the process of updating a Kubernetes Deployment. We'll start with an initial deployment, then update the container image to a new version, and observe the update process.
First, let's create a simple Deployment manifest and apply it to our Kubernetes cluster:
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: my-app:v1
ports:
- containerPort: 8080
This Deployment will create three replicas of the my-app:v1
container image.
Now, let's update the container image to a new version:
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: my-app:v2
ports:
- containerPort: 8080
The only change here is the container image tag, which has been updated from v1
to v2
.
To apply the update, we can use the kubectl apply
command:
kubectl apply -f deployment-update.yaml
Kubernetes will now start the update process, gradually replacing the old Pods with the new ones. You can observe the progress using the following commands:
## Watch the Deployment status
kubectl get deployment my-app -w
## View the ReplicaSet and Pod status
kubectl get rs,pods -l app=my-app
During the update, you should see the new ReplicaSet being created, and the old Pods being terminated as the new Pods are rolled out.
Once the update is complete, you can verify that the new container image is running by checking the Pod details:
kubectl describe pod -l app=my-app
This hands-on example demonstrates how easy it is to update a Kubernetes Deployment and leverage the built-in rolling update functionality to ensure a smooth and reliable application deployment process.