Kubernetes, Docker, and Cron

Nate Baker
Mar 29, 2018 · 3 min read

Docker + Cron

## output 'hello' to stdout/stderr
* * * * * root echo hello > /proc/1/fd/1 2>/proc/1/fd/2
$ docker run -v /path/to/my/Cronfiles:/etc/cron.d bambash/docker-cron
hello
hello
hello

Kubernetes + Docker + Cron

Use Case #1

# kubectl create configmap cronfiles --from-file Cronfiles/
configmap "cronfiles" created
# kubectl apply -f https://raw.githubusercontent.com/bambash/kube-cron-example/master/deployment.yaml
# kubectl logs -f cron-7cf5bd99b7-bcq8g
Thu Mar 29 04:42:02 UTC 2018 hello from kubernetes
Thu Mar 29 04:43:01 UTC 2018 hello from kubernetes
Thu Mar 29 04:44:01 UTC 2018 hello from kubernetes

Use Case #2

example manifest:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: example-cronjob
spec:
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
labels:
app: example-cronjob
spec:
containers:
- name: example-cronjob
image: ubuntu:latest
imagePullPolicy: Always
command:
- /bin/echo
args:
- i am a cronjob
schedule: '*/5 * * * *'
successfulJobsHistoryLimit: 3

Create the resource:

kubectl apply -f https://raw.githubusercontent.com/bambash/kube-cron-example/master/cronjob.yaml

List jobs spawned:

# kubectl get jobs
NAME DESIRED SUCCESSFUL AGE
example-cronjob-1522301640 1 1 10m
example-cronjob-1522301700 1 1 5m
example-cronjob-1522301760 1 1 26s

List finished job pods:

# kubectl get pods -a
NAME READY STATUS RESTARTS AGE
example-cronjob-1522301700-4p68q 0/1 Completed 0 10m
example-cronjob-1522301760-x4nwd 0/1 Completed 0 5m
example-cronjob-1522301820-5qtcs 0/1 Completed 0 1m

Get logs from finished job pod:

# kubectl logs example-cronjob-1522301820-5qtcs
i am a cronjob

Conclusion

Jane.ai - Engineering

Posts about engineering, AI, St. Louis, and probably vim

Nate Baker

Written by

https://github.com/bambash

Jane.ai - Engineering

Posts about engineering, AI, St. Louis, and probably vim