All Products
Search
Document Center

Container Service for Kubernetes:Create a Job

Last Updated:Apr 22, 2024

A Job processes multiple short-lived, one-off tasks at a time to ensure that one or more pods in the tasks terminate with success. You can use a Job to create an application in the Container Service for Kubernetes (ACK) console. This topic describes how to use a Job to create a busybox application and the operations that you can perform on the application.

Prerequisites

An ACK cluster is created. For more information, see Create an ACK managed cluster.

Job types

Kubernetes supports the following types of Jobs:

  • Non-parallel Jobs: In most cases, a Job of this type starts only one pod unless the pod fails. The Job is considered complete when the pod terminates with success.

  • Jobs with a fixed completion count: A non-zero positive value is specified for .spec.completions. A Job of this type starts pods one by one. The Job is considered complete when the number of pods that terminate with success is equal to the value of .spec.completions.

  • Parallel Jobs with a work queue: A non-zero positive value is specified for .spec.Parallelism. A Job of this type starts multiple pods at a time. The Job is considered complete when all pods terminate and at least one pod terminates with success. .spec.completions is not specified.

  • Parallel Jobs with a fixed completion count: A Job of this type has both .spec.completions and .spec.Parallelism specified. The Job starts multiple pods at a time to process a work queue.

Jobs can manage pods based on the settings of .spec.completions and .spec.Parallelism as described in the following table.

Note

The Job created in this example is a parallel Job with a fixed completion count.

Job type

Examples

Action

Completions

Parallelism

One-off Job

Database migration

The Job starts only one pod. The Job is complete when the pod terminates with success.

1

1

Job with a fixed completion count

Start pods one by one to process a work queue

The Job starts pods one by one. The Job is complete when the number of pods that terminate with success reaches the value of .spec.completions.

2+

1

Parallel Job with a fixed completion count

Start multiple pods at a time to process a work queue

The Job starts multiple pods at a time. The Job is complete when the number of pods that terminate with success reaches the value of .spec.completions.

2+

2+

Parallel Job

Start multiple pods at a time to process a work queue

The Job starts one or more pods at a time. The Job is complete when at least one pod terminates with success.

1

2+

Procedure

Step 1: Configure basic settings

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Jobs in the left-side navigation pane.

  3. On the Jobs page, click Create from Image.

  4. On the Basic Information wizard page, configure the basic settings of the application.

    Parameter

    Description

    Name

    Enter a name for the application. Example: busybox.

    Type

    Set Type to Job.

    Label

    Add labels to the application. The labels are used to identify the application.

    Annotations

    Add annotations to the application.

    Synchronize Timezone

    Specify whether to synchronize the time zone between nodes and containers.

  5. Click Next to configure containers.

Step 2: Configure containers

