How to Use the Command 'kubectl rollout' (with Examples)
The kubectl rollout
command is an essential tool for managing the rollout of Kubernetes resources such as deployments, daemonsets, and statefulsets. It allows developers and administrators to effectively manage the deployment lifecycle within a Kubernetes cluster, ensuring that applications can be updated, monitored, and reverted with ease. This article explores various use cases of the kubectl rollout
command to showcase its flexibility and powerful features.
Start a Rolling Restart of a Resource
Code:
kubectl rollout restart deployment/nginx-deployment
Motivation: You have just updated the image version of your application container. To apply this change, you want to restart the application pods without downtime. A rolling restart allows for seamless updates ensuring that there is no disruption in service as old pods are gradually replaced with new ones running the updated image.
Explanation:
kubectl
: The Kubernetes command-line tool used to communicate with a Kubernetes cluster.rollout
: Sub-command related to deployments, daemonsets, and statefulsets.restart
: Specific action to perform a restart on the resource.deployment/nginx-deployment
: The specific resource type and name you are targeting for the restart. Here,deployment
denotes that it is a deployment resource, andnginx-deployment
is the name of that resource.
Example Output:
deployment.apps/nginx-deployment restarted
This output confirms that the rolling restart operation was initiated successfully on the specified resource.
Watch the Rolling Update Status of a Resource
Code:
kubectl rollout status deployment/nginx-deployment
Motivation: After initiating an update to a deployment, you want to monitor the status to ensure that the rollout is successful and complete without errors. Watching the rollout status helps in identifying potential issues during the deployment process.
Explanation:
kubectl
: The CLI for Kubernetes cluster operations.rollout
: The command related to managing rollouts.status
: A directive to monitor the current state of the rollout.deployment/nginx-deployment
: Indicates the type and name of the resource being monitored. In this case, it’s adeployment
with the namenginx-deployment
.
Example Output:
Waiting for deployment "nginx-deployment" rollout to finish: 2 of 3 updated replicas are available...
deployment "nginx-deployment" successfully rolled out
This output initially indicates the progress of the rollout and concludes with confirmation of its successful completion.
Roll Back a Resource to the Previous Revision
Code:
kubectl rollout undo deployment/nginx-deployment
Motivation: If a recent update to a deployment resulted in errors or unexpected behavior, you may need to revert to a previous stable version. Rolling back to the previous revision provides an immediate fix for deployment issues, restoring the application’s previous state.
Explanation:
kubectl
: The tool used for managing Kubernetes resources.rollout
: A command that interfaces with deployment objects.undo
: An action taken to revert to a previous state.deployment/nginx-deployment
: Designates the resource typedeployment
and its namenginx-deployment
, to be rolled back.
Example Output:
deployment.apps/nginx-deployment rolled back
This output signifies that the rollback operation has completed, returning the deployment to its prior configuration.
View the Rollout History of a Resource
Code:
kubectl rollout history deployment/nginx-deployment
Motivation: Understanding the rollout history of a deployment is crucial for tracking changes over time, including image updates, configuration changes, and rollbacks. This historical insight aids in auditing, debugging, and verifying the impact of changes made to deployments.
Explanation:
kubectl
: The Kubernetes CLI tool.rollout
: Pertains to controlling rollout operations.history
: Fetches and displays past rollout events and changes.deployment/nginx-deployment
: Specifies looking into thedeployment
resource namednginx-deployment
.
Example Output:
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 ...
2 ...
3 kubectl apply --filename...
The output lists each revision, giving users visibility into the sequence of changes and their causes.
Conclusion
The kubectl rollout
command is a powerful utility in Kubernetes that enables effective management of application deployment lifecycles. Whether restarting deployments, monitoring rollout status, rolling back changes, or viewing revision history, these use cases demonstrate how kubectl rollout
empowers developers and administrators with robust tools for maintaining application availability and integrity in a Kubernetes ecosystem.