Sep 13th, ‘23/6 min read

How to restart Kubernetes Pods with kubectl

A simple reckoner on how to restart a Kubernetes pod with kubectl

How to restart Kubernetes Pods with kubectl

In Kubernetes, Pods are the smallest units of computing that you can create and manage.

A Pod represents a single instance of a running process in a cluster. A Pod contains one or more containers that share the same storage and network resources. Pods are relatively ephemeral (temporary) rather than durable components and have a defined lifecycle like containers.

The lifecycle of a Pod starts in the Pending phase and then moves through Running if at least one of its primary containers starts OK. After that, it moves to either the Succeeded or Failed phases depending on whether any container in the Pod terminated in failure.

While a Pod goes through its lifecycle, there are several scenarios where you might want to restart it to ensure your cluster is at its desired state.

This article will discuss various scenarios where you might want to restart a Kubernetes Pod and walk you through methods to restart Pods with kubectl.

kubectl exec: Commands, Examples, and Best Practices | Last9
Learn essential commands, examples, and best practices for using kubectl exec to troubleshoot and manage your Kubernetes applications.

Understanding Kubernetes Pods and Their Lifecycle

Before diving into the restart process, let's briefly review what pods are and their lifecycle in a Kubernetes cluster:

  1. Pending: The pod has been accepted by the cluster but containers are not yet running.
  2. Running: At least one container in the pod is running.
  3. Succeeded: All containers in the pod have terminated successfully.
  4. Failed: All containers in the pod have terminated, and at least one container has failed.
  5. Unknown: The state of the pod cannot be determined.

The kubelet, running on each node in the cluster, is responsible for managing the pod lifecycle, including restarting pods when necessary based on the defined restart policy in the pod spec.

Why Restart Kubernetes Pods?

There are several use cases for restarting pods:

  1. Applying updated configuration or config changes
  2. Updating dependencies or the container image itself
  3. Troubleshooting and debugging issues
  4. Clearing corrupted internal states
  5. Addressing resource constraints (CPU, memory, Out of Memory errors)
  6. Rolling updates to deployments or ReplicaSets
  7. Recovering from a terminating state

Methods to Restart Kubernetes Pods Using kubectl

1. Restarting Pods by Changing the Number of Replicas

This method involves scaling down the deployment to zero replicas and then scaling it back up:

kubectl scale deployment <deployment_name> --replicas=0
kubectl scale deployment <deployment_name> --replicas=<desired_number>

The kubectl scale command allows you to change the number of replicas for a deployment. By setting it to zero, you can terminate all running pods. Then, by scaling back up, you create new pods with the latest configuration.

2. Using Rolling Update for Zero Downtime

To perform a rolling update without downtime, use the following command:

kubectl rollout restart deployment <deployment_name>

The kubectl rollout restart command updates the pod template with a unique label, triggering a rolling update. This method ensures that old pods are gradually replaced by new ones, maintaining service availability.

3. Updating Environment Variables

Modifying environment variables can trigger a pod restart:

kubectl set env deployment/<deployment_name> RESTART_DATE="$(date)"

This command adds or updates an environment variable in the deployment's pod template, causing Kubernetes to create new pods with the updated configuration.

4. Deleting Specific Pods

For troubleshooting or when dealing with a single pod, you might need to delete and restart it:

kubectl delete pod <pod_name>

When you delete a pod, the ReplicaSet controller notices that the desired number of replicas is not met and creates a new pod to replace the deleted one.

5. Restarting StatefulSet Pods

For StatefulSets, you can use:

kubectl rollout restart statefulset <statefulset_name>

This command works similarly to restarting a deployment but maintains the identity and storage of the StatefulSet pods.

Using Kubectl Logs: Guide to Viewing Kubernetes Pod Logs | Last9
Learn how to effectively use kubectl logs to view and analyze Kubernetes pod logs. Master advanced techniques, troubleshoot issues, and optimize your K8s deployments.