On the Container wizard page, configure the following container settings: images, resources, ports, environment variables, health checks, lifecycle configurations, volumes, and Log Service configurations.
Note On the Container wizard page, Click Add Container to add containers to the pods that run the application.
  1. Configure containers.

    1. In the General section, configure the basic settings of the container.

      Parameter

      Description

      Image Name

      • Select images

        You can click Select images and select an image. The following types of images are supported:

        • Container Registry Enterprise Edition: Select an image stored on a Container Registry Enterprise Edition instance. You must select the region and the Container Registry instance to which the image belongs. For more information about Container Registry, see What is Container Registry?

        • Container Registry Personal Edition: Select an image stored on a Container Registry Personal Edition instance. You must select the region and the Container Registry instance to which the image belongs.

        • Artifact Center: The artifact center contains base OS images, base language images, and AI- and big data-related images for application containerization. In this example, an NGINX image is selected. For more information, see Artifact center.

          Note

          The artifact center of Container Registry provides base images that are updated and patched by Alibaba Cloud or OpenAnolis. If you have other requirements or questions, join the DingTalk group 33605007047 to request technical support.

        You can also enter the address of an image that is stored in a private registry. The image address must be in the following format: domainname/namespace/imagename:tag.

      • Image Pull Policy

        You can select the following image pulling policies:

        • ifNotPresent: If the image that you want to pull is found on your on-premises machine, the image on your on-premises machine is used. Otherwise, ACK pulls the image from the image registry.

        • Always: ACK pulls the image from the registry each time the application is deployed or expanded.

        • Never: ACK uses only images on your on-premises machine.

        Note

        If you select Image Pull Policy, no image pulling policy is applied.

      • Set Image Pull Secret

        You can click Set Image Pull Secret to set a Secret for pulling images from a private registry.

      Resource Limit

      Specify the maximum amount of CPU, memory, and ephemeral storage resources that the application can use. This prevents the application from occupying an excessive amount of resources. For more information about how to configure resource limits, see Resource profiling.

      Required Resources

      Specify the amount of CPU, memory, and ephemeral storage resources reserved for the application. These resources are dedicated to the pods of the application and cannot be preempted by other applications. For more information about how to reserve resources, see Resource profiling.

      Container Start Parameter

      • stdin: specifies that start parameters are sent to the container as standard input (stdin).

      • tty: specifies that start parameters defined in a virtual terminal are sent to the container.

      The two options are usually used together. In this case, the virtual terminal (tty) is associated with the stdin of the container. For example, an interactive program receives the stdin from the user and displays the content in the terminal.

      Privileged Container

      • If you select Privileged Container, privileged=true is set for the container and the privilege mode is enabled.

      • If you do not select Privileged Container, privileged=false is set for the container and the privilege mode is disabled.

      Init Containers

      Select this option to create an init container.

      Init containers can be used to block or postpone the startup of application containers. Application containers in a pod start concurrently only after init containers start. For example, you can use init containers to verify the availability of a dependent service. You can run tools or scripts that are not provided by an application image in init containers to initialize the runtime environment for application containers. For example, you can run tools or scripts to set kernel parameters or generate configuration files. For more information, see init containers.

    2. Optional:In the Ports section, click Add to configure ports for the container.

      Parameter

      Description

      Name

      Enter a name for the port.

      Container Port

      Specify the container port that you want to expose. The port number must be in the range of 1 to 65535.

      Protocol

      Valid values: TCP and UDP.

    3. Optional:In the Environments section, click Add to set environment variables.
      You can configure environment variables for pods in key-value pairs. Environment variables are used to apply pod configurations to containers. For more information, see Pod variables.

      Parameter

      Description

      Type

      Specify the type of environment variable that you want to add. Valid values:

      • Custom

      • ConfigMaps

      • Secrets

      • Value/ValueFrom

      • ResourceFieldRef

      If you select ConfigMaps or Secrets, you can pass all data in the selected ConfigMap or Secret to the container environment variables. In this example, Secrets is selected. Select Secrets from the Type drop-down list and select a Secret from the Value/ValueFrom drop-down list. By default, all data in the selected Secret is passed to the environment variable.环境变量

      In this case, the YAML file that is used to deploy the application contains the settings that reference all data in the selected Secret.yaml

      Variable Key

      Specify the name of the environment variable.

      Value/ValueFrom

      Specify the value of the environment variable.

    4. Optional:In the Health Check section, you can enable liveness, readiness, and startup probes based on your business requirements.

      For more information, see Configure Liveness, Readiness and Startup Probes.

      Parameter

      Request type

      Description

      • Liveness: Liveness probes are used to determine when to restart a container.

      • Readiness: Readiness probes are used to determine whether a container is ready to accept traffic.

      • Startup: Startup probes are used to determine when to start a container.

        Note

        Startup probes are supported in Kubernetes 1.18 and later.

      HTTP

      Sends an HTTP GET request to the container. You can set the following parameters:

      • Protocol: HTTP or HTTPS.

      • Path: the requested HTTP path on the server.

      • Port: Enter the container port that you want to expose. Enter a port number that ranges from 1 to 65535.

      • HTTP Header: the custom headers in the HTTP request. Duplicate headers are allowed. You can set HTTP headers in key-value pairs.

      • Initial Delay (s): the initialDelaySeconds field in the YAML file. This field specifies the waiting time (in seconds) before the first probe is performed after the container is started. Default value: 3.

      • Period (s): the periodSeconds field in the YAML file. This field specifies the time interval (in seconds) at which probes are performed. Default value: 10. Minimum value: 1.

      • Timeout (s): the timeoutSeconds field in the YAML file. This field specifies the time (in seconds) after which a probe times out. Default value: 1. Minimum value: 1.

      • Healthy Threshold: the minimum number of consecutive successes that must occur before a container is considered healthy after a failed probe. Default value: 1. Minimum value: 1. For liveness probes, this parameter must be set to 1.

      • Unhealthy Threshold: the minimum number of consecutive failures that must occur before a container is considered unhealthy after a success. Default value: 3. Minimum value: 1.

      TCP

      Sends a TCP socket to the container. kubelet attempts to open the socket on the specified port. If the connection can be established, the container is considered healthy. Otherwise, the container is considered unhealthy. You can configure the following parameters:

      • Port: Enter the container port that you want to open. Enter a port number that ranges from 1 to 65535.

      • Initial Delay (s): the initialDelaySeconds field in the YAML file. This field specifies the waiting time (in seconds) before the first probe is performed after the container is started. Default value: 15.

      • Period (s): the periodSeconds field in the YAML file. This field specifies the time interval (in seconds) at which probes are performed. Default value: 10. Minimum value: 1.

      • Timeout (s): the timeoutSeconds field in the YAML file. This field specifies the time (in seconds) after which a probe times out. Default value: 1. Minimum value: 1.

      • Healthy Threshold: the minimum number of consecutive successes that must occur before a container is considered healthy after a failed probe. Default value: 1. Minimum value: 1. For liveness probes, this parameter must be set to 1.

      • Unhealthy Threshold: the minimum number of consecutive failures that must occur before a container is considered unhealthy after a success. Default value: 3. Minimum value: 1.

      Command

      Runs a probe command in the container to check the health status of the container. You can configure the following parameters:

      • Command: the probe command that is run to check the health status of the container.

      • Initial Delay (s): the initialDelaySeconds field in the YAML file. This field specifies the waiting time (in seconds) before the first probe is performed after the container is started. Default value: 5.

      • Period (s): the periodSeconds field in the YAML file. This field specifies the time interval (in seconds) at which probes are performed. Default value: 10. Minimum value: 1.

      • Timeout (s): the timeoutSeconds field in the YAML file. This field specifies the time (in seconds) after which a probe times out. Default value: 1. Minimum value: 1.

      • Healthy Threshold: the minimum number of consecutive successes that must occur before a container is considered healthy after a failed probe. Default value: 1. Minimum value: 1. For liveness probes, this parameter must be set to 1.

      • Unhealthy Threshold: the minimum number of consecutive failures that must occur before a container is considered unhealthy after a success. Default value: 3. Minimum value: 1.

    5. Optional:In the Lifecycle section, configure the lifecycle of the container.

      You can set the following parameters to configure the lifecycle of the container: Start, Post Start, and Pre Stop. For more information, see Configure the lifecycle of a container.生命周期

      Parameter

      Description

      Start

      Specify a command and parameter that take effect before the container starts.

      Post Start

      Specify a command that takes effect after the container starts.

      Pre Stop

      Specify a command that takes effect before the container stops.

    6. Optional:In the Volume section, you can mount local volumes and persistent volume claims (PVCs) to the container.

      Parameter

      Description

      Add Local Storage

      You can select HostPath, ConfigMap, Secret, or EmptyDir from the PV Type drop-down list. Then, set Mount Source and Container Path to mount the volume to the container. For more information, see Volumes.

      Add PVC

      You can mount persistent volumes (PVs) by using persistent volume claims (PVCs). You must create a PVC before you can select the PVC to mount a PV. For more information, see Create a PVC.

      In this example, a PVC named disk-ssd is mounted to the /tmp path of the container. Configure a volume
    7. Optional:In the Log section, you can specify logging configurations and add custom tags to the collected log.
      Important Make sure that the Log Service agent is installed in the cluster.

      Parameter

      Description

      Collection Configuration

      Logstore: creates a Logstore in Simple Log Service to store the collected log.

      Log Path in Container: Specify stdout or a container path to collect log data.

      • Collect stdout files: If you specify stdout, the stdout files are collected.

      • Text Logs: specifies that the log in the specified path of the container is collected. In this example, /var/log/nginx is specified as the path. Wildcard characters can be used in the path.

      Custom Tag

      You can also add custom tags. The tags are added to the log of the container when the log is collected. Custom tags provide an easy method to filter and analyze the collected log.

    8. Click Next.
      Proceed to the Advanced wizard page.

