0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CKAD) Job、CronJob

Last updated at Posted at 2023-08-22

Job

  • k8sでBatchの実行するためのAPIを提供する
  • Batchを処理するPodは作業が完了になったら、終了する
  • Batch処理で適切なコントローラでPodの完了を保証する
    • Batch実行中にNGになったら、リランする
    • 正常終了時に完了

job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never # 必須
  backoffLimit: 4
  • pi というJobがpiというコンテナをJobとして動く
  • restartPolicyリランするかどうかを書いておく(必須)(初期値はNever)
  • backoffLimit 何回りランするのか書いておく(オプション)
  • activeDeadlineSeconds バッチの実行時間は何秒か設定する

実行結果

controlplane $ vi job.yaml
controlplane $ kubectl apply -f ./job.yaml 
job.batch/pi created
controlplane $ kubectl get pods
NAME       READY   STATUS              RESTARTS   AGE
pi-v6pmv   0/1     ContainerCreating   0          13s
controlplane $ kubectl get pods
NAME       READY   STATUS              RESTARTS   AGE
pi-v6pmv   0/1     ContainerCreating   0          18s
controlplane $ kubectl get pods
NAME       READY   STATUS              RESTARTS   AGE
pi-v6pmv   0/1     ContainerCreating   0          19s
controlplane $ kubectl get pods
NAME       READY   STATUS              RESTARTS   AGE
pi-v6pmv   0/1     ContainerCreating   0          20s
controlplane $ kubectl get pods
NAME       READY   STATUS              RESTARTS   AGE
pi-v6pmv   0/1     ContainerCreating   0          21s
controlplane $ kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
pi-v6pmv   1/1     Running   0          22s
controlplane $ kubectl get pods
NAME       READY   STATUS      RESTARTS   AGE
pi-v6pmv   0/1     Completed   0          30s
controlplane $ 
  • 実行が完了になったら、ステータスはCompletedになるが実行後のログを確認するために残す
  • 必要なければ、手動で削除する

CronJob

  • Jobコントローラで実行するApplications Podを周期的に繰り返しながら実行
  • LinuxのCronjobのスケジューリング機能をJobコントローラに追加したAPI
  • 次のような作業に適切
    • Data backup
    • データベースの更新
    • メールの転送
    • タスクの整理
  • Cronjob の時間の設定はLinuxCronjobと構成が同様
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │                                   OR sun, mon, tue, wed, thu, fri, sat
# │ │ │ │ │
# * * * * *

  • Jobを作成し、Jobの上にCronJobを追加、Jobを周期的に実行できる構成
(上記のJobをCronjobに修正)job.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: pi
spec:
  schedule: "* * * * *"
  jobTemplate: 
    spec:
      template:
        spec:
          containers:
          - name: pi
            image: perl:5.34.0
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: Never # 必須
      backoffLimit: 4

実行結果

controlplane $ kubectl get pods
NAME                READY   STATUS              RESTARTS   AGE
pi-28211988-k45wc   0/1     Completed           0          73s
pi-28211989-gh5r4   0/1     ContainerCreating   0          13s
pi-v6pmv            0/1     Completed           0          19m
  • 1分ごとにCronjobが実行されることを確認する
controlplane $ kubectl delete cronjobs.batch pi
cronjob.batch "pi" deleted
  • cronjob piを削除する
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?