DaemonSet, Job, CronJob
1. DaemonSet
1-1) DaemonSet - HostPort
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-1
spec:
selector:
matchLabels:
type: app
template:
metadata:
labels:
type: app
spec:
containers:
- name: container
image: kubetm/app
ports:
- containerPort: 8080
hostPort: 18080
Command
curl 192.168.56.31:18080/hostname
curl 192.168.0.31:18080/hostname
1-2) DaemonSet - NodeSelector
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-2
spec:
selector:
matchLabels:
type: app
template:
metadata:
labels:
type: app
spec:
nodeSelector:
os: centos
containers:
- name: container
image: kubetm/app
ports:
- containerPort: 8080
Label Add
kubectl label nodes k8s-node1 os=centos
kubectl label nodes k8s-node2 os=ubuntu
Label Remove
kubectl label nodes k8s-node2 os-
2. Job
2-1) Job1
apiVersion: batch/v1
kind: Job
metadata:
name: job-1
spec:
template:
spec:
restartPolicy: Never
containers:
- name: container
image: kubetm/init
command: ["sh", "-c", "echo 'job start';sleep 20; echo 'job end'"]
terminationGracePeriodSeconds: 0
2-2) Job2
apiVersion: batch/v1
kind: Job
metadata:
name: job-2
spec:
completions: 6
parallelism: 2
activeDeadlineSeconds: 30
template:
spec:
restartPolicy: Never
containers:
- name: container
image: kubetm/init
command: ["sh", "-c", "echo 'job start';sleep 20; echo 'job end'"]
terminationGracePeriodSeconds: 0
3. CronJob
3-1) CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: cron-job
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: Never
containers:
- name: container
image: kubetm/init
command: ["sh", "-c", "echo 'job start';sleep 20; echo 'job end'"]
terminationGracePeriodSeconds: 0
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cron-job
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: Never
containers:
- name: container
image: kubetm/init
command: ["sh", "-c", "echo 'job start';sleep 20; echo 'job end'"]
terminationGracePeriodSeconds: 0
Manual
kubectl create job --from=cronjob/cron-job cron-job-manual-001
Suspend
kubectl patch cronjobs cron-job -p '{"spec" : {"suspend" : false }}'
1.19버전 이후 변경사항
Cronjob 삭제시 Manual로 만든 Job도 같이 삭제됨3-2) CronJob - ConcurrencyPolicy
CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: cron-job-2
spec:
schedule: "20,21,22 * * * *"
concurrencyPolicy: Replace
jobTemplate:
spec:
template:
spec:
restartPolicy: Never
containers:
- name: container
image: kubetm/init
command: ["sh", "-c", "echo 'job start';sleep 140; echo 'job end'"]
terminationGracePeriodSeconds: 0
1.19버전 이후 변경사항
Replace 모드 : 2min이 되었을 시 기존 Job은 삭제되고 (기존 Pod도 같이 삭제됨), 새 Job이(새 pod 생성) 만들어집니다.Referenece
Kubernetes
- Jobs : https://kubernetes.io/docs/concepts/workloads/controllers/job/
- CronJob : https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
- Running Automated Tasks with a CronJob : https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/