Technical Theory

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.

  1. Create a deployment manifest file named nginx-deployment.yaml:
NODE_TYPE // 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: 80
This YAML file defines a Deployment named nginx-deployment that manages three replicas of the nginx:1.20 container. The selector and matchLabels ensure that the Deployment manages the correct Pods.
  1. Apply the deployment using kubectl:
NODE_TYPE // bash
kubectl apply -f nginx-deployment.yaml
  1. Verify that the deployment is created and the pods are running:
NODE_TYPE // bash
kubectl get deployments
kubectl get pods
  1. Expected output:
NODE_TYPE // 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.

  1. Create a service manifest file named nginx-service.yaml:

    NODE_TYPE // yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
    spec:
      type: NodePort
      selector:
        app: nginx
      ports:
      - port: 80
        targetPort: 80
        nodePort: 30080
    This YAML file defines a Service named nginx-service that exposes the nginx deployment on port 30080 of each node in the cluster.
  2. Apply the service using kubectl:

    NODE_TYPE // bash
    kubectl apply -f nginx-service.yaml
  3. Verify that the service is created:

    NODE_TYPE // bash
    kubectl get services
  4. Expected output:

    NODE_TYPE // output
    NAME            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>
  5. 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 using kubectl get nodes -o wide. For minikube, use minikube ip.

Task 3: Performing a Rolling Update

Now, let’s perform a rolling update to deploy a new version of the nginx image.

  1. Update the nginx-deployment.yaml file to use nginx:1.21 image:
NODE_TYPE // 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.21 # Changed image version
        ports:
        - containerPort: 80
  1. Apply the updated deployment:
NODE_TYPE // bash
kubectl apply -f nginx-deployment.yaml
  1. Monitor the rollout status:
NODE_TYPE // bash
kubectl rollout status deployment/nginx-deployment
This command will show the progress of the rolling update. Kubernetes will gradually replace the old pods with new ones, ensuring minimal downtime.
  1. Verify that the pods are running the new version:
NODE_TYPE // bash
kubectl get pods -o wide
  1. Expected output: (Note the change in the pod age as new pods get created and the old ones terminated)
NODE_TYPE // output
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>
  1. 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.

  1. Undo the last deployment:
NODE_TYPE // bash
kubectl rollout undo deployment/nginx-deployment
  1. Monitor the rollout status:
NODE_TYPE // bash
kubectl rollout status deployment/nginx-deployment
  1. Verify that the pods are running the previous version (nginx:1.20):
NODE_TYPE // bash
kubectl get pods -o wide
  1. Check the rollout history:
NODE_TYPE // bash
kubectl rollout history deployment/nginx-deployment
This command displays the revision history of the deployment. You can rollback to a specific revision using kubectl rollout undo deployment/nginx-deployment --to-revision=<revision-number>.
  1. 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.

Next Topic