/plushcap/analysis/spacelift/restart-kubernetes-pods-with-kubectl

How to Restart Kubernetes Pods With Kubectl

What's this blog post about?

In Kubernetes (K8S), a pod is the smallest unit and should run until replaced by a new deployment. There is no direct way to restart a pod; instead, it should be replaced. Pods can have five possible statuses: Pending, Running, Succeeded, Failed, and Unknown. A pod may need to be 'restarted' in various situations, such as when the pod status is stuck in a 'terminating' state or when a pod is erroring and that error cannot be fixed without a restart. To 'restart' a pod with kubectl, several methods can be used: 1. `kubectl rollout restart`: This method is the recommended first port of call as it will not introduce downtime as pods will be functioning. A rollout restart will kill one pod at a time and then new pods will be scaled up. 2. `kubectl scale`: This method will introduce an outage and is not recommended. If downtime is not an issue, this method can be used as it can be a quicker alternative to the kubectl rollout restart method. 3. `kubectl delete pod` or `kubectl delete replicaset`: Each pod can be deleted individually if required. However, where lots of pods are running this is not really a practical approach. 4. `kubectl get pod | kubectl replace`: The pod to be replaced can be retrieved using the kubectl get pod to get the YAML statement of the currently running pod and pass it to the kubectl replace command with the --force flag specified in order to achieve a restart. 5. `kubectl set env`: Setting or changing an environment variable associated with the pod will cause it to restart to take the change. In general, the best approach is to use the kubectl rollout restart method as it will avoid application downtime. A restart will not resolve the problem that caused the pods to have issues in the first place, so further investigation will be required into the root cause.

Company
Spacelift

Date published
Oct. 6, 2022

Author(s)
Jack Roper

Word count
1018

Hacker News points
None found.

Language
English


By Matt Makai. 2021-2024.