Schedule icon
Webhook icon
Loop icon
Get icon
If icon
Request icon
Get icon
Script icon
Process icon
Set icon
Patch icon
LoopUntil icon
Log icon
SlackIncomingWebhook icon

Kubernetes Failed Rollout Auto-Rollback to Last Known-Good Revision

Roll failed Kubernetes Deployment rollouts back to the last known-good ReplicaSet, with opt-in, cooldown, a race-safe JSON patch and rollout verification.

Categories
Infrastructure

A bad image tag or a crashing release leaves a Deployment stuck: Kubernetes marks the rollout ProgressDeadlineExceeded and then waits for a human. With the Recreate strategy the old pods are already gone, so that wait is an outage. This blueprint finds those failed rollouts and undoes them the way kubectl rollout undo does, with three things rollout undo does not have: it remembers which revision was last healthy, it refuses to roll the same Deployment back twice in a cooldown window, and it watches the rollout until the restored pods are available.

How it works

  1. every_five_minutes (Schedule) sweeps the guarded namespaces; after_deploy (Webhook) lets a CD pipeline trigger a sweep right after it ships. Both are disabled until a dry run looks right. Flow-level concurrency: limit 1 keeps sweeps from overlapping.
  2. detect (core.flow.Loop over namespaces) reads every Deployment with kubernetes.kubectl.Get and the namespace's guard state with core.kv.Get. Only when a status contains ProgressDeadlineExceeded does it list the ReplicaSets through core.http.Request against the Kubernetes API, because kubectl.Get returns metadata and status but not the pod templates a rollback needs.
  3. plan (scripts.python.Script, Process runner) records the current revision of every fully healthy Deployment as its known-good revision. For each failed rollout it picks the known-good ReplicaSet (or the previous revision when none is recorded), and escalates instead when the Deployment is not opted in, was already rolled back inside the cooldown, or has nothing older to go back to. Each rollback becomes a JSON patch that first tests the revision annotation, so it aborts if someone deployed again in between, then replaces spec.template and stamps a rollout-guard/last-rollback annotation.
  4. save_state writes the KV state before any patch is sent, so a rollback that later fails verification is still counted and cannot loop.
  5. apply (core.flow.If, live mode only) runs rollback (Loop): kubectl.Patch with JSON_PATCH, then wait_for_rollout (core.flow.LoopUntil) polls kubectl.Get every 10 seconds until the controller has observed the patch and reports NewReplicaSetAvailable, failing after verify_timeout_minutes.
  6. notify posts one Slack message when a rollback is planned or done, or a new escalation appears; repeated sweeps over the same failure stay quiet. errors alerts with the rollbacks that were attempted if a patch is refused or a rollout never converges.

What you get

  • Failed rollouts reverted in one sweep and verified healthy, with before and after images in the digest.
  • Rollback to the last revision that was really healthy, so two bad deploys in a row do not land you on the other bad one.
  • Safety rails: dry run by default, a per-Deployment opt-in annotation, one automatic rollback per Deployment per cooldown, a race-safe patch, no overlapping runs.
  • An audit trail on the Deployment itself (rollout-guard/last-rollback) and flow outputs (failed_rollouts, rollbacks, escalations, applied).

Who it's for

  • Platform and SRE teams who own shared clusters but not every team's deploy pipeline.
  • Teams shipping with plain kubectl apply, Kustomize or Helm templates who want automatic rollback without adopting Argo Rollouts or Flagger.
  • On-call engineers who would rather read "rolled back and healthy" than be paged to type rollout undo at 3 a.m.

Why orchestrate this with Kestra

A cron job running kubectl rollout undo forgets which version was good, rolls back again and again when the old version is broken too, and says nothing about whether the cluster recovered. Here detection, decision, the state that prevents flapping, the patch, the verification loop and the alert are one execution: every decision is logged with its reason, the KV store carries known-good revisions and cooldowns between runs, LoopUntil turns "did it recover" into a step that can fail, retries absorb API hiccups, and the same flow runs from a schedule, a webhook or the UI.

Prerequisites

  • A Kubernetes cluster reachable from the Kestra worker and a ServiceAccount with get, list, patch on deployments and get, list on replicasets (both in the apps group) in the guarded namespaces.
  • progressDeadlineSeconds set on your Deployments to how long a healthy rollout may take (Kubernetes defaults to 600).
  • Python 3 on the worker (the default Kestra image has it), or switch plan to the Docker task runner.
  • A Slack incoming webhook.

Secrets

  • K8S_MASTER_URL: Kubernetes API server URL.
  • K8S_TOKEN: ServiceAccount token with the permissions above.
  • SLACK_WEBHOOK_URL: Slack incoming webhook for the digest and failure alerts.
  • ROLLOUT_GUARD_WEBHOOK_KEY: long random key for the after_deploy webhook URL.

Quick start

  1. Create the secrets. Bind a ClusterRole with the verbs above through a RoleBinding in each guarded namespace and mint a token with kubectl create token.
  2. Annotate the Deployments the guard may roll back with rollout-guard/enabled: "true" and set namespaces.
  3. Execute once while everything is healthy: the KV store now holds a known-good revision per Deployment.
  4. Ship a broken image to a test Deployment, wait for progressDeadlineSeconds, execute with dry_run=true and read the plan in the Slack digest and the rollbacks output.
  5. Execute with dry_run=false and confirm the Deployment is back on the old image and carries the rollout-guard/last-rollback annotation.
  6. Enable every_five_minutes, and after_deploy if your pipeline can call it.

How to extend

  • Treat pods in CrashLoopBackOff on the new ReplicaSet as failed before the deadline expires, by adding a kubectl.Get on pods to detect.
  • Gate production namespaces behind a core.flow.Pause approval before apply.
  • Open an incident or a GitHub issue for every escalation, or revert the commit that introduced the bad image.
  • Cover StatefulSets by reading controllerrevisions instead of ReplicaSets.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.