To do a rolling restart of pods in Kubernetes without changing the
deployment YAML, we can use different ways that help us update smoothly.
One easy way is to use the kubectl rollout restart command.
This command restarts all the pods in a deployment but keeps the current
settings. It helps us reduce downtime and uses Kubernetes’ features for
rolling updates.
In this article, we will look at different ways to do a rolling restart of pods in Kubernetes without changing the deployment YAML. We will talk about these solutions:
- Using
kubectl rollout restartfor a rolling restart of pods in Kubernetes - Using annotations to start a rolling restart of pods in Kubernetes
- Doing a rolling restart of pods in Kubernetes with the patch command
- Doing a rolling restart of pods in Kubernetes using the scale command
- Using the Kubernetes API to do a rolling restart of pods without changing the deployment YAML
These methods give us options and help us manage our Kubernetes deployments. They also make sure our applications stay available during updates. For more details on Kubernetes concepts, check out our article on what are Kubernetes pods and how do I work with them.
Using kubectl rollout restart for a rolling restart of pods in Kubernetes
We can do a rolling restart of pods in Kubernetes without changing
the deployment YAML. We use the kubectl rollout restart
command for this. This command helps us restart the pods that a
deployment manages. It makes sure that we have no downtime.
Command Syntax
The basic command for the rollout restart looks like this:
kubectl rollout restart deployment <deployment-name>Example
For example, if we have a deployment called my-app, we
would run:
kubectl rollout restart deployment my-appVerification
To check that the pods are restarting, we can see the status of the deployment:
kubectl get pods -wThis command will show us the status of the pods. We will see the new pods starting as the old ones stop. This way, the current settings of the deployment stay the same while we refresh the pods.
If you want to read more about managing deployments in Kubernetes, you can look at this guide on Kubernetes deployments.
Leveraging annotations to trigger a rolling restart of pods in Kubernetes
In Kubernetes, we can use annotations to start a rolling restart of pods. We don’t need to change the deployment YAML. When we add an annotation to a deployment, Kubernetes sees it as a change. This will start a new rollout of the pods.
To do this, we can use the kubectl annotate command.
Here is how we do it:
kubectl annotate deployment <deployment-name> <annotation-key>=<annotation-value> --overwriteFor example, we can use a timestamp as the annotation value to force a restart:
kubectl annotate deployment my-deployment kubernetes.io/restart="$(date +%s)" --overwriteThis command adds or updates the annotation
kubernetes.io/restart with the current timestamp. This will
trigger a rolling restart of the pods in our deployment.
Another way is to use custom annotations to show a change:
kubectl annotate deployment my-deployment my.custom/annotation="trigger-restart" --overwriteWhen we apply this annotation, Kubernetes will see the change and restart the pods.
Using annotations for rolling restarts works really well. It helps us avoid changing the deployment configuration or the YAML file directly. This way, we can update and manage our application deployments in Kubernetes easily.
For more information about Kubernetes deployments and how to manage them, we can read about Kubernetes Deployments and how to use them.
Executing a rolling restart of pods in Kubernetes with the patch command
We can do a rolling restart of pods in Kubernetes without changing
the deployment YAML. We use the kubectl patch command for
this. This command helps us update the deployment’s annotations. When we
do this, Kubernetes will restart the pods one by one.
Here is how we can run a rolling restart using the patch
command:
kubectl patch deployment <deployment-name> -n <namespace> -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"kubectl.kubernetes.io/restartedAt\":\"$(date +%Y-%m-%dT%H:%M:%SZ)\"}}}}}}"Explanation:
- We need to replace
<deployment-name>with the name of our deployment. - We should change
<namespace>with the right namespace. We can skip the-n <namespace>part if our deployment is in the default namespace. - This command updates the
kubectl.kubernetes.io/restartedAtannotation with the current time. This makes Kubernetes see the pod template as changed. Then it starts a rolling restart.
This way is easy and does not need us to change the existing deployment YAML file. This makes it a good option for managing rolling restarts in a Kubernetes environment. For more info on managing deployments, we can check Kubernetes Deployments.
Applying a rolling restart of pods in Kubernetes using the scale command
We can do a rolling restart of pods in Kubernetes without changing
the deployment YAML. We use the kubectl scale command for
this. This command helps us change the number of replicas in a
deployment. When we do this, it triggers a rolling restart of the
pods.
Example Command
kubectl scale deployment <deployment-name> --replicas=<new-replica-count>Steps
Identify the Deployment: First, we need to find the name of the deployment we want to restart.
Scale Down: Next, we set the replicas to zero. This will stop all existing pods.
kubectl scale deployment <deployment-name> --replicas=0Scale Up: After that, we scale back up to the number of replicas we want.
kubectl scale deployment <deployment-name> --replicas=<desired-replica-count>
Notes
- This method will cause a short downtime. All pods stop before new ones start.
- We should make sure our application can handle this downtime.
This way is a fast way to do a rolling restart of our pods in Kubernetes. For more details on managing deployments, we can check Kubernetes Deployments.
Using Kubernetes API to do a rolling restart of pods without changing the deployment YAML
We can do a rolling restart of pods in Kubernetes using the Kubernetes API. We do not need to change the deployment YAML for this. We can update the deployment’s settings using a PATCH request. Below are the steps to show how we can do this.
Step 1: Find the Deployment
First, we need to find the deployment we want to restart. We can use this command to see the deployments in a specific namespace:
kubectl get deployments -n your-namespaceStep 2: Use the PATCH Method
To start a rolling restart, we can use the kubectl patch
command. Or we can use a direct API call with a PATCH request to the
deployment resource. Here is how we can do it with
kubectl:
kubectl patch deployment your-deployment-name -n your-namespace -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"$(date +%Y-%m-%dT%H:%M:%S)\"}}}}}}"This command adds or updates an annotation with the current date and time. This will make Kubernetes restart the pods.
Step 3: Direct API Call (Optional)
If we want to use the Kubernetes API directly, we can do a PATCH
request with curl. We should change
your-k8s-api-server, your-token,
your-namespace, and your-deployment-name as
needed.
curl -X PATCH \
https://your-k8s-api-server/apis/apps/v1/namespaces/your-namespace/deployments/your-deployment-name \
-H "Authorization: Bearer your-token" \
-H "Content-Type: application/strategic-merge-patch+json" \
-d '{
"spec": {
"template": {
"metadata": {
"annotations": {
"date": "'$(date +%Y-%m-%dT%H:%M:%S)'"
}
}
}
}
}'Notes
- We need to make sure we have the right permissions to do PATCH requests on the deployment.
- The annotation key (
datehere) can be any key that does not clash with other annotations. - This way we do not need to change the deployment YAML file. It helps to restart the pods smoothly.
If we want to learn more about Kubernetes deployments, we can read about Kubernetes Deployments.
Frequently Asked Questions
What is a rolling restart in Kubernetes, and why is it used?
A rolling restart in Kubernetes helps us update or restart the pods in a deployment one by one. This way, we keep some parts of the application running while we restart others. It is good for making changes or updates without stopping the whole service. So, it is a best practice for keeping high availability.
How do I perform a rolling restart of pods without modifying the deployment YAML?
We can do a rolling restart of pods in Kubernetes without changing
the deployment YAML. We just need to use the
kubectl rollout restart command. This command restarts the
pods linked with a deployment. It lets us update the application without
changing the configuration file. It is simple and effective to make sure
our application runs the latest code or settings.
Can I use annotations to trigger a rolling restart in Kubernetes?
Yes, we can use annotations to start a rolling restart in Kubernetes. When we add or change an annotation on the deployment, Kubernetes will notice it and start a rolling restart of the pods. This way, we can use versioning or other info to trigger the restart without changing the main deployment settings.
What is the difference between the scale command and the rollout restart command in Kubernetes?
The scale command in Kubernetes changes how many copies
of a deployment we have. It can make the application bigger or smaller.
On the other hand, the kubectl rollout restart command
restarts the pods in a deployment but does not change how many copies we
have. Both commands have different jobs: scaling changes capacity, while
rolling restart updates the running instances.
How can I use the Kubernetes API to perform a rolling restart?
We can use the Kubernetes API to start a rolling restart by sending a PATCH request to the deployment resource. By changing the deployment’s spec, like updating the annotations or images, we can trigger a rolling restart in a program way. This is really good for automating deployments and working with CI/CD pipelines. It helps us update applications smoothly.
For more information about Kubernetes and its features, we can check out articles like What are Kubernetes Deployments and How Do I Use Them? and How Do I Perform Rolling Updates in Kubernetes?.