Pods in Kubernetes should typically run until a new deployment replaces them. As a result, “restarting” a single Pod is not possible. When one of the containers fails, instead of restarting it, replace it. The small change in terminology better aligns with Kubernetes Pods’ stateless operating architecture. To eliminate old containers and begin fresh new instances, start a rollout. One can also manually remove Pods from a ReplicaSet or scale the replica count. For modern Kubernetes releases, rollouts are the preferred method, but other ways can be more suited to specific cases. This guide will show different ways to restart Kubernetes pods.
To run the instructions in Kubernetes, you will need to install Ubuntu 20.04 on your Linux operating system. To run Kubernetes on Linux, you must also install the Minikube cluster on your workstation. Minikube creates a smooth experience by allowing you to test commands and programs systematically. As a result, it offers the best learning experience for Kubernetes beginners. The minikube cluster must be started initially. Then, in Ubuntu 20.04, go to the command line terminal you just installed. By using the Ctrl+Alt+T shortcut key or putting “Terminal” into the Ubuntu 20.04 system’s search box, you can do so.
Either of the aforementioned methods will start the terminal in its entirety. After then, the minikube will be initiated. To start the minikube, type “minikube start” into the terminal. A virtual machine capable of running a single node cluster will be constructed, and the Kubernetes cluster will be launched. It also goes with the kubectl setup. This will be utilized to communicate with the cluster.
Restarting Kubernetes Pods
Assume one of your container’s pods is reporting an error. Kubernetes may try to resume the pod automatically based on the restart policy to rebuild functionality. However, this solution does not always solve the problem.
First, we are going to list all the deployments. To see if the Deployment was created, use kubectl to get deployments. When you observe your cluster’s Deployments, you’ll notice the following fields. The titles of the Deployments are listed in NAME. The number of replicas of the application available to your users is displayed in READY.
Rolling Restart Method
Kubernetes now allows you to execute a rolling restart of your deployment as of version 1.15. This is the quickest restart mechanism in Kubernetes, as it is a new addition. The command given above shuts down and restarts each container in your deployment one by one. Because most of the containers are still functioning, your app will be accessible.
Environment Variable Method
Another option is to force pods to restart and sync with your changes by setting or changing an environment variable. You can, for instance, alter the date of container deployment:
In the scenario above, set env modifies the environment variables, deployment [deployment name] selects your deployment, and DEPLOY DATE=”$(date)” modifies the deployment date and causes the pod to resume.
Scale Command to Change Replicas
Pods in Kubernetes should typically run until a new deployment replaces them. As a result, “restarting” a single Pod is not possible. If your container has any issues, you should try to replace it. The small change in terminology better aligns with Kubernetes Pods’ stateless operating architecture.
You can modify the number of clones of the defective pod by using the scale command. When you set this value to 0, the pod is effectively turned off:
To restart the pod, provide the following command with a number of replicas greater than zero:
Kubernetes eliminates the replicas it no longer requires when the number of replicas is set to zero. Kubernetes produces new replicas after you set a number greater than zero. The names of the new duplicates will be different from the previous ones. Use the query “kubectl get pods” to check the validity of the pods and discover what the names are.
Conclusion
Kubernetes is a very helpful system, but it, like any other system, is not without flaws. When problems arise, you may utilize the three ways outlined above to swiftly and safely get your app back up and running without affecting your users’ experience. After restarting the pods, you will have more time to figure out what’s causing the problem and solve it. Now you have explored the basics of kubectl rollout restart. Additionally, you are now aware of different methods to restart Kubernetes pods.