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?

k8s cronjobについて

Posted at

概要

k8s cronjobについて勉強したのでわかったことについてまとめます

cronjob

k8sで特定の処理を定期的に実行するのに使用されます

[1]で紹介されていたcronjob manifestを以下に示します。
以下の設定をapplyすると毎分おきに現在時刻とhelloのメッセージを表示する処理が行われます

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

上記の設定に様々なオプションの設定が行えます

startingDeadlineSeconds

このオプションを使ってジョブが開始されてから何秒までに終了させるのかを設定できます。これにより何らかの原因でジョブが正常に実行できなかった場合に無限ループを防ぐことが可能となります。
指定時間を過ぎた場合k8s側でジョブが失敗されたと処理されます。

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  startingDeadlineSeconds: 30 # 30秒後にはジョブが終了する
  
  jobTemplate:
  (省略)

concurrencyPolicy

このオプションを使ってジョブの実行を同時並行で行うかどうかを設定できます。デフォルトはAllowとなっており、forbidを指定すると無効化できる。
下記の例ではReplaceとなっており、この場合新しいジョブを実行する際に以前のジョブが終了していない場合、以前のジョブを終了してから新しいジョブを実行するようになります

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  startingDeadlineSeconds: 30 
  concurrencyPolicy: Replace
  
  jobTemplate:
  (省略)

suspend

このオプションを有効化するとジョブの中断が可能となる.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  startingDeadlineSeconds: 30 
  concurrencyPolicy: Replace

  suspend: false # ここでは中断を無効化している
  
  jobTemplate:
  (省略)

参考

[1] https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

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?