<p>By default, if a pod is unable to be scheduled because of a node affinity constraint and the specified node is not available, the pod will remain in a &quot;Pending&quot; state until the node becomes available or the node affinity constraint is removed.</p>
<p>One option to address this issue is to use a pod disruption budget (PDB) to specify the minimum number of available nodes that must be present in the cluster at all times. This can help ensure that there are always enough available nodes to satisfy the node affinity constraints of your pods.</p>
<p>For example, you can use a PDB to specify that at least one node must always be available in the cluster by setting the minAvailable field to 1:</p>
<pre><code>apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: my-pdb
spec:
  minAvailable: 1
  selector:
    matchLabels:
      app: my-app
</code></pre>
<p>This PDB would prevent any nodes from being deleted if there are pods with node affinity constraints that are scheduled on those nodes.</p>
<p>Alternatively, you can use a deployment or replica set to manage the lifecycle of your pods and specify a node affinity constraint for the pods in the deployment or replica set. If a node becomes unavailable, the deployment or replica set will automatically reschedule the pods on another available node. This can help ensure that your pods are always running and avoid the &quot;Pending&quot; state.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - node1
      containers:
      - name: my-container
        image: my-image
</code></pre>
<p>I hope this helps!</p>

CC BY-SA 4.0
2023-01-05T06:41:03.137Z
2023-01-05T06:41:03.137Z
Rami M. Nassar
<p>You may delete all pending pods after a while
This lists all pending pods</p>
<pre><code>kubectl get pods --all-namespaces --field-selector=status.phase=Pending
</code></pre>
<p>This will delete all pending pods that are older than 1 minute</p>
<pre><code>kubectl get pods --all-namespaces --field-selector=status.phase=Pending -o json | jq '.items[] | select((now - (.metadata.creationTimestamp | fromdateiso8601)) &gt; 60) | .metadata.name' | xargs -I{} kubectl delete pod {} --force --grace-period=0
</code></pre>

CC BY-SA 4.0
2023-04-05T06:16:53.240Z
2023-04-05T06:16:53.240Z
Sam Aslanpour
<p>The &quot;pending&quot; pods won't get deleted automatically. Pods will get deleted only when the pod object gets deleted.
If you want to delete the pods when there are no nodes present, you may need to write a custom solution (something like a cron job which keeps checking the pending pods and deleting them). Also, if the pods are being created by other objects such as statefulsets/deployments etc, you need to delete the object which is responsible for creating the pods. (You can also just scale down the size to 0 in case of statefulsets/deployments)</p>
<p>PS: The pods are in pending state is the default behaviour and whenever a new node is available, the workload will be getting scheduled. This is one of the features of kubernetes which helps in auto healing capability.</p>

CC BY-SA 4.0
2023-01-04T12:58:23.773Z
2023-01-04T12:58:23.773Z
Samit
<p>I have scenario where PODs are ending up into “Pending” state.</p>
<p>The reason is that when I launch a POD, I launch it with node affinity because the requirement is that I want to run the POD on specific node.</p>
<p>Now in case of auto-scanning up/down. Sometime the node gets deleted but the POD created to be scheduled on the Node end-up into “Pending” state.</p>
<p>Is there a way that “Pending” pods gets existed/deleted by themself if the Node is not available itself?</p>

<kubernetes>
How to exit "Pending" status pods in K8s?
balaji
# How to exit "Pending" status pods in K8s?

(Asked by: username_0 on 2023-01-03)

I have scenario where PODs are ending up into “Pending” state.

The reason is that when I launch a POD, I launch it with node affinity because the requirement is that I want to run the POD on specific node.

Now in case of auto-scanning up/down. Sometime the node gets deleted but the POD created to be scheduled on the Node end-up into “Pending” state.

Is there a way that “Pending” pods gets existed/deleted by themself if the Node is not available itself?

**Tags:** kubernetes


---

## 3 answers

### Answer by username_1 on 2023-01-05. Score: 0

By default, if a pod is unable to be scheduled because of a node affinity constraint and the specified node is not available, the pod will remain in a "Pending" state until the node becomes available or the node affinity constraint is removed.
One option to address this issue is to use a pod disruption budget (PDB) to specify the minimum number of available nodes that must be present in the cluster at all times. This can help ensure that there are always enough available nodes to satisfy the node affinity constraints of your pods.
For example, you can use a PDB to specify that at least one node must always be available in the cluster by setting the minAvailable field to 1:
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: my-pdb
spec:
  minAvailable: 1
  selector:
    matchLabels:
      app: my-app

This PDB would prevent any nodes from being deleted if there are pods with node affinity constraints that are scheduled on those nodes.
Alternatively, you can use a deployment or replica set to manage the lifecycle of your pods and specify a node affinity constraint for the pods in the deployment or replica set. If a node becomes unavailable, the deployment or replica set will automatically reschedule the pods on another available node. This can help ensure that your pods are always running and avoid the "Pending" state.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - node1
      containers:
      - name: my-container
        image: my-image

I hope this helps!


---

### Answer by username_2 on 2023-04-05. Score: 0

You may delete all pending pods after a while
This lists all pending pods
kubectl get pods --all-namespaces --field-selector=status.phase=Pending

This will delete all pending pods that are older than 1 minute
kubectl get pods --all-namespaces --field-selector=status.phase=Pending -o json | jq '.items[] | select((now - (.metadata.creationTimestamp | fromdateiso8601)) > 60) | .metadata.name' | xargs -I{} kubectl delete pod {} --force --grace-period=0


---

### Answer by username_3 on 2023-01-04. Score: 0

The "pending" pods won't get deleted automatically. Pods will get deleted only when the pod object gets deleted.
If you want to delete the pods when there are no nodes present, you may need to write a custom solution (something like a cron job which keeps checking the pending pods and deleting them). Also, if the pods are being created by other objects such as statefulsets/deployments etc, you need to delete the object which is responsible for creating the pods. (You can also just scale down the size to 0 in case of statefulsets/deployments)

PS: The pods are in pending state is the default behaviour and whenever a new node is available, the workload will be getting scheduled. This is one of the features of kubernetes which helps in auto healing capability.


---

