Job Management

Last updated: 2021-06-23 15:47:02

    Overview

    A Job creates one or more Pods and ensures that these pods run according to the specified rules until a specified number of them successfully terminate. Jobs can be used in many scenarios, such as batch computing and data analysis. You can specify the number of repeated runs, the level of parallelism and the restart policy as needed.
    A Job will keep existing Pods and not create new Pods after it is complete. You can view the logs of completed Pods in "Logs". Deleting a Job will clean up the Pods it created as well as the logs of those Pods.

    Managing Jobs in the Console

    Creating a Job

    1. Log in to the TKE console.
    2. In the left sidebar, click Cluster to go to the cluster management page.
    3. Click the ID of the cluster where Job needs to be created to enter the cluster management page.
    4. Select Workload > Job to go to the Job information page, as shown below:
      Job
    5. Click Create to go to the Create Workload page, as shown below:
      Create a workload
    6. Set the Job parameters based on your actual needs. The key parameters are as follows:
      • Workload Name: customize the workload name.
      • Namespace: select the namespace based on your actual needs.
      • Type: select Job (One-time Task).
        Job Settings: set one or more containers for a Pod of the Job as needed.
        • Repeat Times: set the times of repeated executions of Pods under this Job.
        • Concurrent Pods: set the number of parallel Pods in this Job.
        • Restart Policy: set the restart policy applied when containers under the Pod abnormally exit.
          • Never: do not restart the container until all the containers under the Pod exit.
          • OnFailure: the Pod continues to run while the container will be restarted.
      • Containers in the Pod: set one or more containers for a Pod of the Job as needs.
        • Name: customize the name of the container in the Pod.
        • Image: select the image based on your actual needs.
        • Image Tag: enter the tag based on your actual needs.
        • CPU/Memory Limits: set the CPU and memory limit according to Kubernetes' resource limits to improve the robustness of the business.
        • Advanced Settings: you can set the parameters such as Working Directory, Running Command, Running Parameter, Container Health Check, and Privileged Container.
    7. Click Create Workload to complete the creation.

    Viewing Job Status

    1. Log in to the TKE console.
    2. In the left sidebar, click Cluster to go to the cluster management page.
    3. Click the ID of the cluster for which you want to view the Job status to enter the cluster management page.
    4. Select Workload > Job to go to the Job information page, as shown below:
      Job
    5. To view the Job's details, click its name.

    Deleting a Job

    A Job will keep existing Pods and not create new Pods after it is complete. You can view the logs of completed Pods in "Logs". Deleting a Job will clean up the Pods it created as well as the logs of those Pods.

    Managing Jobs via Kubectl

    YAML sample

    apiVersion: batch/v1
    kind: Job
    metadata:
    name: pi
    spec:
    completions: 2
    parallelism: 2
    template:
      spec:
        containers:
        - name: pi
          image: perl
          command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
        restartPolicy: Never
    backoffLimit: 4
    
    • kind: identifies the Job resource type.
    • metadata: the basic information such as Job name and label.
    • metadata.annotations: the additional description of the Job. You can set additional enhancements to TKE through this parameter.
    • spec.completions: the times of repeated executions of Pods under this Job.
    • spec.parallelism: the number of parallel Pods in this Job.
    • spec.template: the detailed template configuration for Pod of the Job.

    Creating a Job

    1. See the YAML sample to prepare the Job YAML file.

    2. Install kubectl and connect to a cluster. For detailed operations, see Connecting to a Cluster.

    3. Create the Job YAML file.

      kubectl create -f Job YAML filename
      

      For example, to create a Job YAML file named pi.yaml, run the following command:

      kubectl create -f pi.yaml
      
    4. Run the following command to check whether the Job is successfully created.

      kubectl get job
      

    If a message similar to the following is returned, the creation is successful.

    NAME      DESIRED   SUCCESSFUL   AGE
    job       1         0            1m
    

    Deleting a Job

    Run the following command to delete a Job.

    kubectl delete job [NAME]