Creating a Job
A job is a Kubernetes workload designed for managing batch processing tasks, handling one-time or short-lived tasks. Unlike long-running services managed by Deployments or StatefulSets, a job creates one or more pods, executing them with defined start and end phases. If the task is not completed, the job retries pod execution until the target is reached.
You can set different tasks for a job as required.
- One-off job: A job creates only one pod to execute a task. If the task is successfully completed, the pod exits (exit 0).
- Job with fixed completion counts: A job must have multiple pods successfully executed tasks. When a specified number of successful pod completions are reached, the task is complete.
- Job with a work queue: A job processes tasks in a work queue. Each pod is allocated with an index. If each of these pods is successfully completed, the job is finished.
Prerequisites
- A cluster is available. For details about how to create a cluster, see Buying a CCE Standard/Turbo Cluster.
- There are some available nodes in the cluster. If no node is available, create one by referring to Creating a Node.
Using the CCE Console
- Log in to the CCE console.
- Click the cluster name to go to the cluster console, choose Workloads in the navigation pane, and click the Create Workload in the upper right corner.
- Configure basic information about the workload.
Parameter
Description
Workload Type
Select Job. For details about different workload types, see Workload Overview.
Workload Name
Enter a name for the workload. Enter 1 to 63 characters starting with a lowercase letter and ending with a lowercase letter or digit. Only lowercase letters, digits, and hyphens (-) are allowed.
Namespace
Select a namespace for the workload. The default value is default. You can also click Create Namespace to create one. For details, see Creating a Namespace.
Pods
Enter the number of workload pods.
Container Runtime
A CCE standard cluster uses a common runtime by default, whereas a CCE Turbo cluster supports both common and secure runtimes. For details about their differences, see Secure Runtime and Common Runtime.
- Configure container settings for the workload.
- Container Information: Click Add Container on the right to configure multiple containers for the pod.
If you configured multiple containers for a pod, ensure that the ports used by each container do not conflict with each other, or the workload cannot be deployed.
- Basic Info: Configure basic information about the container.
Parameter
Description
Container Name
Enter a name for the container.
Pull Policy
Image update or pull policy. If you select Always, the image is pulled from the image repository each time. If you do not select Always, the existing image of the node is preferentially used. If the image does not exist, the image is pulled from the image repository.
Image Name
Click Select Image and select the image used by the container.
- SWR Shared Edition: easy-to-use, secure, and reliable image management
- My Images: You can select a private image that you have uploaded. For details about how to upload images, see Pushing an Image Through a Container Engine Client.
- Open Source Images: Select a public image provided by SWR. For details, see Image Center.
- Shared Images: You can select an image shared by another account. For details, see Sharing Private Images.
To use a third-party image, directly enter image path. Ensure that the image access credential can be used to access the image repository. For details, see Using Third-Party Images.
Image Tag
Select the image tag to be deployed.
CPU Quota
- Request: minimum number of CPU cores required by a container. The default value is 0.25 cores.
- Limit: maximum number of CPU cores that can be used by a container. This prevents containers from using excessive resources.
If Request and Limit are not specified, the quota is not limited. For more information and suggestions about Request and Limit, see Configuring Container Specifications.
Memory Quota
- Request: minimum amount of memory required by a container. The default value is 512 MiB.
- Limit: maximum amount of memory available for a container. When memory usage exceeds the specified memory limit, the container will be terminated.
If Request and Limit are not specified, the quota is not limited. For more information and suggestions about Request and Limit, see Configuring Container Specifications.
(Optional) GPU Quota
Configurable only when the cluster contains GPU nodes and the CCE AI Suite (NVIDIA GPU) add-on has been installed.
- Do not use: No GPU will be used.
- GPU card: The GPU is dedicated for the container.
- GPU Virtualization: percentage of GPU resources used by the container. For example, if this parameter is set to 10%, the container will use 10% of GPU resources.
For details about how to use GPUs in a cluster, see Default GPU Scheduling in Kubernetes.
(Optional) NPU Quota
Number of required NPU chips. The value must be an integer and the CCE AI Suite (Ascend NPU) add-on must be installed.
For details about how to use NPUs in a cluster, see Complete NPU Allocation.
(Optional) Privileged Container
Programs in a privileged container have certain privileges.
If Privileged Container is enabled, the container is assigned privileges. For example, privileged containers can manipulate network devices on the host machine and modify kernel parameters.
(Optional) Init Container
Whether to use the container as an init container. An init container does not support health check.
An init container is a special container that runs before other app containers in a pod are started. Each pod can contain multiple containers. In addition, a pod can contain one or more init containers. Application containers in a pod are started and run only after the running of all init containers completes. For details, see Init Containers.
- SWR Shared Edition: easy-to-use, secure, and reliable image management
- (Optional) Lifecycle: Configure operations to be performed in a specific phase of the container lifecycle, such as Startup Command, Post-Start, and Pre-Stop. For details, see Configuring the Container Lifecycle.
- (Optional) Environment Variables: Configure variables for the container running environment using key-value pairs. These variables transfer external information to containers running in pods and can be flexibly modified after application deployment. For details, see Configuring Environment Variables.
- (Optional) Data Storage: Mount local storage or cloud storage to the container. The application scenarios and mounting modes vary with the StorageClass. For details, see Storage.
If the workload contains more than one pod, EVS volumes cannot be mounted.
- (Optional) Logging: Report standard container output logs to AOM by default, without requiring manual settings. You can manually configure the log collection path. For details, see Collecting Container Logs Using ICAgent (Not Recommended).
To disable the collection of the standard output logs of the current workload, add the annotation kubernetes.AOM.log.stdout: [] in Labels and Annotations in the Advanced Settings area. For details about how to use this annotation, see Table 1.
- Basic Info: Configure basic information about the container.
- Image Access Credential: Select the credential used for accessing the image repository. The default value is default-secret. You can use default-secret to access images in SWR Shared Edition. For details about default-secret, see default-secret.
- (Optional) GPU: All is selected by default. The workload instance will be scheduled to the node of the specified GPU type.
- Container Information: Click Add Container on the right to configure multiple containers for the pod.
- (Optional) Configure advanced settings for the workload.
Parameter
Description
Labels and Annotations
Add labels or annotations for pods using key-value pairs. After the setting, click Confirm. For details about labels and annotations, see Configuring Labels and Annotations.
Job Settings
- Parallel Pods: Maximum number of pods that can run in parallel during job execution. The value cannot be greater than the total number of pods in the job.
- Timeout (s): Once a job reaches this time, the job status becomes failed and all pods in this job will be deleted. If you leave this parameter blank, the job will never time out.
- Completion Mode
- Non-indexed: A job is considered complete when all the pods are successfully executed. Each pod completion is homologous to each other.
- Indexed: Each pod gets an associated completion index from 0 to the number of pods minus 1. The job is considered complete when every pod allocated with an index is successfully executed. For an indexed job, pods are named in the format of $(job-name)-$(index).
- Suspend Job: By default, a job is executed immediately after being created. The job's execution will be suspended if you enable this option, and resumed after you disable it.
Network Configuration
- Pod ingress/egress bandwidth limit: You can set ingress and egress bandwidth limits for pods. For details, see Configuring QoS for a Pod.
- Whether to enable a specified container network configuration: available only for clusters that support this function. After you enable a specified container network configuration, the workload will be created using the container subnet and security group in the configuration. For details, see Binding a Subnet and Security Group to a Namespace or Workload Using a Container Network Configuration.
- Specify the container network configuration name: Only the custom container network configuration whose associated resource type is workload can be selected.
- Click Create Workload in the lower right corner. After a period of time, when the workload enters the processing state, tasks will be processed sequentially. After all tasks are processed, the workload status changes to Executed.
Using kubectl
A job has the following configuration parameters:
- .spec.completions: indicates the number of pods that need to run successfully to end a job. The default value is 1.
- .spec.parallelism: indicates the number of pods that run concurrently. The default value is 1.
- .spec.backoffLimit: indicates the maximum number of retries performed if a pod fails. When the limit is reached, the pod will not try again.
- .spec.activeDeadlineSeconds: indicates the running time of pods. Once the time is reached, the job and its pods are terminated. .spec.activeDeadlineSeconds has a higher priority than .spec.backoffLimit. If a job reaches .spec.activeDeadlineSeconds, spec.backoffLimit is ignored.
Based on the .spec.completions and .spec.parallelism settings, jobs are classified into the following types.
Job Type |
Description |
.spec.completions |
.spec.parallelism |
---|---|---|---|
One-off jobs |
A job creates one pod until it successfully completes. |
1 |
1 |
Jobs with a fixed completion count |
A job creates one pod in sequence and is complete when the number of successful pods reaches the value of .spec.completions. |
>1 |
1 |
Parallel jobs with a fixed completion count |
A job creates multiple pods in sequence and is complete when the number of successful pods reaches the value of .spec.completions. |
>1 |
>1 |
Parallel jobs with a work queue |
A job creates one or more pods. Each pod takes one task from the message queue, processes it, and repeats until the end of the queue is reached. Then the pod deletes the task and exits. For details, see Fine Parallel Processing Using a Work Queue. |
Leave this parameter blank. |
>1 or =1 |
The following is an example job, which calculates π till the 2000th digit and prints the output.
apiVersion: batch/v1 kind: Job metadata: name: myjob spec: completions: 50 # A total of 50 pods need to run to finish a job. In this example, π is printed for 50 times. parallelism: 5 # A total of 5 pods run in parallel. backoffLimit: 5 # A maximum of 5 retries is allowed. template: spec: containers: - name: pi image: perl command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] restartPolicy: Never # For a job, set this parameter to Never or OnFailure. For other controllers (such as Deployments), set this parameter to Always. imagePullSecrets: - name: default-secret
Run the job.
- Start the job.
kubectl apply -f myjob.yaml
Command output:
job.batch/myjob created
- View the job details.
kubectl get job
Command output:
NAME COMPLETIONS DURATION AGE myjob 50/50 23s 3m45s
If the value of COMPLETIONS is 50/50, the job is successfully executed.
- View the pod status.
kubectl get pod
Command output:
NAME READY STATUS RESTARTS AGE myjob-29qlw 0/1 Completed 0 4m5s ...
If the status is Completed, the job is complete.
- View the pod logs.
kubectl logs myjob-29qlw
Command output:3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
Related Operations
After a one-off job is created, you can perform operations listed in Table 2.
Operation |
Description |
---|---|
Viewing a YAML file |
Locate the row containing the target job and choose More > View YAML to view the YAML file of the job. |
Deleting a job |
|
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.