Pages

Tuesday, August 7, 2018

Kubernetes - Repeatable Job

There may be cases where you want to run a program repeatedly. It is very use full to run a program at certain times repeatedly to check something or perform some other actions

Lets create a basic repeatable job manifest file,
[root@manja17-I13330 kubenetes-config]# cat basic-repeatable-job.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: testing-service
spec:
  completions: 3
  template:
    metadata:
      labels:
        name: testing-service
    spec:
      containers:
      - name: counter
        image: centos:7
        command:
          - "bin/bash"
          - "-c"
          - "for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done"
      restartPolicy: Never

The important element is the completions: 3 which tells to run the job until completed for 3 times

[root@manja17-I13330 kubenetes-config]# kubectl create -f basic-repeatable-job.yml
job.batch "testing-service" created

[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                       READY      STATUS                 RESTARTS   AGE
testing-service-strtz   0/1          ContainerCreating   0                3s

[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                          READY     STATUS                 RESTARTS   AGE
testing-service-dpwr2   0/1          ContainerCreating   0               2s
testing-service-strtz     0/1          Completed              0               6s

[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                          READY     STATUS      RESTARTS   AGE
testing-service-7vrx5    0/1          Completed   0              4s
testing-service-dpwr2   0/1          Completed   0              6s
testing-service-strtz      0/1          Completed   0             10s

If we see the above pods, we see that they are created one after another and as defined ran a bash command. Lets see the job status ,
[root@manja17-I13330 kubenetes-config]# kubectl get jobs
NAME              DESIRED   SUCCESSFUL   AGE
testing-service   3               3                   1m

Now if we describe the job, we can see
[root@manja17-I13330 kubenetes-config]# kubectl describe job testing-service
Name:           testing-service
Namespace:      default
Selector:       controller-uid=be5f8b69-8f0c-11e8-a791-020055e1ea1d
Labels:         controller-uid=be5f8b69-8f0c-11e8-a791-020055e1ea1d
                job-name=testing-service
                name=testing-service
Annotations:    <none>
Parallelism:    1
Completions:    3
Start Time:     Tue, 24 Jul 2018 02:42:33 -0400
Pods Statuses:  0 Running / 3 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=be5f8b69-8f0c-11e8-a791-020055e1ea1d
           job-name=testing-service
           name=testing-service
  Containers:
   counter:
    Image:      centos:7
    Port:       <none>
    Host Port:  <none>
    Command:
      bin/bash
      -c
      for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  1m    job-controller  Created pod: testing-service-strtz
  Normal  SuccessfulCreate  1m    job-controller  Created pod: testing-service-dpwr2
  Normal  SuccessfulCreate  1m    job-controller  Created pod: testing-service-7vrx5

We can check logs for each pod and we can see that the bash command ran as defined. If we set a for repeatable job, the pods run one after the other to perform a job defined until completion.

No comments :

Post a Comment