This article explores rolling updates and rollbacks, demonstrating how to update applications with minimal downtime using Kubernetes.
In this lesson, we explore rolling updates and rollbacks, with a focus on rolling updates. We will demonstrate how to verify your environment and update your application while ensuring minimal downtime. For simplicity, we use an alias (k) for kubectl. The alias is confirmed to be set with the following output:
Copy
Ask AI
kubectl [flags] [options]Use "kubectl <command> --help" for more information about a given command.Use "kubectl options" for a list of global command-line options (applies to all commands).controlplane ~ ⬢ aliasalias k='kubectl'alias kubectl='k3s kubectl'alias vi='vim'controlplane ~ ⬢ cl
We have deployed a simple web application. First, inspect the Pods:
Copy
Ask AI
controlplane ~ ⟩ k get podNAME READY STATUS RESTARTS AGEfrontend-5c74c57d95-mkjgh 1/1 Running 0 48sfrontend-5c74c57d95-dkbjj 1/1 Running 0 48sfrontend-5c74c57d95-gk6xp 1/1 Running 0 48sfrontend-5c74c57d95-xpwbt 1/1 Running 0 48s
Then, check the Deployment details:
Copy
Ask AI
controlplane ~ ⟩ k get deployNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 55scontrolplane ~ ⟩
Wait for the application to be fully deployed. Once it is ready, open the provided web app portal in your browser. The application displays the message “hello, front end moment.”
Q1: What Is the Current Color of the Web Application?
Recheck the Pods and Deployment information:
Copy
Ask AI
controlplane ~ ↗ k get podNAME READY STATUS RESTARTS AGEfrontend-5c74c57d95-nkgjh 1/1 Running 0 48sfrontend-5c74c57d95-dkbj1 1/1 Running 0 48sfrontend-5c74c57d95-gk6xp 1/1 Running 0 48sfrontend-5c74c57d95-xpwbt 1/1 Running 0 48scontrolplane ~ ↗ k get deployNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 55s
After running a test script that sends multiple requests, the output confirms the application color is blue:
Copy
Ask AI
Hello, Application Version: v1 ; Color: blue OKHello, Application Version: v1 ; Color: blue OK...
This script simulates multiple users accessing the web application, confirming that all responses return the blue color. Additionally, the deployment details confirm that four Pods have been created as expected.
The deployment details show that a rolling update strategy is in use. With this strategy, Pods are replaced one at a time, ensuring continuous availability. Specifically, only 25% of Pods are allowed to be unavailable at any given moment, preventing complete downtime.
A rolling update strategy allows gradual replacement of Pods to ensure that some instances are always available during an upgrade.
To upgrade the application, update the image in the deployment to version 2 using the “kubectl set image” command. Our container name is “simple-webapp”. Run the following command:
Copy
Ask AI
kubectl set image deploy frontend simple-webapp=kodekloud/webapp-color:v2
After executing the command, verify that the image has been updated by describing the deployment again. You should now see version v2 in the container image field.Run the curl test (or your test script) again:
Copy
Ask AI
./curl-test.sh
The output may initially show a mix of blue and green responses:
Copy
Ask AI
Hello, Application Version: v1 ; Color: blue OKHello, Application Version: v2 ; Color: green OK...
This indicates a gradual rollout where some Pods still run the older version (blue) and others have been updated (green). Eventually, all responses will reflect the new version.
With the current rolling update settings (25% max unavailable) and a deployment of 4 replicas, only one Pod is taken down at a time. The following command confirms these updates and settings:
Copy
Ask AI
controlplane ~ ✗ k set image deploy frontend simple-webapp=kodekloud/webapp-color:v2deployment.apps/frontend image updatedcontrolplane ~ ✗ k describe deploy frontendName: frontendNamespace: defaultCreationTimestamp: Sat, 16 Apr 2022 20:35:48 +0000...Replicas: 4 desired | 2 updated | 5 total | 3 available | 2 unavailableStrategyType: RollingUpdateMinReadySeconds: 20RollingUpdateStrategy: 25% max unavailable, 25% max surge...
If you prefer to minimize complexities related to gradual updates, you can change the deployment strategy to “Recreate”. This strategy terminates all running Pods before creating new ones. To update the deployment strategy, use kubectl edit on the deployment and modify the strategy section as shown below:
After saving the changes, verify that the strategy has been updated to “Recreate”. Note that the recreate strategy does not support rolling update parameters.
Switching to the “Recreate” strategy may cause temporary downtime since all Pods are terminated before new ones are created.
Finally, upgrade the application by setting the deployment image to version 3:
Copy
Ask AI
kubectl set image deploy frontend simple-webapp=kodekloud/webapp-color:v3
Then, run your test script once again:
Copy
Ask AI
./curl-test.sh
Since the deployment strategy is now set to “Recreate”, you might experience a brief period of downtime with failed requests (e.g., “Failed” messages or “bad gateway” errors) during the Pod replacement process. Once the new Pods are running, the output should stabilize to:
Copy
Ask AI
Hello, Application Version: v3 ; Color: red OKHello, Application Version: v3 ; Color: red OK...
This confirms that the new version (v3) displaying red is fully deployed.That concludes the lesson on rolling updates and rollbacks.