Best Practices for Restarting Kubernetes Pods

  1. Use Rolling Updates: Whenever possible, use rolling updates to minimize downtime and ensure a smooth transition.
  2. Monitor Metrics: Keep an eye on CPU and memory usage before and after restarts using Kubernetes' built-in metrics server or third-party monitoring solutions.
  3. Automate Restarts: Use tools like Helm or Kustomize to automate configuration changes and restarts, reducing manual errors and improving consistency.
  4. Version Control: Store your Kubernetes YAML files, including pod specs and deployment templates, in a version control system like Git for easy tracking of changes.
  5. Debug Carefully: When debugging deployments with multiple replicas, use labels and selectors to target specific pods for investigation.
  6. Optimize Resources: Regularly review and optimize your Docker and Kubernetes resource allocations to reduce costs and prevent Out of Memory (OOM) errors.
  7. Use Readiness and Liveness Probes: Implement proper readiness and liveness probes in your pod spec to ensure Kubernetes can accurately determine the health of your pods after restarts.
  8. Consider Node Constraints: Be aware of node selector and affinity rules that might affect where restarted pods are scheduled.

Handling Special Cases

Restarting the Nginx Ingress Controller

To restart an Nginx ingress controller, which is often deployed as a DaemonSet or Deployment, you can use:

kubectl rollout restart deployment <ingress_controller_deployment_name> -n <namespace>

Dealing with Pods Stuck in Terminating State

If a pod is stuck in a terminating state, you may need to force delete it:

kubectl delete pod <pod_name> --grace-period=0 --force

Be cautious when using this command, as it bypasses normal pod termination processes.

Differences from Docker Restart

Unlike the docker restart command, Kubernetes doesn't have a direct "restart" command for pods. Instead, it focuses on maintaining the desired state of the cluster.

When you need to restart a pod, you're essentially telling Kubernetes to replace the existing pod with a new one that matches the updated specification.

Conclusion

Restarting Kubernetes pods is a common task in the lifecycle of containerized applications. This article discussed various scenarios where you might want to restart Kubernetes Pods and walked you through the methods with kubectl.

There is more to learn about kubectl. To learn more, check out the kubectl commands reference and use kubectl logs to debug systems.

Want to know more about Last9? Check out last9.io; we’re building a control plane for software monitoring that makes running systems at scale fun and embarrassingly easy. ✌️

FAQs

Q: How to restart the pod in kubectl?

A: Use kubectl rollout restart deployment <deployment_name> for zero-downtime restarts.

Q: How do I restart my ingress pod in Kubernetes?

A: Run kubectl rollout restart deployment <ingress_deployment_name> -n <namespace>.

Q: How do you delete and restart a pod?

A: Use kubectl delete pod <pod_name> to delete, Kubernetes will automatically restart it.

Q: How do I restart Kubernetes components?

A: Restart methods vary by component. For core components, often restarting the kubelet service is necessary.

Q: Why Restart Kubernetes Pods?

A: To apply changes, update dependencies, troubleshoot issues, or clear corrupted states.

Q: How can you debug Kubernetes deployments with multiple replicas?

A: Use labels to target specific pods and analyze logs with kubectl logs.

Q: Why rely on kubelet to restart it?

A: Kubelet manages pod lifecycles and ensures they maintain the desired state.

Q: What is the lifecycle of a Pod in Kubernetes?

A: Pending, Running, Succeeded, Failed, and Unknown.

Q: What is the command to force a pod to restart in Kubernetes using kubectl?

A: Use kubectl rollout restart deployment <deployment_name>.

Q: What command is used to restart a specific pod in Kubernetes using kubectl?

A: Delete the pod with kubectl delete pod <pod_name> to force a restart.

Q: How can I force a Kubernetes pod to restart using kubectl?

A: Use kubectl delete pod <pod_name> or update an environment variable.

Q: How can I force a pod to restart in a Kubernetes deployment using kubectl?

A: Use kubectl rollout restart deployment <deployment_name>.

Q: What is the command to restart a specific pod using kubectl?

A: Use kubectl delete pod <pod_name> to force Kubernetes to create a new pod.

Contents


Newsletter

Stay updated on the latest from Last9.

Authors

Anjali Udasi

Helping to make the tech a little less intimidating. I love breaking down complex concepts into easy-to-understand terms.

Handcrafted Related Posts