Kubernetes is a command line tool and the kubectl is used to run commands against the Kubernetes clusters.
Sometimes the containers don't work the way they should due to errors. Restarting the pods can solve this problem but kubectl doesn't provide a way to restart the pods directly.
In this tutorial, we are going to know the different ways to restart the Kubernetes pods.
How to restart the Kubernetes Pods with Kubectl
To restart the Kubernetes pods, you need to start the minikube cluster. minikube is a utility that allows running Kubernetes cluster on your local machine. Start Minikube by using the following command -
minikube start
The process will take some time. Now, you can list the pods using the following command -
kubectl get pods
You will be able to see only one pod listed. Now to create a deployment, you have to build a configuration file in the home directory of your system. Type the following command -
touch deployment.YAML
Alright, now the file is created in your home directory. Just open it, paste the below code into the file and save it.
You can create the deployment using the following command -
kubectl create –f deployment.yaml
The deployment is now created. Now, you can check that 2 pods are running in the system.-
kubectl get pods
Method 1: Rolling Restart method
It is the fastest method to restart a pod. The Kubernetes allows you to restart a pod using the rollout restart.
kubectl rollout restart deployment deployment-name
Replace the deployment-name according to you. When this process completes, the same number of replicas will be present but each container will be a fresh instance.
You can check the status of the rollout by listing the pods using kubectl get pods
command.
Method 2: Scale the Replica Count
When your Pod is part of a Deployment, StatefulSet, ReplicaSet, or Replication Controller then you can restart the pods by scaling the number of container replicas you’re running.
It reduces the number of deployment copies to zero and scales back it up to an appropriate state.
kubectl scale deployment deployment-name --replicas=0
kubectl scale deployment deployment-name --replicas=3
It will remove all of your existing pods. then scale the number of replicas to a specific number. With this, Kubernetes will create new Pods.
Method 3: Change an Environment Variable
There's another way to restart a Kubernetes Pod by setting or changing the environment variable.
kubectl set env deployment <deployment name> DEPLOY_DATE="$(date)"
It will restart the pods to synchronise with the modifications you have made.
Conclusion
These are the different ways to restart a Kubernetes Pod using Kubectl. Pods run until they are replaced with a new deployment. So there's no direct way to restart them. You can restart them by the rolling restart method or by, scaling the Replica count or changing the environment variable.
Frequently Asked Questions
1. What is the difference between Kubernetes and Kubectl?
The difference between Kubernetes and Kubectl is that Kubernetes is an open-source container orchestration system used to automate the deployment, scaling, and management of containerized applications. kubectl is a command-line tool used to interact with Kubernetes clusters to run various commands.
2. How can I restart Kubernetes Pods using Kubectl?
To restart Kubernetes Pods using Kubectl, there are three different methods that you can use.
- The first method is the Rolling Restart method, where you use the rollout restart command to restart a pod quickly.
- The second method is to scale the replica count, which involves reducing the number of deployment copies to zero and scaling back up to an appropriate state.
- The third method is changing an environment variable to restart a Kubernetes pod.
3. How to start a Kubernetes cluster using minikube?
To start a Kubernetes cluster using minikube, you need to use the minikube start command, and then you can list the pods using the kubectl get pods command.
4. How can I check if the Pods have been restarted successfully after using one of the methods mentioned in the article?
After using one of the methods mentioned in the article to restart the Pods, you can check the status of the Pods using the following command:
kubectl get pods
This command will list all the Pods in the cluster along with their current status. If the Pods have been restarted successfully, their status should be "Running". If a Pod is still in a "Pending" or "Error" state, it may indicate an issue with the restart process. In this case, you may need to troubleshoot further or try a different method to restart the Pods.
.