<1> job = 단일로 수행하는 것
<2> cronjob = 일정 주기로 실행
<1> job = 단일로 수행하는 것
1
topasvga@cloudshell:~ (ap-seoul-1)$ cat << EOF > job.yaml
> apiVersion: batch/v1
> kind: Job
> metadata:
> name: pi
> spec:
> template:
> spec:
> containers:
> - name: pi
> image: perl
> command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
> restartPolicy: Never
> backoffLimit: 4
> EOF
2
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl apply -f job.yaml && kubectl get pods -w
job.batch/pi created
NAME READY STATUS RESTARTS AGE
pi-62mft 0/1 ContainerCreating 0 2s
pi-62mft 1/1 Running 0 26s
// 이 버전에서는 잘 안됨.
3
kubectl get pods,jobs -o wide
4
결과 확인
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl logs -l job-name=pi
5
다시 시도
topasvga@cloudshell:~ (ap-seoul-1)$ rm -rf *
6
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl delete -f https://kubernetes.io/examples/controllers/job.yaml
job.batch "pi" deleted
7
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl apply -f https://kubernetes.io/examples/controllers/job.yaml
job.batch/pi created
8
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl describe jobs.batch pi
Name: pi
Namespace: default
Selector: controller-uid=1db222cd-6292-467a-8fad-dbc5abe6032f
Labels: controller-uid=1db222cd-6292-467a-8fad-dbc5abe6032f
job-name=pi
Annotations: batch.kubernetes.io/job-tracking:
Parallelism: 1
Completions: 1
Completion Mode: NonIndexed
Start Time: Tue, 21 Feb 2023 07:58:35 +0000
Pods Statuses: 1 Active (0 Ready) / 0 Succeeded / 0 Failed
Pod Template:
Labels: controller-uid=1db222cd-6292-467a-8fad-dbc5abe6032f
job-name=pi
Containers:
pi:
Image: perl:5.34.0
Port: <none>
Host Port: <none>
Command:
perl
-Mbignum=bpi
-wle
print bpi(2000)
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 42s job-controller Created pod: pi-2wfrq
9
로그에서 결과 확인
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl logs -l job-name=pi
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
topasvga@cloudshell:~ (ap-seoul-1)$
10
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get jobs.batch pi -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pi 1/1 53s 4m28s pi perl:5.34.0 controller-uid=1db222cd-6292-467a-8fad-dbc5abe6032f
11
삭제
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl delete -f https://kubernetes.io/examples/controllers/job.yaml
job.batch "pi" deleted
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get jobs.batch pi -o wide
Error from server (NotFound): jobs.batch "pi" not found
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl get pods
No resources found in default namespace.
<2> cronjob = 일정 주기로 실행
// 쿠버네티스 신규 버전에서는 안됨. yaml 수정 필요
1
topasvga@cloudshell:~ (ap-seoul-1)$ cat << EOF > cronjob.yaml
> apiVersion: batch/v1beta1
> kind: CronJob
> metadata:
> name: hello
> spec:
> schedule: "*/1 * * * *" # 매 1분마다 실행
> successfulJobsHistoryLimit: 10 # 성공 Job 10개 까지 기록
> jobTemplate: # 실행될 Job 내용
> spec:
> template:
> spec:
> containers:
> - name: hello
> image: busybox
> imagePullPolicy: IfNotPresent
> command:
> - /bin/sh
> - -c
> - date; echo Hello from the DKOS Member!
> restartPolicy: OnFailure
> EOF
2
topasvga@cloudshell:~ (ap-seoul-1)$ kubectl apply -f cronjob.yaml && kubectl get jobs --watch
error: resource mapping not found for name: "hello" namespace: "" from "cronjob.yaml": no matches for kind "CronJob" in version "batch/v1beta1"
ensure CRDs are installed first
3
삭제
kubectl delete deploy,svc,pods --all
감사합니다.