Step 3: Configure advanced settings

  1. You can set the status of a Job in the Job Settings section.

    Parameter

    Description

    Completions

    The COMPLETIONS parameter in the YAML file. This parameter specifies the number of pods that must complete with success. Default value: 1.

    Parallelism

    The number of pods that the job must run in parallel at any time. Default value: 1.

    Timeout

    The activeDeadlineSeconds field in the YAML file. This parameter specifies the time limit of the job. If the job is not complete within the time limit, the system attempts to terminate the job.

    BackoffLimit

    The backoffLimit parameter in the YAML file. This parameter specifies the number of retries that are performed by the job upon pod failures. Each time a pod fails, the pod is recreated with a delay that exponentially increases until a maximum of 6 minutes. Default value: 6.

    Restart

    Valid values: Never and On Failure.

  2. Optional:In the Labels and Annotations section, click Add to configure labels and annotations for the pod.

    Parameter

    Description

    Pod Labels

    Add a label to the pod. The label is used to identify the application.

    Pod Annotations

    Add an annotation to the pod configurations.

  3. Click Create.

Step 4: Check the application

  1. After the application is deployed, you are redirected to the Complete wizard page. The resource objects of the application are displayed.

    You can click View Details to view application details. In this example, two pods are created to run in parallel based on the Job configuration.

    Wait until all pods are created.

  2. Click Workloads > Jobs. On the Jobs page, you can view the time when the Job is complete.

    Note

    The end time of the Job is not displayed until all pods of the Job are created.

What to do next

You can perform the following operations after a Job is created:

Find the application that you want to manage and click the application name or click Details in the Actions column. On the details page, you can scale and refresh the application. You can also view the YAML file of the application.

  • Scale: On the application details page, click Scale to scale the application to a required number of pods.

  • View the YAML file: On the application details page, click View in YAML. Then, you can update and download the YAML file. You can also save the YAML file as a template.

  • Refresh: On the application details page, click the image.png icon to update the application information.

Find the application that you want to delete and choose More > Delete in the Actions column.