PodCreate icon
Get icon
Log icon
Delete icon
kubernetes icon

Kubernetes Pod Lifecycle Management: Create, Inspect, and Cleanup

Orchestrate the full Kubernetes pod lifecycle with Kestra: create an ephemeral pod, fetch its status via kubectl, log results, and delete it automatically.

Categories
Infrastructure

Run the complete lifecycle of a Kubernetes pod as a single declarative Kestra workflow: create an ephemeral pod, query its live status with kubectl, log what the cluster returns, then tear the pod down so nothing lingers. This solves a common pain point with running one-off jobs on Kubernetes, where pods, unit-test containers, and short-lived processing tasks pile up because nothing reliably cleans them up after they finish. By tying creation and deletion together in one flow, you get a repeatable, auditable pattern for ephemeral compute on a cluster.

How it works

  1. The create task (io.kestra.plugin.kubernetes.core.PodCreate) launches a debian:stable-slim pod in the monitoring namespace with a custom label and a restartPolicy: Never, running a short bash command. delete: false keeps the pod alive so later tasks can inspect it.
  2. The get task (io.kestra.plugin.kubernetes.kubectl.Get) lists pods in the namespace with fetchType: FETCH, pulling their status into the flow outputs.
  3. The log task (io.kestra.plugin.core.log.Log) prints {{ outputs.get.statusItems }} to Kestra's logs for auditing and debugging.
  4. The delete task (io.kestra.plugin.kubernetes.kubectl.Delete) removes the pod by referencing {{ outputs.create.metadata.name }}, so cleanup always targets the exact pod that was created.

Cluster authentication is centralized in pluginDefaults for io.kestra.plugin.kubernetes, injecting masterUrl and oauthToken from secrets with trustCerts: true.

What you get

  • A repeatable create, inspect, cleanup loop for ephemeral pods
  • Live pod status surfaced into flow outputs and logs
  • Targeted deletion keyed to the created pod's name, no leftover resources
  • Centralized, secret-backed cluster credentials with no hardcoded tokens

Who it's for

  • Platform and DevOps engineers running one-off or scheduled jobs on Kubernetes
  • Data engineers spinning up temporary processing containers
  • Teams that need auditable, automated cleanup of ephemeral workloads

Why orchestrate this with Kestra

Kubernetes itself has no built-in scheduler that creates a pod, reads its status, and guarantees deletion as one accountable unit. CronJobs run pods but leave orchestration, status capture, and conditional cleanup to you. With Kestra you get event and schedule triggers, automatic retries, full execution lineage across every task, and a declarative YAML definition that lives in version control. The dynamic {{ outputs.create.metadata.name }} reference ties cleanup to the precise resource created, closing the gap the cluster's own tooling leaves open.

Prerequisites

  • A reachable Kubernetes cluster and API server
  • A monitoring namespace (or change it to one you own)
  • Permissions to create, get, and delete pods in that namespace

Secrets

  • K8S_MASTER_URL: the Kubernetes API server URL
  • K8S_TOKEN: the OAuth token used to authenticate against the cluster

Quick start

  1. Add the K8S_MASTER_URL and K8S_TOKEN secrets to your Kestra instance.
  2. Confirm the target namespace exists and your token has pod permissions.
  3. Add the flow and run it.
  4. Open the log task output to see the fetched pod status, and confirm the pod is gone after delete.

How to extend

  • Swap resourceType in Get and Delete to manage Deployments, Services, or ConfigMaps.
  • Replace the container image and command in create to run your own job.
  • Add a Schedule or event trigger to run the lifecycle on a cadence.
  • Add retries or an errors branch to handle transient cluster failures.

Links

Share this Blueprint
See How

New to Kestra?

Use blueprints to kickstart your first workflows.