Kubernetes

[Kubernetes] 완료된 잡 정리(ttlSecondsAfterFinished)

망원동똑똑이 2025. 10. 6. 22:05

Job 리소스는 실행이 완료되어도 그 Job 과 Pod 가 자동으로 삭제되지 않는다. 이를 정리하는 옵션이 바로 ttlSecondsAfterFinished 이다. ttlSecondsAfterFinished 를 원하는 만큼 초 단위로 지정하면, Job 실행이 완료되 이후 해당 초가 지나면 Job 과 Pod 가 삭제된다.

apiVersion: batch/v1
kind: Job
metadata:
  name: job-ttl
spec:
  ttlSecondsAfterFinished: 30
  completions: 1
  parallelism: 1
  backoffLimit: 10
  template:
    spec:
      containers:
        - name: sleep-container
          image: centos:7
          command: ["sleep"]
          args: ["60"]
      restartPolicy: Never

 

위의 매니페스트로 Job 을 실행하면 60초 동안 Pod 하나가 실행되고 완료된 후, 30초 후에 Job 과 Pod 가 삭제되는 것을 볼 수 있다.

$ kubectl apply -f sample-job-ttl.yaml

$ kubectl get jobs
NAME                  STATUS     COMPLETIONS   DURATION   AGE
job-ttl               Running    0/1           4s         4s

# 30초 동안 실행되고 종료되는 Pod
$ kubectl get pods
NAME                        READY   STATUS      RESTARTS   AGE
job-ttl-kcv8n               1/1     Running     0          7s

# Job 의 상태가 변경되는 것을 출력
$ kubectl get job job-ttl --watch --output-watch-events
EVENT      NAME      STATUS    COMPLETIONS   DURATION   AGE
ADDED      job-ttl   Running   0/1           36s        36s
MODIFIED   job-ttl   Running   0/1           62s        62s
MODIFIED   job-ttl   Running   0/1           63s        63s
MODIFIED   job-ttl   Complete   1/1           63s        63s
MODIFIED   job-ttl   Complete   1/1           63s        93s
DELETED    job-ttl   Complete   1/1           63s        93s # Job 이 삭제된

# Pod 삭제 확인
$ kubectl get pods
No resources found in default namespace.