Running a Job

The simplest workload you can set up in RAIL is a Job. It will schedule a Pod that runs its container to completion and then stops.

In this example, we will create a job that runs a Python program that computes plenty of digits of 𝜋.

You will need to have access to a container image that does the job and is available from one of the registry locations that RAIL is allowed to pull images from. We have prepared one from the public GitLab project at <https://git.app.uib.no/gisle/k8s-pi-job>. In the repo you can inspect the Python code that runs to do the computation and the Dockerfile required to build the image.

This manifest defines a Job called pi for RAIL:

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  ttlSecondsAfterFinished: 600
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: pi
        image: git.app.uib.no:4567/gisle/k8s-pi-job/pi-job:451766a3
        command: ["python", "pi.py", "10000"]

        # RAIL requires us to specify how resource hungry each container is
        resources:
          requests:
            cpu: 200m
            memory: 5Mi
          limits:
            cpu: 200m
            memory: 20Mi

        # This states the defaults for the securityContext and will get rid of
        # the warning that you should set these values.  These values can not be
        # set at the Pod-level, so they need to be specified here.
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
          runAsNonRoot: true
          seccompProfile:
            type:
              RuntimeDefault

On the login hosts of the cluster where you want to run this job, save this text in a file called job-pi.yaml. Then create this job in the cluster by running:

kubectl apply -f job-pi.yaml

Then inspect the state of the job with:

kubectl describe job/pi

When the job finishes you can read the output generated with:

kubectl logs job/pi

The job and the pod are automatically deleted after 10 minutes (as specified by the ttlSecondsAfterFinished setting). If you want to clean up before this time run this command:

kubectl delete -f job-pi.yaml

If you want to learn about the meaning of an option in a manifest you can look it up with kubectl as well. For example to learn about ttlSecondsAfterFinished run:

kubectl explain job.spec.ttlSecondsAfterFinished

The dotted path given as argument starts out with the type of object (job in this case) and then you just nest the names of the fields until you get to the one you are interested in.