[k8s] job,cronjob

Posted by Albert 106Day 18Hour 11Min 57Sec ago [2025-04-22]

1. JOB 

쿠버네티스에서 잡은 쿠버네티스 애플리케이션의 실행 및 종료에 초점을 맞춘 리소스유형

(잡을 활용하면 파드를 생성하고 지정된 수의 파드가 성공적으로 종료될때까지 계속해서 파드의 실행을 재시도하고, 지정된 수의 성공횟수가 완료횟수에 도달하면 잡이 종료됨)

albert@k8s-master:~/work/ch09/ex15$ vi job-cronjob01.yml
apiVersion: batch/v1
kind: Job
metadata:
name: job-test01
spec:
template:
spec:
containers:
- name: nginx-test01
image: nginx:1.25
command: ["echo", "Hello, Kubernetes!"]
restartPolicy: Never
backoffLimit: 3

nginx-test01xxx pod 생성 하고 job 실행시 "Hello, Kubernetes!" 메세지 출력한다

albert@k8s-master:~/work/ch09/ex15$ kubectl apply -f job-cronjob01.yml
job.batch/job-test01 created

albert
@k8s-master:~/work/ch09/ex15$ kubectl get job
NAME COMPLETIONS DURATION AGE
job-test01 1/1 3s 6s

albert
@k8s-master:~/work/ch09/ex15$ kubectl describe job job-test01
Name:
job-test01
Namespace:
default
Selector: batch.kubernetes.io/controller-uid=04568748-3a2d-4c39-8017-91dc12262073
Labels: batch.kubernetes.io/controller-uid=
04568748-3a2d-4c39-8017-91dc12262073
batch.kubernetes.io/
job-name=job-test01
controller-uid=
04568748-3a2d-4c39-8017-91dc12262073
job-name=job-test01
Annotations: <
none>
Parallelism:
1
Completions: 1
Completion Mode: NonIndexed
Start Time: Tue,
22 Apr 2025 04:30:02 +0000
Completed At: Tue, 22 Apr 2025 04:30:05 +0000
Duration: 3s
Pods Statuses:
0 Active (0 Ready) / 1 Succeeded / 0 Failed
Pod Template:
Labels: batch.kubernetes.io/controller-uid=
04568748-3a2d-4c39-8017-91dc12262073
batch.kubernetes.io/
job-name=job-test01
controller-uid=
04568748-3a2d-4c39-8017-91dc12262073
job-name=job-test01
Containers:
nginx-test01:
Image: nginx:
1.25
Port: <none>
Host Port: <
none>
Command:
echo
Hello, Kubernetes!
Environment: <
none>
Mounts: <
none>
Volumes: <
none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate
23s job-controller Created pod: job-test01-q6j27
Normal Completed
20s job-controller Job completed

albert
@k8s-master:~/work/ch09/ex15$ kubectl get pod
NAME READY STATUS RESTARTS
AGE
job-test01-q6j27 1/1 Completed 0 56s

albert
@k8s-master:~/work/ch09/ex15$ kubectl logs job-test01-q6j27
Hello, Kubernetes!


2. CRONJOB 

크론잡은 리눅스의 crontab처럼 반복적으로 잡을 만든다.

albert@k8s-master:~/work/ch09/ex15$ vi job-cronjob02.yml
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-test02
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: nginx-test02
image: nginx:1.25
command:
- /bin/sh
- -c
- echo Hello Kubernetes!
restartPolicy: Never

매1분마다 pod 생성하는 cronjob

albert@k8s-master:~/work/ch09/ex15$ kubectl apply -f job-cronjob02.yml
cronjob.batch/cronjob-test02 created

albert@k8s-master:~/work/ch09/ex15$ kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-test02 */1 * * * * False 0 <none> 6s


최초 확인시 아무런 파트도 생성되지않는다.

albert@k8s-master:~/work/ch09/ex15$ kubectl describe cronjob cronjob-test02
Name: cronjob-test02
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:
nginx-test02:
Image: nginx:1.25
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
echo Hello Kubernetes!
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: <unset>
Active Jobs: <none>
Events: <none>


1분후 확인시 신규 pod가 생성된거 확인 할 수 있다. 

albert@k8s-master:~/work/ch09/ex15$ kubectl describe cronjob cronjob-test02
Name: cronjob-test02
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:
nginx-test02:
Image: nginx:1.25
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
echo Hello Kubernetes!
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Tue, 22 Apr 2025 04:34:00 +0000
Active Jobs: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 25s cronjob-controller Created job cronjob-test02-29088274
Normal SawCompletedJob 22s cronjob-controller Saw completed job: cronjob-test02-29088274, status: Complete





LIST

Copyright © 2014 visionboy.me All Right Reserved.