Using kubernetes pulumi.com with cronjobber.hidde.co
TypeScriptTo set up a Kubernetes cron job with Pulumi, you'll first need to have a Kubernetes cluster and
kubectl
configured to communicate with your cluster. Once that's in place, you can use Pulumi's Kubernetes package to define resources declaratively in TypeScript.The cron job we'll create will be scheduled using CronJobber, which is an alternative to the default CronJob resource in Kubernetes that supports time zones. This is valuable for jobs that need to be scheduled according to local times taking daylight saving changes into account.
Here's a Pulumi program in TypeScript that sets up a cron job in Kubernetes using CronJobber:
- First, we need to install the
cronjobber.hidde.co
Helm chart, which provides the time zone aware cron job capabilities on our cluster. - Next, we define a
CronJob
resource itself using the extended kind from thecronjobber.hidde.co
API. - We specify the schedule, container to run, and the time zone.
Below is the full program that creates a time zone aware cron job to run at a fixed schedule:
import * as kubernetes from "@pulumi/kubernetes"; // Create a Kubernetes provider instance using the current context from kubectl. const provider = new kubernetes.Provider("provider", { kubeconfig: kubectl.config() }); // Deploying the cronjobber Helm chart const cronjobberChart = new kubernetes.helm.v3.Chart("cronjobber", { chart: "cronjobber", version: "1.0.0", // Use the version you require namespace: "default", // Adjust the namespace accordingly fetchOpts: { repo: "https://hiddeco.github.io/cronjobber", }, }, { provider: provider }); // Once the chart is deployed, you can define a time zone aware CronJob resource. const cronJob = new kubernetes.apiextensions.CustomResource("example-cronjob", { apiVersion: "cronjobber.hidde.co/v1alpha1", kind: "CronJob", metadata: { name: "example", namespace: "default", // Use the namespace where cronjobber is deployed }, spec: { schedule: "0 0 * * *", // This is the schedule in cron format timeZone: "Europe/Amsterdam", // Specify the time zone jobTemplate: { // Define the job spec, which will be run according to the schedule spec: { template: { spec: { containers: [{ name: "example-container", image: "busybox", args: ["/bin/sh", "-c", "date; echo Hello from the Kubernetes cluster"], }], restartPolicy: "OnFailure", }, }, }, }, }, }, { dependsOn: cronjobberChart, provider: provider }); // Export the name of the cron job export const cronJobName = cronJob.metadata.name;
Let's breakdown this program:
- We run this as part of a TypeScript Pulumi program. This allows you to manage your infrastructure as code.
- We import the Pulumi Kubernetes package which allows us to interact with Kubernetes resources using Pulumi's infrastructure as code approach.
- We create a provider that represents the configured Kubernetes cluster.
- We install
cronjobber
using a Helm chart. Helm charts are a way to package Kubernetes applications, andcronjobber
provides the CRD definition for time zone aware CronJobs. - We define a CronJob that uses the
cronjobber
API with the schedule, time zone, and job details such as the container to run. - Upon completion, Pulumi will ensure that your Kubernetes cluster has this cron job set up and scheduled as defined in the code.
Please make sure to adjust the
namespace
,version
, and any other specifics as per your Kubernetes environment and thecronjobber
chart version you wish to use.Finally, to run this Pulumi program, you would typically execute it using
pulumi up
, which will prompt you to review and confirm the changes before applying them to your Kubernetes cluster.- First, we need to install the