We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Jobs

Every app we've deployed so far is supposed to run forever. If the web server exits, it means something's wrong, and Kubernetes restarts it.

But some work is supposed to finish, things like:

  • A database migration

  • A one-off data cleanup

  • A nightly report

For that, there's the Job. A Job creates a pod and keeps retrying until it exits successfully (with exit code 0). Then it's done, and the pod just sits there in a Completed state so you can read its logs.

Jobs have a few important parts of their configuration:

  • A backoffLimit can be set, which is how many times to retry before giving up and marking the Job as failed (the default is 6).

  • While a Deployment's pods always restart, a Job's pods have a restartPolicy that must be set to either:

    • Never: start a new pod on failure

    • OnFailure: restart the container in the same pod

Assignment

Run a one-off stats report.

  1. apiVersion: batch/v1
    kind: Job
    metadata:
      name: synergychat-stats
    spec:
      backoffLimit: 4
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: synergychat-stats
              image: curlimages/curl:latest
              args:
                - -sf
                - http://crawler-service.crawler.svc.cluster.local/stats

    This Job uses the curl image to fetch the crawler's keyword stats and print them.

  2. kubectl logs job/synergychat-stats

Make sure kubectl proxy is running, then run and submit the CLI tests.