Webhook icon
Script icon
Labels icon
Switch icon
Commands icon
Docker icon
Query icon
If icon
SlackIncomingWebhook icon
Exit icon
Pause icon
Upload icon
Download icon
Set icon
Loop icon
Patch icon
Return icon

Kubernetes Service Decommissioning with Traffic Proof, Soft-Off Period, Verified Snapshot, Approved Deletion and Restore

Retire Kubernetes services safely with Kestra. Prometheus traffic proof, a verified S3 snapshot, a quiet period with auto-restore, and approved deletion.

Categories
Infrastructure

Services are created with care and retired carelessly. Someone deletes the Deployment and leaves its Service, Ingress, ConfigMaps and CronJobs behind, or nobody dares delete anything because nobody can prove the service is unused, so it keeps costing money and stays unpatched. When it is finally removed, a caller nobody knew about breaks and there is no copy to bring it back. This blueprint gives platform teams one safe, audited path for retiring a service, from a portal webhook or the Execute form.

How it works

  1. Find everything. inventory lists every object carrying the service's label (app.kubernetes.io/name by default): Deployments, StatefulSets, CronJobs, Services, Ingresses, ConfigMaps, HorizontalPodAutoscalers and PersistentVolumeClaims.
  2. Prove it is idle. traffic_before asks Prometheus how many requests reached the service in the last lookback_days. If there were any, the run stops with a Slack message and changes nothing. If Prometheus returns no series at all, the run fails instead of assuming zero traffic.
  3. First approval. The owner sees the object list and the traffic evidence in Slack and approves the soft off in a Pause form.
  4. Snapshot before touching anything. The objects are cleaned so they can be applied again (no status, uid, resourceVersion or cluster IP), uploaded to S3, then downloaded again and checked against the inventory.
  5. Soft off. Deployments and StatefulSets are scaled to zero and CronJobs suspended with kubectl.Patch. The original replica counts and the snapshot key are kept in the KV store.
  6. Quiet period. The execution waits quiet_days, then checks Prometheus again. If anything called the service while it was off, it is scaled back up, its CronJobs resume, and the run ends with a warning naming the request count.
  7. Second approval. Delete, restore, or keep it scaled down for now.
  8. Delete and verify. Objects are deleted by label, PersistentVolumeClaims are kept unless delete_pvcs is set, and verify_gone lists the namespace again and fails if anything unexpected is left. A KV record keeps who, why, when and where the snapshot is.
  9. Restore mode re-applies the snapshot (from the input or the KV record) and waits for the workloads to be ready.

One execution covers the whole decommission, so its history is the audit trail. Each run is labelled with service and k8s-namespace.

Prerequisites

  • A Kubernetes cluster, plus Prometheus scraping your ingress controller. The default query is for ingress-nginx scraped by the Prometheus Operator (kube-prometheus-stack), whose labels arrive as exported_namespace and exported_service. Prometheus retention must cover lookback_days.
  • An S3 bucket (AWS S3 or any S3-compatible store such as MinIO) for snapshots, set in vars.snapshot_bucket, vars.s3_endpoint and vars.s3_region.
  • Docker on the Kestra worker for the bitnami/kubectl script tasks.
  • A ServiceAccount whose ClusterRole (or per-namespace Role) allows get, list, create, patch and delete on deployments, statefulsets, cronjobs, services, ingresses, configmaps, horizontalpodautoscalers and persistentvolumeclaims, watch on deployments and statefulsets, and get, list, watch on replicasets (for the rollout check). It needs no access to Secrets.

Secrets:

  • K8S_MASTER_URL: the Kubernetes API URL.
  • K8S_TOKEN: the ServiceAccount token.
  • K8S_CA_CERT: the cluster CA certificate (PEM), base64-encoded.
  • PROMETHEUS_URL: the Prometheus base URL.
  • SNAPSHOT_S3_ACCESS_KEY_ID and SNAPSHOT_S3_SECRET_KEY: credentials that can read and write the snapshot bucket.
  • DECOMMISSION_WEBHOOK_KEY: the key in the webhook URL.
  • SLACK_WEBHOOK_URL: a Slack incoming webhook for approvals, results and failures.

Inputs

  • mode (SELECT, default decommission): decommission or restore.
  • namespace and service (STRING): the namespace, and the value of the selector label. A webhook body with the same field names overrides them.
  • reason (STRING, optional): shown in the approval message and stored in the record.
  • lookback_days (INT, default 14): days of zero traffic required.
  • quiet_days (INT, default 7): days scaled to zero before deletion can be approved.
  • delete_pvcs (BOOL, default false): also delete volume claims. Their data is lost.
  • snapshot_key (STRING, optional): the snapshot to restore. Empty uses the one recorded at decommission.
  • dry_run (BOOL, default false): report the objects and traffic evidence without changing anything.

Quick start

  1. Create the ServiceAccount and the secrets above, and point the vars at your bucket.
  2. Run a dry run for a service you believe is unused. Slack lists every object and the request count, and nothing changes.
  3. Run it for real with a short quiet_days on a test service, approve the soft off, and watch the replicas go to zero.
  4. Send a request through the ingress during the quiet period to see the automatic restore.
  5. Connect the webhook to your portal: POST /api/v1/main/executions/webhook/company.platform/kubernetes-service-decommissioning/<key> with {"namespace": "...", "service": "...", "reason": "...", "requested_by": "..."}.

Outputs

  • objects (INT): how many objects belong to the service.
  • snapshot (STRING): the S3 key of the snapshot.
  • KV decommission-<namespace>-<service>: state (scaled-down, restored-after-traffic, deleted, restored), snapshot key, replica counts, reason, requester and execution.

Common pitfalls and how to adapt it

  • Another ingress or mesh. Change vars.traffic_query; keep $NAMESPACE, $SERVICE and $WINDOW. Keep the second half of the default, which adds series that start inside the window: after a service is scaled to zero its first 503s appear as a new series, and increase() alone counts them as 0.
  • Labels. Objects without the selector label are not found and not deleted. Change vars.selector_label if you use another label, such as app.
  • Secrets are not touched. They are left out of vars.resource_types so they never end up in a snapshot. Remove them separately, or add secrets only with an encrypted, access-controlled bucket.
  • Volume data. The snapshot holds object definitions, not volume contents. PVCs are kept by default; back up their data before setting delete_pvcs.
  • Restore after deletion recreates the objects with their original replica counts. A deleted PVC comes back empty.
  • Approvals time out. The first approval cancels after 3 days, the deletion approval after 7, leaving the service in its current state.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.