Restart Kubernetes Pods with kubectl
A Kubernetes pod serves as an isolated environment for running a single process, representing the smallest operational unit within the Kubernetes ecosystem. Unlike other resources, such as Deployments, which are replaced during updates, pods have a more persistent nature. Consequently, Kubernetes does not provide a dedicated `kubectl restart pod` command. Instead, various methods, both automatic and manual, can be employed to restart pods effectively.
The short answer
To restart a Kubernetes pod, you can use the [.inline-code]kubectl delete pod[.inline-code] command as follows:
Where:
- [.inline-code]pod_name[.inline-code] is the name of the pod to delete
Kubernetes will delete the pod and automatically recreate a new one with the same configuration.
[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering [.inline-code]kubectl delete pod[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]kubectl[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].
[#delete-multiple-pods] Deleting multiple pods [#delete-multiple-pods]
To delete all the pods that have failed across all namespaces, you can use the following command:
Where:
- The [.inline-code]--field-selector[.inline-code] flag with [.inline-code]status.phase=Failed[.inline-code] specifies failed pods.
- The [.inline-code]-A[.inline-code] flag (short for [.inline-code]--all-namespaces[.inline-code]) specifies all the namespaces in the cluster.
Deleting pods in a namespace
To delete all the failed pods in a particular namespace, you can use the following command instead:
Where:
- [.inline-code]namespace[.inline-code] is the name of the namespace from which to delete the failed pods.
[#delete-pods-by-label] Deleting pods by label [#delete-pods-by-label]
If you’ve used labels to organize your pods, you can delete pods with a specific label using the [.inline-code]-l[.inline-code] flag as follows:
For example:
The above command will delete all the pods with the [.inline-code]environment[.inline-code] label set to [.inline-code]test[.inline-code].
[#define-a-restart-policy] Defining a restart policy [#define-a-restart-policy]
Pod specifications include a [.inline-code]restartPolicy[.inline-code] field used to define the restart policy associated with a pod. The available values are [.inline-code]Always[.inline-code], [.inline-code]OnFailure[.inline-code], and [.inline-code]Never[.inline-code].
[#always-restart-policy]The [.inline-code]Always[.inline-code] restart policy[#always-restart-policy]
[.inline-code]Always[.inline-code] is the default restart policy. It ensures that Kubernetes will automatically restart a container within the pod whenever it terminates, whether it exits successfully or fails. As in the example below, a web server is a common use case for a restart policy of [.inline-code]Always[.inline-code].
[#the-on-failure-restart-policy] The [.inline-code]OnFailure[.inline-code] restart policy [#the-on-failure-restart-policy]
The [.inline-code]OnFailure[.inline-code] restart policy is used for pods whose containers execute tasks on a schedule or only once. This ensures that the containers in the pod are replaced automatically in the event of a failure and prevents them from restarting unnecessarily, thus consuming resources.
The example below of a container that copies a file from one directory to another is a use case for setting the restart policy to [.inline-code]OnFailure[.inline-code].
[#restart-with-rolling-updates] Restarting pods using rolling updates [#restart-with-rolling-updates]
Aside from choosing the appropriate restart policy, there are many scenarios where it becomes necessary to restart a pod manually. The most recommended method for a manual restart is the rolling restart.
A rollout is a process used to ensure that updates are rolled out with minimal disruption of the availability of the application by terminating pods gracefully and recreating them one at a time. This method is available in Kubernetes v1.15 and later.
[#rolling-restart-of-a-deployment] Performing the rolling restart of a Deployment [#rolling-restart-of-a-deployment]
A Deployment controller can be defined to declare the desired application state, including details such as the container image, number of replicas, and update strategy. A common use case is managing the deployment and scaling of a web application.
If a change is made to the Deployment manifest, all the pods in the Deployment can be restarted with the command below:
[#rolling-restart-using-namespaces] Performing a rolling restart using namespaces [#rolling-restart-using-namespaces]
Namespaces are logical, isolated environments within a Kubernetes cluster that allow you to group and manage Kubernetes resources separately.
To restart all the Deployments located in a specified namespace, you can use the following [.inline-code]kubectl rollout restart deployment[.inline-code] command with the [.inline-code]-n[.inline-code] flag as follows:
[#rolling-restart-using-labels] Performing a rolling restart using labels [#rolling-restart-using-labels]
Labels are key-value pairs attached to Kubernetes objects used for organizing resources based on specific criteria.
To restart all the pods based on a specific label, you can use the [.inline-code]kubectl rollout restart pod[.inline-code] command with the [.inline-code]-l[.inline-code] flag as follows:
For example:
The above command will restart all the pods identified by the [.inline-code]environment=development[.inline-code] label.
[#scale-down-deployment-replicas] Scaling down deployment replicas [#scale-down-deployment-replicas]
Scaling pods down is another method to restart pods in a controlled manner with graceful termination.
To restart a pod by scaling deployment replicas to 0, you can use the following [.inline-code]kubectl scale deployment[.inline-code] command:
Which will gracefully terminate all the pods in the specified deployment.
You can then recreate them by setting the number of replicas to 1 or more as follows:
[#scale-down-using-namespaces] Scaling down Deployments in a namespace [#scale-down-using-namespaces]
To restart the pods in a particular namespace, you can use the [.inline-code]kubectl scale deployment[.inline-code] with the [.inline-code]-n[.inline-code] flag as follows:
[#scale-down-using-labels] Scaling down Deployment using labels [#scale-down-using-labels]
To restart pods with a specific label, you can use the [.inline-code]kubectl scale[.inline-code] command with the [.inline-code]--selector[.inline-code] flag as follows:
Where:
- [.inline-code]label[.inline-code] is the name of the label selector.
- [.inline-code]value[.inline-code] is the value associated with this label.
For example:
[#update-environment-variables] Updating environment variables [#update-environment-variables]
Updating the environment variables of a pod, or container in a pod, will gracefully terminate the pod, change the specified environment variables, and recreate the pod.
[#update-pods-environment-variables] Updating the environment variables of a pod [#update-pods-environment-variables]
To update the environment variables of a pod, you can use the [.inline-code]kubectl set env[.inline-code] command as follows:
Where:
- [.inline-code]pod_name[.inline-code] is the name of the pod you want to update.
- [.inline-code]namespace[.inline-code] is the name of the namespace the pod runs in.
- [.inline-code]env_name[.inline-code] is the name of the environment variable you want to add or update.
- [.inline-code]env_value[.inline-code] is the value of the `env_name` variable.
For example:
[#update-containers-environment-variables]Updating the environment variables of a container [#update-containers-environment-variables]
To update the environment variables of a specific container in a pod, you can use the [.inline-code]kubectl set env[.inline-code] command as follows:
Where:
- [.inline-code]pod_name[.inline-code] is the name of the pod you want to update.
- [.inline-code]namespace[.inline-code] is the name of the namespace the pod runs in.
- [.inline-code]container_name[.inline-code] is the name of the container in the pod.
- [.inline-code]env_name[.inline-code] is the name of the environment variable you want to add or update.
- [.inline-code]env_value[.inline-code] is the value of the [.inline-code]env_name[.inline-code] variable.
For example: