DaemonSet, Job, CronJob

Controller with DatemonSet, Job, CronJob for Kubernetes.


1. DaemonSet


HostPort, NodeSelector with DaemonSet for Kubernetes.

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


Parrallelism, Completions with Job for Kubernetes.

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


Allow with CronJob for Kubernetes.

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 }}'



3-2) CronJob - ConcurrencyPolicy

ConcurencyPolicy with CronJob for Kubernetes.

CronJob

apiVersion: batch/v1beta1
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

ConcurencyPolicy 1.19 with CronJob for Kubernetes.


Referenece


Kubernetes