Job 이란?
- Kubernetes는 Pod를 running 중인 상태로 유지
- Batch 처리하는 Pod는 작업이 완료되면 종료됨
- Batch 처리에 적합한 컨트롤러로 Pod의 성공적인 완료를 보장
. 비정상 종료 시 다시 실행
. 정상 종료 시 완료
kubectl run testpod --image=centos:7 --command sleep 5 명령을 통해서 5초 후 종료 및 Running 으로 변경되는것을 확인
"쿠버네티스는 pod의 running 중인 상태를 보장"
# kubectl run testpod --image=centos:7 --command sleep 5
pod/testpod created
# kubectl get pods --watch
NAME READY STATUS RESTARTS AGE
testpod 0/1 Pending 0 1s
testpod 0/1 Pending 0 1s
testpod 0/1 ContainerCreating 0 1s
testpod 1/1 Running 0 2s
testpod 0/1 Completed 0 8s
testpod 1/1 Running 1 (2s ago) 9s
testpod 0/1 Completed 1 (7s ago) 14s
testpod 0/1 CrashLoopBackOff 1 (15s ago) 28s
testpod 1/1 Running 2 (16s ago) 29s
testpod 0/1 Completed 2 (21s ago) 34s
testpod 0/1 CrashLoopBackOff 2 (12s ago) 46s
job-exam.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
# activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 25; echo 'Bye'"
restartPolicy: Never
# restartPolicy: OnFailure
# backoffLimit: 3
job-exam.yaml 실행 후 job이 완료되기 전에 pod를 삭제하면 새로운 pod가 생성되어 job을 완료
# kubectl create -f job-exam.yaml
job.batch/centos-job created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-44z52 1/1 Running 0 4s 10.44.0.1 node2.example.com <none> <none>
# kubectl delete pod centos-job-r5hk8
pod "centos-job-r5hk8" deleted
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-bwlvx 1/1 Running 0 13s 10.36.0.1 node1.example.com <none> <none>
centos-job-k628l 1/1 Terminating 0 25s 10.44.0.1 node2.example.com <none> <none>
# kubectl get jobs
NAME COMPLETIONS DURATION AGE
centos-job 1/1 41s 68s
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-bwlvx 0/1 Completed 0 68s 10.36.0.1 node1.example.com <none> <none>
job 종료 방법
# kubectl delete jobs.batch centos-job
job.batch "centos-job" deleted
restartPolicy: OnFailure (컨테이너가 비정상 종료되었을 경우 컨테이너를 다시 실행)
backoffLimit: 3 (3번까지 실행, default 는 6)
임의로 에러가 나올수 있게 command에 잘못된 "bashc" 입력
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
# activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bashc"]
args:
- "-c"
- "echo 'Hello World'; sleep 50; echo 'Bye'"
# restartPolicy: Never
restartPolicy: OnFailure
backoffLimit: 3
RunContainerError 발생 후 CrashLoopBackoff 상태에서 RESTARTS 3번까지 진행하면 컨테이너가 삭제됨
# kubectl create -f job-exam.yaml
job.batch/centos-job created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-glmcp 0/1 RunContainerError 0 (1s ago) 2s 10.36.0.1 node1.example.com <none> <none>
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-glmcp 0/1 CrashLoopBackOff 1 (2s ago) 4s 10.36.0.1 node1.example.com <none> <none>
No resources found in default namespace.
restartPolicy: Never 를 이용하고 command에 잘못된 "bashc" 입력
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
# activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bashc"]
args:
- "-c"
- "echo 'Hello World'; sleep 25; echo 'Bye'"
restartPolicy: Never
# restartPolicy: OnFailure
# backoffLimit: 3
pod가 계속 다시 시작
# kubectl create -f job-exam.yaml
job.batch/centos-job created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-gzlp7 0/1 StartError 0 42s 10.44.0.1 node2.example.com <none> <none>
centos-job-jdhr7 0/1 StartError 0 46s 10.44.0.1 node2.example.com <none> <none>
centos-job-lzz4w 0/1 StartError 0 49s 10.44.0.1 node2.example.com <none> <none>
centos-job-nt6s8 0/1 StartError 0 38s 10.36.0.1 node1.example.com <none> <none>
centos-job-pl7qm 0/1 StartError 0 30s 10.44.0.1 node2.example.com <none> <none>
centos-job-pp6qn 0/1 StartError 0 26s 10.36.0.1 node1.example.com <none> <none>
centos-job-vhdzk 0/1 StartError 0 34s 10.36.0.1 node1.example.com <none> <none>
completions: 3 는 컨테이너를 순차적으로 3번 실행하겠다는 의미
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
completions: 3
# parallelism: 2
# activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 5; echo 'Bye'"
restartPolicy: Never
# restartPolicy: OnFailure
# backoffLimit: 3
동시에 Running 되어지지는 않음
# kubectl create -f job-exam.yaml
job.batch/centos-job created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-6ntb6 0/1 Completed 0 27s 10.44.0.1 node2.example.com <none> <none>
centos-job-chbbr 0/1 Completed 0 17s 10.44.0.1 node2.example.com <none> <none>
centos-job-g984w 1/1 Running 0 7s 10.36.0.1 node1.example.com <none> <none>
parallelism: 2 는 Running 중인 컨테이너를 2개까지 보장
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
completions: 5
parallelism: 2
# activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 5; echo 'Bye'"
restartPolicy: Never
# restartPolicy: OnFailure
# backoffLimit: 3
컨테이너가 2개씩 실행되는것을 확인
# kubectl create -f job-exam.yaml
job.batch/centos-job created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-4kxvn 0/1 Completed 0 13s 10.44.0.1 node2.example.com <none> <none>
centos-job-6chdf 0/1 Completed 0 13s 10.36.0.1 node1.example.com <none> <none>
centos-job-6wpzd 1/1 Running 0 2s 10.44.0.1 node2.example.com <none> <none>
centos-job-snb8l 1/1 Running 0 3s 10.36.0.1 node1.example.com <none> <none>
activeDeadlineSeconds: 5 는 어플리케이션이 5초 안에 끝나지 않으면 종료하겠다는 의미
(특정 어플리케이션이 오류로 인해서 너무 긴 시간을 돌아가는것을 방지 하기 위함)
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
activeDeadlineSeconds: 5
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 25; echo 'Bye'"
restartPolicy: Never
# restartPolicy: OnFailure
# backoffLimit: 3
5초 후 Terminating 되는것 확인 가능
# kubectl create -f job-exam.yaml
job.batch/centos-job created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-5zsw2 1/1 Running 0 3s 10.36.0.1 node1.example.com <none> <none>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-job-5zsw2 1/1 Terminating 0 5s 10.36.0.1 node1.example.com <none> <none>
[참고]
- https://www.youtube.com/watch?v=AxplqT55Kdg&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=22
'Kubernetes' 카테고리의 다른 글
[Kubernetes] Service 개념과 종류 (0) | 2022.12.13 |
---|---|
[Kubernetes] CronJob (0) | 2022.12.13 |
[Kubernetes] StatefulSet (0) | 2022.12.12 |
[Kubernetes] DaemonSet + RollingUpdate (1) | 2022.12.12 |
[Kubernetes] RollingUpdate를 위한 Deployment (0) | 2022.12.11 |