DEV Community

Cover image for 5.Rolling Updates And Rolling Back Deployments in Kubernetes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

5.Rolling Updates And Rolling Back Deployments in Kubernetes

Lab Information

There is a production deployment planned for next week. The Nautilus DevOps team wants to test the deployment update and rollback on Dev environment first so that they can identify the risks in advance. Below you can find more details about the plan they want to execute.

Create a namespace devops. Create a deployment called httpd-deploy under this new namespace, It should have one container called httpd, use httpd:2.4.25 image and 3 replicas. The deployment should use RollingUpdate strategy with maxSurge=1, and maxUnavailable=2. Also create a NodePort type service named httpd-service and expose the deployment on nodePort: 30008.

Now upgrade the deployment to version httpd:2.4.43 using a rolling update.

Finally, once all pods are updated undo the recent update and roll back to the previous/original version.

Note:

a. The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

b. Please make sure you only use the specified image(s) for this deployment and as per the sequence mentioned in the task description. If you mistakenly use a wrong image and fix it later, that will also distort the revision history which can eventually fail this task.

Lab Solutions

Step 1: Create the Namespace

First, create the devops namespace:

kubectl create namespace devops
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Deployment

Create a YAML file for the deployment with the specified configuration:

cat > httpd-deploy.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deploy
  namespace: devops
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 2
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.4.25
        ports:
        - containerPort: 80
EOF
Enter fullscreen mode Exit fullscreen mode

Apply the deployment:

kubectl apply -f httpd-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Service

Create a YAML file for the NodePort service:

cat > httpd-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
  name: httpd-service
  namespace: devops
spec:
  type: NodePort
  selector:
    app: httpd
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30008
EOF
Enter fullscreen mode Exit fullscreen mode

Apply the service:

kubectl apply -f httpd-service.yaml
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Initial Deployment

Check that everything is running correctly:

kubectl get all -n devops
Enter fullscreen mode Exit fullscreen mode

Step 5: Upgrade the Deployment

Upgrade to the new image version using rolling update:

kubectl set image deployment/httpd-deploy httpd=httpd:2.4.43 -n devops
Enter fullscreen mode Exit fullscreen mode

Monitor the rolling update progress:

kubectl rollout status deployment/httpd-deploy -n devops
Enter fullscreen mode Exit fullscreen mode

Verify the upgrade:

kubectl get pods -n devops -o wide
kubectl describe deployment httpd-deploy -n devops
Enter fullscreen mode Exit fullscreen mode

Step 6: Rollback to Previous Version

Undo the recent update and roll back to the original version:

kubectl rollout undo deployment/httpd-deploy -n devops
Enter fullscreen mode Exit fullscreen mode

Monitor the rollback progress:

kubectl rollout status deployment/httpd-deploy -n devops
Enter fullscreen mode Exit fullscreen mode

Step 7: Final Verification

Verify that the rollback was successful:

kubectl get pods -n devops -o wide
kubectl describe deployment httpd-deploy -n devops
Enter fullscreen mode Exit fullscreen mode

Check the rollout history:

kubectl rollout history deployment/httpd-deploy -n devops
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)