Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Info
iconfalse
title목차

Table of Contents

Cron Job개요

Info
iconfalse
  • 기존 리눅스 시스템의 Crontab과 유사한 동작, 동일한 크론 표기법을 사용
  • CronJob하나에 하나의 작업 실행 권장


Info
iconfalse
titlehttps://kubernetes.io/ko/docs/concepts/workloads/controllers/cron-jobs/


CronJob Manifest

Info
iconfalse

https://dev-k8sref-io.web.app/docs/workloads/cronjob-v1beta1/

Info
iconfalse
  • apiVersion: batch/v1beta1
  • kind: CronJob

  • metadata (ObjectMeta)
  • spec (CronJobSpec)
    • schedule                      #실행 스케줄 설정 Linux Crontab정책과 동일
    • concurrencyPolicy   #동시 실행 가능
    • jobTemplate
  • status (CronJobStatus)
Info
  • 이미 하나의 CronJob이 실행중인 경우 CronJob을 추가로 실행할지 결정
  • 동시성 정책 설정하기
    • spec.concurrencyPolicy
      • Allow: 중복 실행을 허용(기본값)
      • Forbid: 중복 실행을 금지
      • Replace: 현재 실행중인 CronJob을 내리고 새로운 CronJob으로 대체

Code Block
titlecronjob.yaml
linenumberstrue
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello-cronjob
spec:
  schedule: "*/1 * * * *" # 매분마다 실행
  concurrencyPolicy: Allow # 동시 실행 가능
  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


CronJob 실습

Info
iconfalse


Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k apply -f cronjob.yaml 
cronjob.batch/hello-cronjob created
sansae@sansaeAir15m2 k8s-lab-workspace % k get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   7s

NAME                          SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
cronjob.batch/hello-cronjob   */1 * * * *   False     0        <none>          4s


sansae@sansaeAir15m2 k8s-lab-workspace % k get pod
NAME                           READY   STATUS      RESTARTS   AGE
hello-cronjob-28499961-hbpgl   0/1     Completed   0          119s
hello-cronjob-28499962-rtts5   0/1     Completed   0          59s


sansae@sansaeAir15m2 k8s-lab-workspace % k describe cronjob hello-cronjob 
Name:                          hello-cronjob
Namespace:                     default
Labels:                        <none>
Annotations:                   <none>
Schedule:                      */1 * * * *
Concurrency Policy:            Allow
Suspend:                       False
Successful Job History Limit:  3
Failed Job History Limit:      1
Starting Deadline Seconds:     <unset>
Selector:                      <unset>
Parallelism:                   <unset>
Completions:                   <unset>
Pod Template:
  Labels:  <none>
  Containers:
   hello:
    Image:      busybox:1.28
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
      -c
      date; echo Hello from the Kubernetes cluster
    Environment:     <none>
    Mounts:          <none>
  Volumes:           <none>
Last Schedule Time:  Sun, 10 Mar 2024 00:23:00 +0900
Active Jobs:         <none>
Events:
  Type    Reason            Age    From                Message
  ----    ------            ----   ----                -------
  Normal  SuccessfulCreate  2m26s  cronjob-controller  Created job hello-cronjob-28499961
  Normal  SawCompletedJob   2m19s  cronjob-controller  Saw completed job: hello-cronjob-28499961, status: Complete
  Normal  SuccessfulCreate  86s    cronjob-controller  Created job hello-cronjob-28499962
  Normal  SawCompletedJob   83s    cronjob-controller  Saw completed job: hello-cronjob-28499962, status: Complete
  Normal  SuccessfulCreate  26s    cronjob-controller  Created job hello-cronjob-28499963
  Normal  SawCompletedJob   22s    cronjob-controller  Saw completed job: hello-cronjob-28499963, status: Complete
Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k get pod
NAME                           READY   STATUS      RESTARTS   AGE
hello-cronjob-28504263-m4dkp   0/1     Completed   0          70s
hello-cronjob-28504264-2w5zp   0/1     Completed   0          10s


sansae@sansaeAir15m2 k8s-lab-workspace % k logs hello-cronjob-28504263-m4dkp 
Tue Mar 12 15:03:05 UTC 2024
Hello from the Kubernetes cluster