In a Serverless Kubernetes (ASK) cluster, you can create pods as needed. When a pod stops running, the billing stops, and you do not need to reserve computing resources for processing jobs. This solves the issues of insufficient computing resources and expanding of the cluster. You can reduce the computing cost by applying preemptible instances. This topic describes how to use ASK to create jobs as needed.

Procedure

  1. Use the kubectl client to create a job.yaml file, copy the following code into the file:
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      template:
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
            resources:
              requests:
                cpu: 16
                memory: 32Gi
          restartPolicy: Never
      backoffLimit: 4
  2. Run the following command to deploy a job:
    kubectl apply -f job.yaml
  3. Run the following command to check the status of the pod:
    # kubectl get pod
    NAME       READY   STATUS      RESTARTS   AGE
    pi-4f7w5   0/1     Completed   0          80s
    
    #kubectl describe pod
    Name:               pi-4f7w5
    Namespace:          default
    Priority:           0
    PriorityClassName:  <none>
    Node:               virtual-kubelet-cn-hongkong-b/10.10.66.169
    ...
    Events:
      Type    Reason                 Age   From          Message
      ----    ------                 ----  ----          -------
      Normal  SuccessfulMountVolume  114s  kubelet, eci  MountVolume.SetUp succeeded for volume "default-token-8k4jz"
      Normal  Pulling                113s  kubelet, eci  pulling image "perl"
      Normal  Pulled                 64s   kubelet, eci  Successfully pulled image "perl"
      Normal  Created                64s   kubelet, eci  Created container
      Normal  Started                64s   kubelet, eci  Started container
  4. Optional:To use a preemptible instance, add the annotation of the preemptible instance to the pod.
    For more information about how to set the annotation of preemptible instances, see Use preemptible instances.
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      template:
        metadata:
          annotations:
            k8s.aliyun.com/eci-spot-strategy: SpotAsPriceGo
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
            resources:
              requests:
                cpu: 16
                memory: 32Gi
          restartPolicy: Never
      backoffLimit: 4