Actions

Kubernetes/Jobs,CronJob

From Ever changing code

< Kubernetes

Experiments with jobs.

CLI

# Delete completed jobs
kubectl.exe -n dev delete job $(kubectl.exe -n dev get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}')


Self terminating jobs-selfTerminate.yml one time off Jobs. Includes a CronJob task, that's manage a pod with RBAC permissions to remove old jobs.

---
apiVersion: batch/v1
kind: Job
metadata:
  name: job-ttl-zero
  namespace: dev
spec:
#  ttlSecondsAfterFinished: 100 # requires enabling TTLAfterFinished on min v1.12
  ttlSecondsAfterFinished: 0    # delete straight after completion
  template:
    spec:
      containers:
      - name: perl
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
---
apiVersion: batch/v1
kind: Job
metadata:
  name: job-activedeadlinedeconds
  namespace: dev
spec:
  backoffLimit: 2
  activeDeadlineSeconds: 120
  template:
    spec:
      containers:
      - name: perl
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cron-delete-jobs
  namespace: dev
spec:
  schedule: "*/2 * * * *"
  successfulJobsHistoryLimit: 2
  failedJobsHistoryLimit: 2
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: kubectl-runner
            image: wernight/kubectl
            command: ["sh", "-c", "kubectl -n dev get jobs | awk '$4 ~ /[2-9]d$/ || $3 ~ 1' | awk '{print $1}' | xargs kubectl -n dev delete job"]
          restartPolicy: Never
          serviceAccountName: delete-jobs
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: delete-jobs
  namespace: dev
  labels:
    k8s-app: delete-jobs
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: delete-jobs
subjects:
- kind: ServiceAccount
  name: delete-jobs
  namespace: dev
roleRef:
  kind: ClusterRole
  name: delete-jobs
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: delete-jobs
  labels:
    k8s-app: delete-jobs
rules:
- apiGroups: [""]
  resources:
#  - namespaces
  - pods
  verbs: ["list","delete"]
- apiGroups: ["batch"]
  resources:
  - jobs
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]


When applied to minikube 1.15.2

kubectl.exe apply -f jobs-selfTerminate.yml
job.batch/job-ttl-zero created
job.batch/job-activedeadlinedeconds created
cronjob.batch/cron-delete-jobs created
serviceaccount/delete-jobs created
clusterrolebinding.rbac.authorization.k8s.io/delete-jobs created
clusterrole.rbac.authorization.k8s.io/delete-jobs created

NAME                                    COMPLETIONS   DURATION   AGE
job.batch/cron-delete-jobs-1568245320   1/1           8s         2m1s
job.batch/cron-delete-jobs-1568245440   0/1           1s         1s

NAME                             SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
cronjob.batch/cron-delete-jobs   */2 * * * *   False     1        2s              8m5s

Resources