Job Controller
- Kubernetes는 Pod를 running 중인 상태로 유지
- Batch 처리하는 pod는 작업이 완료되면 종료됨
- Batch 처리에 적합한 컨트롤러로 pod의 성공적인 완료를 보장
- 비정상 종료 시 다시 실행
- 정상 종료 시 완료
- 쿠버네티스가 pod를 동작시키는 기본원리
- kubectl run testpod --image=centos:7 --command sleep 5
- contos7컨테이너로 pod 생성하면서 sleep 5 수행
- kubectl get pod --watch로 모니터링 해보자
- running중인 pod 보장해주는게 쿠버네티스의 가장 기본적인 동작 방식
- 오류가 있거나 종료가 된 pod를 재실행 시켜줌
- 그던데 항상 running 중일 필요가 없는 pod가 존재한다.
- 백업 같은 경우 백업이 완료되면 끝이다. 또 실행할 필요가 없다.
- 이런 경우를 위해 Job Controller가 있다.
Job definition
Job defintion |
apiVersion: batch/v1 kind: Job metadata: name: job-example spec: template: spec: containers: - name: centos-container image: centos:7 command: ["bash"] args: - "-c" - "echo 'Hello World'; sleep 50; echo 'Bye'" restartPolicy: Never |
- 여기서는 centos로 했지만 실제로는 백업을 한다던지 쓰레기 데이터를 클리어 해준다던지 배치 작업을 해줘야하는 애플리케이션 컨테이너임
- 실행 후 완료되면 그걸로 끝, 비정상적으로 종료되면 리스타트
- restartPolicy: container의 재실행 여부를 판단하며 always, Never, OnFailure가 있다.
- always : 항상 재시작, 디폴트값
- Never : 재시작을 하지 않음
- OnFailure : 비정상 종료 발생 시 컨테이너를 재시작
- backoffLimit: job의 실패 관계에 따라 몇번 pod를 재실행 할 것인지를 결정,기본값은 6이다
- job controller: job이 실패하면 재실행, 성공하면 complete를 결정
- restartPolicy는 container만 관장하며, restartPolicy:Never일 경우 restartPolicy:Never가 pod를 재실행 하는 것이 아니라, Never이기 떄문에 pod안에 job을 에러 상태로 가만히 유지합니다. 따라서 job이 실패 상태이기 때문에 job controller에 의해 파드 재생성을 시도하게 됩니다. backoffLimit은 job의 실패여부를 보고 실패시 지정된 값만큼 pod를 재생성 시도하게 됩니다.
Job example(1)
$cat job-exam.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
# activeDeadlineSeconds: 15
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 50; echo 'Bye'"
restartPolicy: Never
- completions : 실행해야 할 job의 수가 몇 개인지 지정
- parallelism : 별령성, 동시 running 되는 pod 수
- activeDeadlineSeconds : 지정 시간 내에 Job을 완료
TEST
Job example(2)
$cat job-exam.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
# activeDeadlineSeconds: 15
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
- command: ["bashc"] : bashc라는 커맨드는 없기 때문에 오류가 발생
- restartPolicy: OnFailure - 컨테이너가 비정상 종료되었다면 backoffLimit 횟수만큼 컨테이너를 다시 시작, 횟수 넘으면 삭제
- backoffLimit: 3 - 3번까지 재시작. 디폴트는 6번
TEST
Job example(3)
$cat job-exam.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
# completions: 5
# parallelism: 2
# activeDeadlineSeconds: 15
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bashc"]
args:
- "-c"
- "echo 'Hello World'; sleep 50; echo 'Bye'"
restartPolicy: Never
- 이번에는 에러가 발생하게 한 후 restartPolicy를 Never로 변경
- OnFailure과 어떤 차이가 있는지 확인하자.
TEST
- OnFailure과는 다르게 pod가 재생성되고 있다.
- Never는 컨테이너를 재시작시키지 않기 때문에 pod 자체를 재생성하고 있다.
- Job은 오류난 pod를 재시작하는게 목적이기 때문에 컨테이너를 재시작 할 수 없다 pod를 새로 생성하여 작업을 마칠려고 하는 것
- 애플리케이션 성향에 따라 골라쓰자
- kubectl edit job centos-job로 command를 수정해볼려고 했으나 에러가 발생했다.
- 이건 나중에 알아보자.
Job example(4)
$cat job-exam.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
completions: 3
# parallelism: 2
# activeDeadlineSeconds: 15
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 5; echo 'Bye'"
restartPolicy: Never
- completions : replicas 포지션의 옵션, 3이면 컨테이너를 세번 실행
TEST
Job example(5)
$cat job-exam.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: centos-job
spec:
completions: 5
parallelism: 2
# activeDeadlineSeconds: 15
template:
spec:
containers:
- name: centos-container
image: centos:7
command: ["bash"]
args:
- "-c"
- "echo 'Hello World'; sleep 5; echo 'Bye'"
restartPolicy: Never
- parallelism : 병렬 운영. running 중인걸 2개까지 유지
TEST
Job example(6)
$cat 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
- activeDeadlineSeconds : 동작되는 애플리케이션이 끝나야하는 시간. 초 단위이며 5면 5초 안에 안끝날지 강제 컴플리트 시킴
- 특정 애플리케이션이 오류가 발생해서 긴 시간 보존되는걸 방지할 수 있다.
- 5초 설정, 애플리케이션은 25초로 바꾸고 테스트 해보자
TEST