[파드 디자인 패턴과 잡(Job) 실행] CronJob을 활용한 예약 파드 생성 개념과 실습

hi·2023년 8월 19일
0

쿠버네티스

목록 보기
52/60

Cronjob

크롭잡은 반복 일정에 따라 잡 (완료를 목표로 실행되는 유한 또는 배치 작업)을 생성한다.

Cronjob 예약 시간 정하기

  • 예약 시간 작성 요령
    • 기존 리눅스 시스템의 크론에서 표기하는 방법과 동일
    • CronJob yaml 파일에는 예약 실행할 시간과 실행할 컨테이너를 작성
    • 일반적으로 CronJob 하나에 하나의 작업 실행 권장
    • 각각은 아래 내용을 의미
# ┌───────────── 분 (0 - 59)
# │ ┌───────────── 시 (0 - 23)
# │ │ ┌───────────── 일 (1 - 31)
# │ │ │ ┌───────────── 월 (1 - 12)
# │ │ │ │ ┌───────────── 요일 (0 - 6) (일요일부터 토요일까지;
# │ │ │ │ │                                   특정 시스템에서는 7도 일요일)
# │ │ │ │ │                                   또는 sun, mon, tue, wed, thu, fri, sat
# │ │ │ │ │
# * * * * * <command to execute>


동시성 정책 설정하기

  • spec.concurrencyPolicy는 동시성 정책 설정
  • 이미 하나의 크론잡이 실행 중인 경우 크론잡을 추가로 실행할지 결정
정책의미
Allow중복 실행을 허용 (기본값)
Forbid중복 실행을 금지
Replace현재 실행 중인 크론잡을 내리고 새로운 크론잡으로 대체


CronJob 예제 실행하기

  • CronJob 간단한 예제
# cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  concurrencyPolicy: Allow
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
내용설명
예약 실행 시간*/1****매분 컨테이너를 실행
컨테이너 이미지image:busybox:1.28busybox 이미지 사용
커맨드와 아규먼트args: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster매분마다 date 명령과 Hello from the Kubernetes cluster를 출력
concurrencyPolicyAllow동시 실행 가능

imkunyoung@cloudshell:~/job (kubernetes-397511)$ kubectl create -f cronjob.yaml
cronjob.batch/hello created
imkunyoung@cloudshell:~/job (kubernetes-397511)$ kubectl get pods -w
NAME                       READY   STATUS    RESTARTS   AGE
mariadb-5bfcbc8dd5-xfxbd   1/1     Running   0          44h
nginx-sidecar              3/3     Running   0          47h
hello-28242720-rcd7n       0/1     Pending   0          0s
hello-28242720-rcd7n       0/1     Pending   0          0s
hello-28242720-rcd7n       0/1     ContainerCreating   0          0s
hello-28242720-rcd7n       1/1     Running             0          1s
hello-28242720-rcd7n       0/1     Completed           0          2s
hello-28242720-rcd7n       0/1     Completed           0          3s
hello-28242720-rcd7n       0/1     Completed           0          4s
hello-28242720-rcd7n       0/1     Completed           0          4s
hello-28242721-mvpf2       0/1     Pending             0          0s
hello-28242721-mvpf2       0/1     Pending             0          0s
hello-28242721-mvpf2       0/1     ContainerCreating   0          0s
hello-28242721-mvpf2       0/1     Completed           0          1s
hello-28242721-mvpf2       0/1     Completed           0          2s
hello-28242721-mvpf2       0/1     Completed           0          3s
hello-28242721-mvpf2       0/1     Completed           0          3s

cronjob 삭제

imkunyoung@cloudshell:~/job (kubernetes-397511)$ kubectl get cronjob
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        39s             2m30s
imkunyoung@cloudshell:~/job (kubernetes-397511)$ kubectl delete cronjob hello
cronjob.batch "hello" deleted
imkunyoung@cloudshell:~/job (kubernetes-397511)$ kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
mariadb-5bfcbc8dd5-xfxbd   1/1     Running   0          44h
nginx-sidecar              3/3     Running   0          47h

0개의 댓글