For the complete documentation index, see llms.txt. This page is also available as Markdown.

Run Kubernetes Jobs

This topic describes how to define Kubernetes Jobs in Harness Services add then execute them using the Apply step.

In this topic, we will show you how to execute a Job in a Harness Kubernetes deployment as part of the main deployment.

Kubernetes Jobs create one or more pods to carry out commands. For example, a calculation or a backup operation.

In a Harness Kubernetes CD stage, you define Jobs in the Service Manifests. Next you add the Apply step to your Harness Workflow to execute the Job.

Typically, Jobs are not part of the main deployment. You can exclude them from the main deployment and simply call them at any point in the stage using the Apply step. For steps on ignoring the Job as part of the main deployment and executing it separately, see Deploy Manifests Separately using Apply Step.

Before you begin

  • ​Kubernetes Jobs: We assume you are familiar with Kubernetes Jobs.

  • Apply step: The Harness Apply step allows you to deploy any resource you have set up in the Service Manifests section at any point in your stage. See Deploy Manifests Separately using Apply Step.

  • Ignoring Manifests: You can annotate a manifest to have Harness ignore it when performing its main deployment operations. Then you can use the Apply step to execute the manifest wherever you want to run it in the stage. See Ignore a Manifest File During Deployment.

  • Delete Jobs before rerunning deployments: Once you've deployed the Job, you must delete it before deploying a Job of the same name to the same namespace.

Apply step

CD stages include an Apply step that allows you to deploy any resource you have set up in the Service Manifests section.

For details on what you can deploy in different Harness deployment types, see What Can I Deploy in Kubernetes?.

The Apply step can deploy all workload types, including Jobs in any deployment type.

You can add an Apply step anywhere in your Harness stage. This makes the Apply step useful for running Kubernetes Jobs.

Here are some Job examples:

  • Run independent but related work items in parallel: sending emails, rendering frames, transcoding files, or scanning database keys.

  • Create a new pod if the first pod fails or is deleted due to a node hardware failure or node reboot.

  • Create a Job that cleans up the configuration of an environment, to create a fresh environment for deployment.

  • Use a Job to spin down the replica count of a service, to save on cost.

Any workload deployed with the Apply step is not rolled back by Harness.Delete Jobs before rerunning deployments: Once you've deployed the Job, you must delete it before deploying a Job of the same name to the same namespace.### Step 1: Add Job Manifest

In a CD stage, click Service.

In Manifests, add your manifests as described in Add Kubernetes Manifests.

Include your Job manifest in the folder you specify in Manifests.

For example, here's a Manifests section that points to the templates folder.

In the templates folder, there is a folder named jobs and a job.yaml manifest.

Define target cluster

Jobs do not require any changes to the way you specify the target cluster in Harness.

For steps on setting up the target cluster, see Define Your Kubernetes Target Infrastructure.

Add the Job to the execution using the Apply step

The Apply step can be used in any Kubernetes deployment strategy.

In the stage Execution, click Add Step wherever you want to deploy you Job.

In the Apply Step, in File Path, enter the path to the Job manifest relative to the path entered in the Service Manifests section.

For example, the path in Manifests is default-k8s-manifests/Manifests/Files/templates and the Job manifest is in the subfolder of templates named jobs/job.yaml.

In the Apply Step, in File Path, enter jobs/job.yaml.

Skip dry run

By default, Harness uses the --dry-run flag on the kubectl apply command, which prints the object that would be sent to the cluster without really sending it.

If the Skip Dry Run option is selected, Harness will not use the --dry-run flag.

Skip steady state check

If you select this option, Harness will not check that the workload (Job) has reached steady state.

Deploy the Job

In your Pipeline, click Save, Run, and then Run Pipeline.

Let's look at the output of the execution.

In the execution, click the Apply step, and then click Console View.

In Fetch Files, you'll see that Harness successfully fetched the job.yaml:

In Initialize, you can see Harness render the manifest and perform a dry run:

In Prepare, you can see Harness process the manifest before applying it:

In Apply, you can see the kubectl apply:

In Wait for Steady State, you can see Harness wait for the Job to reach steady state. In Harness, this is a managed workload because Harness verifies it has reached steady state and fails the Pipeline if it does not.

Showing Job output

To view Job output after the Apply step, you can use a simple script in a Shell Script step:

In the case of the Job used in this topic, the output will be π to 2000 places:

If you need to show the logs during job execution rather than after the Apply step, then modify the script and run the step in parallel with Apply.

Alternatively, if you have your cluster logs going to a log service you can generate a URL to that system that shows the job logs in parallel as well.

Summary

Using the Apply step, you are able to configure, manage, and deploy a Kubernetes Job.

Last updated

Was this helpful?