Run a job
Start a job from a container image with the stsh CLI, follow its output, check the result and delete it.
Requires: jobs/write, jobs/readLogs
Running a job needs jobs/write in the resource group, and reading its output
jobs/readLogs. The Jobs Operator, Contributor and Owner roles have both.
Before you start
- The
stshCLI, signed in — see Sign in with the CLI. - The boundary and the resource group to run the job in. Pass them with
-band-g, or leave them out to use your context's defaults. - The id of a cluster the boundary is projected into. List them with:
stsh boundary projections -b my-boundaryStart the job
stsh job create nightly-import -b my-boundary -g my-resource-group \
--set image=ghcr.io/example/importer \
--set tag=1.4.2 \
--set clusterId=<cluster-id> \
--set computePlan=small \
--set 'env={"SOURCE_URL":"https://data.example.com/export.csv"}'Or put the body in a file and pass --file job.json; --set then overrides single fields:
{
"image": "ghcr.io/example/importer",
"tag": "1.4.2",
"clusterId": "<cluster-id>",
"computePlan": "small",
"env": { "SOURCE_URL": "https://data.example.com/export.csv" },
"activeDeadlineSeconds": 1800,
"ttlSecondsAfterFinished": 86400
}- The name becomes the Kubernetes Job's name: lowercase letters, digits and hyphens, starting and ending with a letter or digit, at most 63 characters, and not in use by another job in the resource group.
- Give the image without a tag and the tag separately; the tag is
latestwhen you leave it out. The platform joins the two with a colon, so an image given with its tag ends up with two tags and cannot be pulled. clusterIdis required in practice: without it the job is refused.--setreads a value that parses as JSON as JSON. A tag that looks like a number, such as2or1.4, must be quoted —--set tag='"1.4"'— or the create is refused.
The command prints Done. a few seconds after the platform has accepted the job; it does not wait
for the job to finish. When the platform refuses the job — no cluster, a cluster the boundary is not projected into, a
name already in use — the only answer is failed to create job; check those first.
Follow its output
stsh job logs nightly-import -g my-resource-groupThe command streams what the container writes until you stop it with Ctrl+C. When the container
has exited, the stream ends and the CLI connects again, so it prints the job's whole output again
every few seconds. Before the job's pod exists, and after it is removed, it answers
No pods found for job., repeated the same way.
Check the result
stsh job get nightly-import -g my-resource-group
stsh job list -g my-resource-groupstatus is Pending, Running, Succeeded or Failed; a job that succeeded also has
completedAt. The schedule and lastRunUtc columns of stsh job list stay empty; jobs have
neither. The statuses are described in Job settings.
Run it again
A job runs once. To run it again, delete it and create it again with the same name, or create a job with a new name. A finished job keeps its name taken until it is deleted or removed after its time to live.
Delete a job
stsh job delete nightly-import -g my-resource-groupThe CLI asks you to confirm; -y skips the question. The job is removed at once; Kubernetes then
stops its pod, if it is still running, and removes the pod and its output in the background. The
CLI reports Deleted also for a name that has no job.