Loading…
Warp is now open-source Learn more
Loading…
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.
To restart a Kubernetes pod, you can use the kubectl delete pod command as follows:
$ kubectl delete pod <pod_name>Where:
Kubernetes will delete the pod and automatically recreate a new one with the same configuration.
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering kubectl delete pod in the AI Command Suggestions will prompt a kubectl command that can then quickly be inserted into your shell by doing CMD+ENTER.
To delete all the pods that have failed across all namespaces, you can use the following command:
$ kubectl delete pods --field-selector status.phase=Failed -AWhere:
To delete all the failed pods in a particular namespace, you can use the following command instead:
$ kubectl delete pods --field-selector status.phase=Failed --namespace=<namespace>Where:
If you’ve used labels to organize your pods, you can delete pods with a specific label using the -l flag as follows:
$ kubectl delete pods -l <label>=<value>For example:
$ kubectl delete pods -l environment=testThe above command will delete all the pods with the environment label set to test.
Pod specifications include a restartPolicy field used to define the restart policy associated with a pod. The available values are Always, OnFailure, and Never.
Always 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 Always.
apiVersion: v1
kind: Pod
metadata:
name: nginx-webserver-pod
spec:
restartPolicy: Always
containers:
- name: my-container
image: nginx:latestThe OnFailure 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 OnFailure.
apiVersion: v1
kind: Pod
metadata:
name: busybox-file-task
spec:
restartPolicy: OnFailure
containers:
- name: busybox-container
image: busybox:latest
command: ["cp", "/path/to/source/file", "/path/to/destination/file"]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.
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:
$ kubectl rollout restart deployment/<deployment_name>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 kubectl rollout restart deployment command with the -n flag as follows:
$ kubectl rollout restart deployment/<deployment_name>-n<namespace>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 kubectl rollout restart pod command with the -l flag as follows:
$ kubectl rollout restart pod -l <label>=<value>For example:
$ kubectl rollout restart pod -l environment=developmentThe above command will restart all the pods identified by the environment=development label.
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 kubectl scale deployment command:
$ kubectl scale --replicas=0 deployment <deployment_name>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:
$ kubectl scale --replicas=1 deployment <deployment_name>To restart the pods in a particular namespace, you can use the kubectl scale deployment with the -n flag as follows:
$ kubectl scale --replicas=0 deployment <deployment_name> -n <namespace>
$ kubectl scale --replicas=1 deployment <deployment_name> -n <namespace>To restart pods with a specific label, you can use the kubectl scale command with the --selector flag as follows:
$ kubectl scale --replicas=0 --selector=<label>=<value> deployment
$ kubectl scale --replicas=1 --selector=<label>=<value> deploymentWhere:
For example:
$ kubectl scale --replicas=0 --selector=app=my-app deployment
$ kubectl scale --replicas=1 --selector=app=my-app deploymentUpdating 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.
To update the environment variables of a pod, you can use the kubectl set env command as follows:
$ kubectl set env pod <pod_name> -n<namespace> <env_name> =<env_value>Where:
env\_name variable.For example:
$ kubectl set env pod my-pod -n my-namespace USER_NAME=admin USER_PWD=adminTo update the environment variables of a specific container in a pod, you can use the kubectl set env command as follows:
$ kubectl set env pod <pod_name> -n <namespace> -c <container_name> <env_name>=<env_value>Where:
For example:
$ kubectl set env pod my-pod -n my-namespace -c database DB_NAME=admin DB_PWD=adminLearn how to execute standalone commands and start interactive shell sessions in Pods using the `kubectl exec` command.
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
Learn how to delete Kubernetes deployments and its associated Pods using the kubectl command.
Learn how to delete a pod in Kubernetes using graceful shutdowns, labels, field selectors, and namespaces.
$ kubectl delete pod <pod_name>$ kubectl delete pods --field-selector status.phase=Failed -A$ kubectl delete pods --field-selector status.phase=Failed --namespace=<namespace>$ kubectl delete pods -l <label>=<value>$ kubectl delete pods -l environment=testapiVersion: v1
kind: Pod
metadata:
name: nginx-webserver-pod
spec:
restartPolicy: Always
containers:
- name: my-container
image: nginx:latestapiVersion: v1
kind: Pod
metadata:
name: busybox-file-task
spec:
restartPolicy: OnFailure
containers:
- name: busybox-container
image: busybox:latest
command: ["cp", "/path/to/source/file", "/path/to/destination/file"]$ kubectl rollout restart deployment/<deployment_name>$ kubectl rollout restart deployment/<deployment_name>-n<namespace>$ kubectl rollout restart pod -l <label>=<value>$ kubectl rollout restart pod -l environment=development$ kubectl scale --replicas=0 deployment <deployment_name>$ kubectl scale --replicas=1 deployment <deployment_name>$ kubectl scale --replicas=0 deployment <deployment_name> -n <namespace>
$ kubectl scale --replicas=1 deployment <deployment_name> -n <namespace>$ kubectl scale --replicas=0 --selector=<label>=<value> deployment
$ kubectl scale --replicas=1 --selector=<label>=<value> deployment$ kubectl scale --replicas=0 --selector=app=my-app deployment
$ kubectl scale --replicas=1 --selector=app=my-app deployment$ kubectl set env pod <pod_name> -n<namespace> <env_name> =<env_value>$ kubectl set env pod my-pod -n my-namespace USER_NAME=admin USER_PWD=admin$ kubectl set env pod <pod_name> -n <namespace> -c <container_name> <env_name>=<env_value>$ kubectl set env pod my-pod -n my-namespace -c database DB_NAME=admin DB_PWD=admin