Application Deployments and Rolling Updates
Introduction
This tutorial guides you through deploying an application in Kubernetes, performing rolling updates, and executing rollbacks. These are fundamental skills for managing applications in a Kubernetes environment and are crucial for the Certified Kubernetes Administrator (CKA) exam. You should have a basic understanding of Kubernetes concepts such as Pods, Services, and Namespaces before starting this tutorial. A running Kubernetes cluster (minikube, kind, or a cloud-based cluster) is required.
Task 1: Creating a Deployment
Let’s start by creating a simple deployment for a sample application. We’ll use nginx as our example.
- Create a deployment manifest file named
nginx-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80nginx-deployment that manages three replicas of the nginx:1.20 container. The selector and matchLabels ensure that the Deployment manages the correct Pods.
- Apply the deployment using
kubectl:
kubectl apply -f nginx-deployment.yaml- Verify that the deployment is created and the pods are running:
kubectl get deployments
kubectl get pods- Expected output:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 <time>
NAME READY STATUS RESTARTS AGE
nginx-deployment-7c66c9b8d-4lj6r 1/1 Running 0 <time>
nginx-deployment-7c66c9b8d-g8j6z 1/1 Running 0 <time>
nginx-deployment-7c66c9b8d-m92qp 1/1 Running 0 <time>Task 2: Exposing the Deployment with a Service
To access the deployed application, we need to create a Service. We’ll use a NodePort service type for easy access.
-
Create a service manifest file named
nginx-service.yaml:NODE_TYPE // yamlapiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort selector: app: nginx ports: - port: 80 targetPort: 80 nodePort: 30080This YAML file defines a Service namednginx-servicethat exposes the nginx deployment on port 30080 of each node in the cluster. -
Apply the service using
kubectl:NODE_TYPE // bashkubectl apply -f nginx-service.yaml -
Verify that the service is created:
NODE_TYPE // bashkubectl get services -
Expected output:
NODE_TYPE // outputNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP <time> nginx-service NodePort 10.108.255.24 <none> 80:30080/TCP <time> -
Access the application: Open your web browser and navigate to
http://<node-ip>:30080. You should see the default nginx welcome page. You can find the node IP usingkubectl get nodes -o wide. For minikube, useminikube ip.
Task 3: Performing a Rolling Update
Now, let’s perform a rolling update to deploy a new version of the nginx image.
- Update the
nginx-deployment.yamlfile to usenginx:1.21image:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21 # Changed image version
ports:
- containerPort: 80- Apply the updated deployment:
kubectl apply -f nginx-deployment.yaml- Monitor the rollout status:
kubectl rollout status deployment/nginx-deployment- Verify that the pods are running the new version:
kubectl get pods -o wide- Expected output: (Note the change in the pod age as new pods get created and the old ones terminated)
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-69d4f57b95-85wtx 1/1 Running 0 <time> 10.244.0.6 <node-name> <none> <none>
nginx-deployment-69d4f57b95-k64qz 1/1 Running 0 <time> 10.244.0.5 <node-name> <none> <none>
nginx-deployment-69d4f57b95-qndlw 1/1 Running 0 <time> 10.244.0.7 <node-name> <none> <none>- Refresh your browser: You might not see a visible difference as the content remains the same, but the underlying nginx version has been updated. You can verify this by configuring the nginx image to serve a custom page displaying the version.
Task 4: Performing a Rollback
Let’s simulate a scenario where the new version has issues and we need to roll back to the previous version.
- Undo the last deployment:
kubectl rollout undo deployment/nginx-deployment- Monitor the rollout status:
kubectl rollout status deployment/nginx-deployment- Verify that the pods are running the previous version (
nginx:1.20):
kubectl get pods -o wide- Check the rollout history:
kubectl rollout history deployment/nginx-deploymentkubectl rollout undo deployment/nginx-deployment --to-revision=<revision-number>.
- Now, access the service through your browser once more. You should be using the older version of Nginx.
Conclusion
In this tutorial, you learned how to deploy an application in Kubernetes using Deployments, expose it with a Service, perform rolling updates to deploy new versions, and roll back to previous versions in case of issues. These skills are essential for managing applications in Kubernetes and are frequently tested in the CKA exam. You have successfully practiced the core concepts of deployments and version management in Kubernetes.