How to use the command 'kubectl rollout' (with examples)
The kubectl rollout
command is used to manage the rollout of Kubernetes resources such as deployments, daemonsets, and statefulsets. It provides several subcommands to perform different actions related to rolling updates and revision history of resources.
Use case 1: Start a rolling restart of a resource
Code:
kubectl rollout restart resource_type/resource_name
Motivation: The rolling restart of a resource is necessary when changes need to be applied to the Pods running within a resource without affecting the availability of the application. It restarts the pods in a rolling manner, ensuring that new pods are created before terminating the old ones.
Explanation:
resource_type
: The type of the Kubernetes resource you want to restart. This can be “deployment”, “daemonset”, or “statefulset”.resource_name
: The name of the resource you want to restart.
Example output:
deployment.apps/my-deployment restarted
Use case 2: Watch the rolling update status of a resource
Code:
kubectl rollout status resource_type/resource_name
Motivation: Monitoring the status of rolling updates is crucial to ensure that the updates are progressing as expected and there are no issues or errors occurring during the process. By using this command, you can get real-time information on the status of the rolling update.
Explanation:
resource_type
: The type of the Kubernetes resource you want to monitor. This can be “deployment”, “daemonset”, or “statefulset”.resource_name
: The name of the resource you want to monitor.
Example output:
Waiting for deployment "my-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Use case 3: Roll back a resource to the previous revision
Code:
kubectl rollout undo resource_type/resource_name
Motivation: Sometimes, after making changes to a resource, you might realize that the new version is causing issues or it’s not performing as expected. In such cases, rolling back to the previous revision can help in restoring the resource to a known working state.
Explanation:
resource_type
: The type of the Kubernetes resource you want to roll back. This can be “deployment”, “daemonset”, or “statefulset”.resource_name
: The name of the resource you want to roll back.
Example output:
deployment.apps/my-deployment rolled back to revision 1
Use case 4: View the rollout history of a resource
Code:
kubectl rollout history resource_type/resource_name
Motivation: The rollout history of a resource provides insights into the different revisions and changes made to the resource over time. It helps in understanding and analyzing the deployment history, allowing users to track changes and identify potential issues.
Explanation:
resource_type
: The type of the Kubernetes resource you want to view the history for. This can be “deployment”, “daemonset”, or “statefulset”.resource_name
: The name of the resource you want to view the history for.
Example output:
deployments "my-deployment":
REVISION CHANGE-CAUSE
1 <none>
2 kubectl apply --filename=my-updated-manifest.yaml
3 kubectl rollout restart deployment/my-deployment
Conclusion
The kubectl rollout
command provides a set of useful subcommands for managing rolling updates and revision history of Kubernetes resources. By using these subcommands, you can easily perform actions like rolling restarts, monitoring update status, rolling back to previous revisions, and viewing the rollout history of a resource. These capabilities are essential for efficient resource management and ensuring the stability and availability of your applications in a Kubernetes cluster.