A new Deployment will take effect when it's applied.
kubectl apply -f deployment.yaml
kubectl get deployment myapp-deployment # to verify the deployment
The kubectl apply
command and the kubectl rollout
command serve different purposes in managing deployments in Kubernetes.
kubectl apply
- Primary Use: The kubectl apply command is used to create or update resources in a Kubernetes cluster. It takes a configuration file (usually in YAML format) and applies it to the cluster. This is the command you use to initially create objects like Deployments, Services, ConfigMaps, etc., or to apply changes to them.
- Configuration Changes: When you have a new configuration for your Deployment (like updating the version of the container image), you use kubectl apply with the updated YAML file to make changes to the Deployment.
- Declarative Approach: kubectl apply is part of the declarative approach to managing Kubernetes resources, where you declare the desired state of the resource in a file and let Kubernetes make the necessary changes to achieve that state.
kubectl rollout
- Primary Use: The kubectl rollout command is used to manage the deployment process of a Deployment, DaemonSet, StatefulSet, or ReplicaSet after it has been created or updated. It allows you to see the status of the rollout, pause, resume, or undo changes (roll back to a previous version).
- Monitoring and Managing Rollouts: After you use kubectl apply to update a Deployment, you can use kubectl rollout status to watch the status of the update. This command shows you if the rollout is successful, stuck, or failed.
- Rollback: If you find issues with the latest version of your application after applying changes, you can use kubectl rollout undo to revert to a previous version of the Deployment. This is critical for quick recovery in production environments.
- Fine-Tuned Control: kubectl rollout provides more granular control over the deployment process, like pausing and resuming rollouts, which is not achievable with kubectl apply.
kubectl rollout
cannot be used to initiate a new deployment. Its purpose is to manage and control the rollout process of an existing deployment, such as updating, pausing, resuming, or rolling back an in-flight deployment. A typical deployment flow will be like:
kubectl apply -f deployment.yaml
kubectl rollout status deployment/myapp-deployment # view status
kubectl rollout pause deployment/myapp-deployment # pause
kubectl rollout resume deployment/myapp-deployment # resume
kubectl rollout undo deployment/myapp-deployment # rollback