1. Answers
  2. Provision a Kubernetes Job with Pulumi

How do I provision a Kubernetes job with Pulumi?

In this guide, we will provision a Kubernetes job using Pulumi. A Kubernetes job creates one or more pods and ensures that a specified number of them successfully terminate. Jobs are used to run tasks that are expected to terminate, such as batch processes.

Below is a Pulumi program written in TypeScript that provisions a Kubernetes job. This program defines a Kubernetes job resource and specifies its configuration, including the container image to use and the command to run.

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the Kubernetes Job
const job = new k8s.batch.v1.Job("example-job", {
    metadata: {
        name: "example-job",
    },
    spec: {
        template: {
            metadata: {
                name: "example-job",
            },
            spec: {
                containers: [{
                    name: "example",
                    image: "busybox",
                    command: ["echo", "Hello, world!"],
                }],
                restartPolicy: "Never",
            },
        },
        backoffLimit: 4,
    },
});

export const jobName = job.metadata.name;

Key Points

  • Pulumi Program: The program is written in TypeScript and uses the Pulumi Kubernetes provider.
  • Job Resource: The k8s.batch.v1.Job resource is used to define the Kubernetes job.
  • Metadata: The job’s metadata includes a name for the job.
  • Spec: The job specification includes the template for the pod, which contains the container configuration.
  • Container Configuration: The container uses the busybox image and runs a simple command to echo “Hello, world!”.
  • Restart Policy: The restart policy is set to Never to ensure that the job does not restart on failure.
  • Backoff Limit: The backoff limit is set to 4, which specifies the number of retries before the job is considered failed.

Summary

In this guide, we created a Kubernetes job using Pulumi. The job runs a single container that echoes “Hello, world!” and is configured not to restart on failure. This demonstrates how to define and manage Kubernetes resources using Pulumi.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up