다음은 쿠버네티스 스터디 자료를 참고해 정리한 내용입니다.
<1> job = 단일로 수행하는 것
<2> cronjob = 일정 주기로 실행
<3> 삭제
<4> 정리
<5> 다음 과정
<1> job = 단일로 수행하는 것
1
특정 동작을 수행하고 종료하는 작업용 오브젝트
2
접속,정리
ssh -i ~/.ssh/id_rsa ubuntu@api.k8s.serverchk.com
kubectl delete pod --all
3
파이 소수점 계산하기
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
3
터미널2에서 모니터링
watch -d 'kubectl get pods,jobs -o wide'
4
kubectl apply -f job.yaml && kubectl get pods -w
ubuntu@ip-172-20-41-255:~$ kubectl apply -f job.yaml && kubectl get pods -w
job.batch/pi created
NAME READY STATUS RESTARTS AGE
pi-pw6tf 0/1 ContainerCreating 0 0s
pi-pw6tf 0/1 ContainerCreating 0 0s
pi-pw6tf 1/1 Running 0 19s
pi-pw6tf 0/1 Completed 0 23s
pi-pw6tf 0/1 Completed 0 23s
모니터링
Every 2.0s: kubectl get pods,jobs -o wide ip-172-20-41-255: Fri Jul 2 04:48:46 2021
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pi-pw6tf 0/1 Completed 0 54s 100.98.158.185 ip-172-20-38-144.ap-northeast-1.compute.internal <none> <none>
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
job.batch/pi 1/1 23s 54s pi perl controller-uid=63096c24-5805-4e50-bb53-0cf0e060cc72
5
결과 log로 확인
kubectl logs -l job-name=pi
// 3.141592 ....... 파이에 대한 값이 나온다.
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218
6
kubectl get jobs.batch pi -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pi 1/1 23s 110s pi perl controller-uid=63096c24-5805-4e50-bb53-0cf0e06
상세
kubectl describe jobs.batch pi
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 2m23s job-controller Created pod: pi-pw6tf
Normal Completed 2m job-controller Job completed
7
pod name 으로 조회 pi-pw6tf
kubectl get pod pi-pw6tf
NAME READY STATUS RESTARTS AGE
pi-pw6tf 0/1 Completed 0 5m28s
8
kubectl describe pod pi-pw6tf
ubuntu@ip-172-20-41-255:~$ kubectl describe pod pi-pw6tf
Name: pi-pw6tf
Namespace: default
Priority: 0
Node: ip-172-20-38-144.ap-northeast-1.compute.internal/172.20.38.144
Start Time: Fri, 02 Jul 2021 04:47:52 +0000
Labels: controller-uid=63096c24-5805-4e50-bb53-0cf0e060cc72
job-name=pi
Annotations: cni.projectcalico.org/podIP:
cni.projectcalico.org/podIPs:
kubernetes.io/limit-ranger: LimitRanger plugin set: cpu request for container pi
Status: Succeeded
IP: 100.98.158.185
:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 6m2s default-scheduler Successfully assigned default/pi-pw6tf to ip-172-20-38-144.ap-northeast-1.compute.internal
Normal Pulling 6m2s kubelet Pulling image "perl"
Normal Pulled 5m45s kubelet Successfully pulled image "perl" in 16.323738814s
Normal Created 5m44s kubelet Created container pi
Normal Started 5m44s kubelet Started container pi
kubectl describe pod
9
한번 더 실행하면?
kubectl apply -f https://kubernetes.io/examples/controllers/job.yaml
ubuntu@ip-172-20-41-255:~$ kubectl apply -f https://kubernetes.io/examples/controllers/job.yaml
job.batch/pi unchanged
// 변화 없다.
Pod가 이미 셀렉터를 가지고 있는 레이블 값은 Pos가 가지고 있다.
이미 끝난 값이라 변화가 없다.
kubectl get jobs.batch pi -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pi 1/1 23s 110s pi perl controller-uid=63096c24-5805-4e50-bb53-0cf0e06
10
kubectl delete -f https://kubernetes.io/examples/controllers/job.yaml
<2> cronjob = 일정 주기로 실행
1
현재 시간, hello를 1분 마다 실행해 보자
date 시간을 기준으로 동작 한다.
date
Tue Oct 5 07:43:40 UTC 2021
2
clear
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
3
kubectl apply -f cronjob.yaml && kubectl get jobs --watch
error: unable to recognize "cronjob.yaml": no matches for kind "CronJob" in version "batch/v1"
4
kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-1625202360-vbf57 0/1 Completed 0 24s
모니터링
Every 2.0s: kubectl get pods,jobs -o wide ip-172-20-41-255: Fri Jul 2 05:06:43 2021
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/hello-1625202360-vbf57 0/1 Completed 0 35s 100.98.158.181 ip-172-20-38-144.ap-northeast-1.compute.internal <none> <none>
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
job.batch/hello-1625202360 1/1 1s 35s hello busybox controller-uid=9ddac269-bbcd-47fd-a74a-0bf57f668776
5
kubectl get cronjobs.batch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 56s 94s
6
kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
hello-1625202360 1/1 1s 71s
hello-1625202420 1/1 1s 11s
7
watch -n 1 "kubectl logs -l job-name"
Fri Jul 2 05:06:09 UTC 2021
Hello from the DKOS Member!
Fri Jul 2 05:07:09 UTC 2021
Hello from the DKOS Member!
8
결과 확인 !!!
kubectl logs -l job-name
Fri Jul 2 05:06:09 UTC 2021
Hello from the DKOS Member!
Fri Jul 2 05:07:09 UTC 2021
Hello from the DKOS Member!
// 1분 마다 1번씩 찍힘!!
<3> 삭제
kubectl delete -f cronjob.yaml
cronjob.batch "hello" deleted
<4> 정리
1
job = 단일로 수행하는 것
2
cronjob = 일정 주기로 실행
<5> 다음 과정
https://brunch.co.kr/@topasvga/1681
https://brunch.co.kr/@topasvga/1656
감사합니다.