Schedule icon
Commands icon
Docker icon
Script icon
If icon
SlackIncomingWebhook icon
Set icon

PendingWatch - Kubernetes pods stuck Pending watcher

A Kestra blueprint that watches Kubernetes pods stuck in Pending past a threshold and alerts on Slack with the diagnosed scheduling reason, without spamming.

Categories
Infrastructure

How it works

  1. list_pods (io.kestra.plugin.scripts.shell.Commands) dumps the namespace's pods as JSON with kubectl.
  2. find_pending (io.kestra.plugin.scripts.python.Script) keeps pods whose phase is Pending longer than pending_minutes, then reads the PodScheduled=False condition message and translates scheduler jargon into plain causes: no node matching nodeSelector/affinity, insufficient CPU or memory, an unbound PersistentVolumeClaim, or untolerated taints. It compares against the KV state so only newly stuck pods alert.
  3. maybe_alert (io.kestra.plugin.core.flow.If) posts the Slack alert only when there is something new, naming the pod, how long it has been pending, and the diagnosed why.
  4. save_state (io.kestra.plugin.core.kv.Set) writes back the current stuck list. Scheduled pods drop out on their own, so getting stuck again alerts again, and steady-state reruns stay quiet.

What you get

  • A fifteen-minute check for the "pod created but never runs" failure, no agent to install inside the cluster.
  • Slack alerts that do the first minute of debugging for you: the pod, the wait time, and the scheduling reason in plain words.
  • No alert spam: the KV-backed memory remembers who was already reported, and forgets pods that got scheduled.
  • Quiet runs when every pod schedules fine.

Who it's for

Anyone running workloads on Kubernetes who has watched a pod sit in Pending wondering whether it is the node selector, the resources, or the storage. Works on any cluster you can reach with a client certificate: minikube, kind, managed clusters.

Why orchestrate this with Kestra

kubectl get pods shows you the symptom; this flow adds the diagnosis, the memory, and the notification in one scheduled place. Kestra holds the cluster credentials as secrets, keeps every run's findings where you can audit them, and the KV state means the second run does not page you about the same pod again. It completes the pod trilogy with PodPulse (crashed pods) and ImageWatch (pods that cannot pull): this one catches pods that cannot even be placed.

Prerequisites

  • A Kubernetes cluster with pods you can list.
  • A kubeconfig client certificate and key with permission to list pods in the target namespace.
  • Kestra with the Docker task runner available (used for the kubectl and Python steps).
  • A Slack Incoming Webhook URL.

Secrets

Secret What it is
CLIENT_CRT base64-encoded client certificate from your kubeconfig
CLIENT_KEY base64-encoded client key from your kubeconfig
MASTERURL base64-encoded Kubernetes API server URL, reachable from inside the Kestra container
SLACK_WEBHOOK base64-encoded Slack Incoming Webhook URL

In Kestra OSS, secrets come from environment variables prefixed with SECRET_ and must be base64-encoded. If the API URL uses 127.0.0.1 or localhost, replace it with host.docker.internal so the Kestra container can reach it.

Quick start

  1. Start Kestra with the secrets above as SECRET_ environment variables. If minikube restarted since your last run, regenerate SECRET_MASTERURL first: the API port changes on every minikube start.
  2. Import this flow and run it with pending_minutes: 1 for testing: create a stuck pod with kubectl run pending-demo --image=nginx --overrides='{"spec":{"nodeSelector":{"gpu":"true"}}}' and the Slack alert names it with the affinity reason.
  3. Run again: quiet, the pod is already reported.
  4. Delete the pod and run again: quiet, it dropped out of state.

How to extend

  • Watch all namespaces by looping over kubectl get namespaces output in the shell step.
  • Page instead of Slack-alert by swapping the notification task for PagerDuty or Opsgenie.
  • Add event context by also dumping kubectl get events for the stuck pod.
  • Lower pending_minutes for dev namespaces where fast feedback matters.

Inputs

Input Type Default What it does
namespace STRING default the namespace whose pods to watch
pending_minutes INT 10 pods pending longer than this count as stuck
slack_webhook STRING (required) Incoming webhook URL of the Slack channel for alerts

Links

Pitfalls

  • pending_minutes exists because every pod is Pending for a few seconds while scheduling. Set it to 1 only for testing; 10 is sane for real use.
  • The diagnosis reads the scheduler's condition message. If the scheduler gave no reason, the alert says so honestly instead of guessing.
  • The flow is strictly read-only. It never deletes, evicts, or reschedules anything.
  • Minikube's API server port changes on every minikube start. Regenerate SECRET_MASTERURL and restart the Kestra container, or the kubectl step fails to connect.
See How

New to Kestra?

Use blueprints to kickstart your first workflows.