Kubernetes: Rollout Strategy
In Kubernetes, a rollout is the process of deploying a new version of an application or service. There are two common rollout strategies: rolling update and recreate. A rolling update gradually replaces pods running the old version with new ones, while a recreate strategy deletes all the old pods and creates new ones. Rollback is the process of undoing a rollout and reverting to a previous version of the application. These features help to minimize downtime and ensure a smooth deployment process.
Rollout
In Kubernetes, a rollout is the process of updating an application to a new version and a new Deployment’s revision is created. When using Deployments, a rollout only occurs when there is a change to the Pod template (specifically, the .spec.template
section). This includes updates to the labels or container images used in the template. However, other changes such as the number of replicas for the Deployment (specified in the .spec.replicas
) do not trigger a rollout.
Rollout Strategy
In Kubernetes, a deployment strategy refers to the method by which you manage updates and rollouts of your application. Essentially, it’s a way to ensure that your application is always available and running, while minimizing downtime and ensuring that new versions of your application are rolled out smoothly.
Rolling update strategy(Default strategy)
This rollout strategy gradually replaces old replicas with new ones, while monitoring the state of the application to ensure that everything is running smoothly. This allows for a smooth transition from the old version to the new, and minimizes the risk of downtime or errors.
Analogy: In a restaurant, gradually introducing a new dish to the menu, starting with a small subset of tables and gradually expanding to the rest of the restaurant. This allows you to get feedback on the dish and make any necessary adjustments before rolling it out to everyone.
Recreate strategy
This rollout strategy involves deleting all the old replicas and creating new ones with the updated version of your application. This results in a brief period of downtime while the old replicas are being deleted and the new ones are being created.
Analogy: Close your restaurant for a short period of time in order to revamp your menu. During this time, you would remove all the old dishes and replace them with new ones, resulting in a brief period of downtime while the kitchen staff prepares and cooks the new dishes. Once the new menu is ready, the restaurant would reopen to customers.
Rollback
Rolling back a deployment in Kubernetes refers to the process of undoing an update that has caused problems or errors. If the new version of the application or service is not functioning correctly, Kubernetes can automatically roll back to the previous stable version. You can also revert to a previous state at any given time because the system stores the complete record of the Deployment’s rollout history.
Analogy: In a restaurant, imagine that the updated recipe for the popular dish is causing some customers to complain about the taste. In this case, the restaurant would need to quickly revert back to the previous recipe to maintain customer satisfaction.
Rollout Strategy with YAML
Add the following declaration to the deployment yaml file under the spec
category.
Rolling update strategy
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: <number_of_pods or percentage_of_desired_pods>
maxUnavailabe: <number_of_pods or percentage_of_desired_pods>
The rollingUpdate
field contains two properties:
maxUnavailable
: specify the maximum number of pods or percentage of desired pods that can be unavailable during the update process.maxSurge
: specify the maximum number of new pods or percentage of desired pods that can be created above the desired number of replicas.
Recreate update strategy
spec:
strategy:
type: Recreate
Commands
- Update the container image of a specific deployment
$ kubectl set image deployment/<deployment_name> <container_name>=<new_image>
analogy: Update the recipe for a dish on the menu. They would swap out the old ingredients with new ones and cook a new version of the dish for customers to try.
2. Retrieve the status of a deployment rollout
$ kubectl rollout status deployment/<deployment_name>
analogy: Check the progress of a new menu rollout. They would check to see how many new dishes have been added to the menu, how many customers are ordering them, and whether any issues have arisen during the rollout.
3. List the revision history of a deployment
$ kubectl rollout history deployment/<deployment_name>
analogy: Look back at the history of their menu, including when new dishes were added, when changes were made to existing dishes, and how popular each dish was over time.
4. Rollback a deployment to a previous revision
$ kubectl rollout undo deployment/<deployment_name>
analogy: Undo a recent menu change that has caused problems for customers. They would revert back to the previous menu version, ensuring that customers can continue to order their favorite dishes without any issues.
These are my personal notes for CKA exam preparation on Kubernetes. Please feel free to correct me if you notice any errors. 😊
Related Story
- Kubernetes: Understanding Kubernetes Architecture through a Restaurant Chef’s Analogy
- Kubernetes: Deployments
Reference: