Kubernetes: Defining a CronJob for Collecting Log Data

Kubernetes CronJobs

  • scheduleTime - Uses the Linux crontab syntax to precisly define the exact, day, hour, minute and second when the job should run.
  • completions - The number of successful completions that need to be achieved before this cron job is considered successful. Potential use cases are jobs that optimize storage in a database, or clean up files and which may have a different set of success criteria.
  • parallelism - Controls if the the cron job can run in multiple parallel jobs or only sequentially
  • activeDeadlineSeconds - the maximum timespan in which the batch job needs to be finished. If it reaches this limit, the Kublet scheduler stops the cron job and considers it failed

KubeLogExporter CronJob

  • It needs to run every hour
  • It needs to be successful, e.g. no error when reading or storing a log occurs
  • It needs the use the service account that we discussed in the last article to have the proper access rights to namespaces, pods and logs
  • It needs to use a persistent volume to store the log files, so that independent of the actual node where the job runs the same log files are amended
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: kube-log-exporter-cron
spec:
schedule: "0 * * * *"
jobTemplate:
spec:
completions: 1
template:
spec:
restartPolicy: OnError
...
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: kube-log-exporter-cron
spec:
schedule: "0 * * * *"
jobTemplate:
spec:
completions: 1
template:
spec:
restartPolicy: OnError
serviceAccountName: log-exporter-sa
...
kind: PersistentVolumeClaim
metadata:
name: log-exporter-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: kube-log-exporter-cron
spec:
schedule: '0 * * * *'
jobTemplate:
spec:
completions: 1
template:
spec:
serviceAccountName: log-exporter-sa
containers:
- name: kube-log-exporter
image: docker.admantium.com/kube-log-exporter:0.1.9.12
volumeMounts:
- name: log-exporter-volume
mountPath: /etc/kube-log-exporter/logs
restartPolicy: Never
volumes:
- name: log-exporter-volume
persistentVolumeClaim:
claimName: log-exporter-pvc

Executing the CronJob

kb describe cronjob kube-log-exporter   130 

Name: kube-log-exporter-cron
Namespace: default
Labels: <none>
Annotations: Schedule: 0 * * * *
...Last Schedule Time: Mon, 25 Aug 2020 19:00:00 +0200
Active Jobs: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 12m cronjob-controller Created job kube-log-exporter-cron-1590426000
Normal SawCompletedJob 12m cronjob-controller Saw completed job: kube-log-exporter-cron-1590426000, status: Complete
Normal SuccessfulDelete 12m cronjob-controller Deleted job kube-log-exporter-cron-1590415200
> ls -la /etc/kube-log-exporter/logs-rw-r--r-- 1 root root    4515 Aug 25 19:00 lighthouse-78cc7475c7-74ctt_lighthouse.log
-rw-r--r-- 1 root root 6012 Aug 25 19:00 lighthouse-78cc7475c7-gcl94_lighthouse.log
-rw-r--r-- 1 root root 6873 Aug 25 19:00 lighthouse-78cc7475c7-k2cv7_lighthouse.log
-rw-r--r-- 1 root root 7634 Aug 25 19:00 lighthouse-78cc7475c7-l7zpv_lighthouse.log
-rw-r--r-- 1 root root 4636 Aug 25 19:00 lighthouse-78cc7475c7-wh2gk_lighthouse.log
-rw-r--r-- 1 root root 25741 Aug 25 19:00 redis-6b746f4d9b-8tjds_redis.log
....
> cat /etc/kube-log-exporter/logs/redis-6b746f4d9b-8tjds_redis.log1:C 25 Aug 2020 16:21:04.675 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 25 Aug 2020 16:21:04.675 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 25 Aug 2020 16:21:04.675 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.1 (00000000/0) 64
.-`` .-```
```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 7139
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
| `-._`-._ _.-'_.-' |
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 25 Aug 2020 16:21:04.678 # Server initialized

CronJob: Complete Resource Defintion

apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: kube-log-exporter-cron
spec:
schedule: '0 * * * *'
jobTemplate:
spec:
completions: 1
template:
spec:
serviceAccountName: log-exporter-sa
containers:
- name: kube-log-exporter
image: docker.admantium.com/kube-log-exporter:0.1.9.12
args: ['node', 'cluster.js']
volumeMounts:
- name: log-exporter-volume
mountPath: /etc/kube-log-exporter/logs
restartPolicy: Never
volumes:
- name: log-exporter-volume
persistentVolumeClaim:
claimName: log-exporter-pvc
imagePullSecrets:
- name: registry-secret

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store