Blueprints

Kubernetes Pod Lifecycle Management: Create, Inspect, and Cleanup

Source

yaml
id: k8-pod-lifecycle
namespace: company.team

tasks:
  - id: create
    type: io.kestra.plugin.kubernetes.core.PodCreate
    namespace: monitoring
    delete: false
    metadata:
      labels:
        my-label: my-value
    spec:
      containers:
        - name: unittest
          image: debian:stable-slim
          command:
            - 'bash'
            - '-c'
            - 'for i in {1..10}; do echo $i; sleep 0.1; done'
      restartPolicy: Never

  - id: get
    type: io.kestra.plugin.kubernetes.kubectl.Get
    namespace: monitoring
    resourceType: pods
    fetchType: FETCH

  - id: log
    type: io.kestra.plugin.core.log.Log
    message: "{{ outputs.get.statusItems }}"

  - id: delete
    type: io.kestra.plugin.kubernetes.kubectl.Delete
    namespace: monitoring
    resourceType: pods
    resourcesNames:
      - "{{ outputs.create.metadata.name }}"

pluginDefaults:
  - forced: true
    type: io.kestra.plugin.kubernetes
    values:
      connection:
        masterUrl: "{{ secret('K8S_MASTER_URL') }}"
        oauthToken: "{{ secret('K8S_TOKEN') }}"
        trustCerts: true

About this blueprint

Infrastructure

This flow demonstrates the end-to-end lifecycle of a Kubernetes resource. It automates the process of spinning up an ephemeral pod, retrieving its status, and ensuring the environment is cleaned up by deleting the resource once the task is complete. This pattern is ideal for running one-off jobs, unit tests, or temporary data processing tasks within a cluster.

  • Ephemeral Pod Creation: Deploys a Debian-based pod to the monitoring namespace to execute a specific script or command.
  • Resource Inspection: Utilizes the Get task to fetch detailed status items from the cluster, providing visibility into the pod's state.
  • Logging: Captures and outputs the resource data to Kestra's internal logs for auditing and debugging.
  • Automated Cleanup: Dynamically references the created pod's name to perform a targeted Delete operation, preventing resource clutter.
  • Secure Connection Management: Leverages pluginDefaults to centralize cluster authentication using secrets, ensuring credentials aren't hardcoded.

The flow is highly adaptable; you can swap the resourceType to manage Deployments or Services, or update the spec to use your own custom container images and commands.

Pod Create

Get

Log

Delete

kubernetes

More Related Blueprints

New to Kestra?

Use blueprints to kickstart your first workflows.

Get started with Kestra