Jobs

A Job creates one or more Pods and ensures that a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the task (ie, Job) is complete. Deleting a Job will clean up the Pods it created.

A simple case is to create one Job object in order to reliably run one Pod to completion. The Job object will start a new Pod if the first Pod fails or is deleted (for example due to a node hardware failure or a node reboot). You can also use a Job to run multiple Pods in parallel.

The following example demonstrates specific steps of creating a Job (computing π to 2000 decimal places) in KubeSphere.

Prerequisites

  • You need to create a workspace, project and project-regular account. Please refer to Create Workspace, Project, Account and Role if they are not ready yet.
  • You need to sign in with project-admin account first and invite project-regular to the corresponding project. If it is not ready yet, please invite a member first.

Create a Job

Step 1. Open Modal

  1. Log in the console as project-regular. Go to Application Workloads and click Jobs.
  2. Click Create button to open the modal.

create-job

Step 2. Basic Info

Enter the basic information. Refer to the image below as an example.

  • Name: The name of the job, which is also the unique identifier.
  • Alias: The alias name of the job, making resources easier to identify.
  • Description: The description of the job, which gives a brief introduction of the job.

job-create-basic-info

Step 3. Job Settings (Optional)

You can set the values in this step as below or click Next to skip it directly. Refer to the table below for detailed explanations of each field.

job-create-job-settings

NameDefinitionDescription
Back off Limitspec.backoffLimitIt specifies the number of retries before this job is marked failed. It defaults to 6.
Completionsspec.completionsIt specifies the desired number of successfully finished pods the job should be run with. Setting it to nil means that the success of any pod signals the success of all pods, and allows parallelism to have any positive value. Setting it to 1 means that parallelism is limited to 1 and the success of that pod signals the success of the job. For more information, see Jobs.
Parallelismspec.parallelismIt specifies the maximum desired number of pods the job should run at any given time. The actual number of pods running in a steady state will be less than this number when the work left to do is less than max parallelism ((.spec.completions - .status.successful) < .spec.parallelism). For more information, see Jobs.
Active Deadline Secondsspec.activeDeadlineSecondsIt specifies the duration in seconds relative to the startTime that the job may be active before the system tries to terminate it; the value must be a positive integer.

Step 4. Container Image

  1. Select Never for Restart Policy. You can only specify Never or OnFailure for Restart Policy when the job is not completed:

    • If Restart Policy is set to Never, the job creates a new Pod when the Pod fails, and the failed Pod does not disappear.

    • If Restart Policy is set to OnFailure, the job will internally restart the container when the Pod fails, instead of creating a new Pod.

job-container-settings

  1. Click Add Container Image which directs you to the Add Container page. Enter perl in the image search bar and press the Return key.

add-container-image-job

  1. On the same page, scroll down to Start Command. Input the following commands in the box which computes pi to 2000 places then prints it. Click in the bottom right corner and select Next to continue.
  1. perl,-Mbignum=bpi,-wle,print bpi(2000)

start-command-job

Step 5. Inspect Job Manifest (Optional)

  1. Enable Edit Mode in the top right corner which displays the manifest file of the job. You can see all the values are set based on what you have specified in the previous steps.
  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. namespace: cc
  5. labels:
  6. app: job-test-1
  7. name: job-test-1
  8. annotations:
  9. kubesphere.io/alias-name: Test
  10. kubesphere.io/description: A job test
  11. spec:
  12. template:
  13. metadata:
  14. labels:
  15. app: job-test-1
  16. annotations:
  17. kubesphere.io/containerSecrets: null
  18. spec:
  19. containers:
  20. - name: container-xv4p2o
  21. imagePullPolicy: IfNotPresent
  22. image: perl
  23. command:
  24. - perl
  25. - '-Mbignum=bpi'
  26. - '-wle'
  27. - print bpi(2000)
  28. restartPolicy: Never
  29. serviceAccount: default
  30. initContainers: []
  31. volumes: []
  32. imagePullSecrets: null
  33. backoffLimit: 5
  34. parallelism: 2
  35. completions: 4
  36. activeDeadlineSeconds: 300
  1. You can make adjustments in the manifest directly and click Create or disable the Edit Mode and get back to the Create Job page.

Note

You can skip Mount Volumes and Advanced Settings for this tutorial. For more information, see Pod Volumes and Deployment Advanced Settings.

Step 6. Check Result

  1. In the final step of Advanced Settings, click Create to finish. A new item will be added the Job list if the creation is successful.

job-list-new

  1. Click this job and go to Execution Records tab where you can see the information of each execution record. There are four completed Pods since Completions was set to 4 in Step 3.

execution-record

Tip

You can rerun the job if it fails, the reason of which displays under Messages.

  1. In Resource Status, you can inspect the Pod status. Two pods were created each time as Parallelism was set to 2. Click the arrow on the right and check the container log as shown below, which displays the expected calculation result.

container-log

container-log-check

Tip

  • In Resource Status, the pod list provides the pod’s detailed information (e.g. creation time, node, pod IP and monitoring data).
  • You can view the container information by clicking the pod.
  • Click the container log icon to view the output logs of the container.
  • You can view the pod detail page by clicking the pod name.

Job Operations

On the job detail page, you can manage the job after it is created.

  • Edit Info: Edit the basic information except Name of the job.
  • Rerun Job: Rerun the job, the pod will restart, and a new execution record will be generated.
  • View YAML: View the job’s specification in YAML format.
  • Delete: Delete the job and return to the job list page.

job-operation