Kubernetes: Is it possible to mount volumes to a container running as a CronJob? Kubernetes: Is it possible to mount volumes to a container running as a CronJob? kubernetes kubernetes

Kubernetes: Is it possible to mount volumes to a container running as a CronJob?


A CronJob uses a PodTemplate as everything else based on Pods and can use Volumes. You placed your Volume specification directly in the CronJobSpec instead of the PodSpec, use it like this:

apiVersion: batch/v1beta1kind: CronJobmetadata:  name: update-dbspec:  schedule: "*/1 * * * *"  jobTemplate:    spec:      template:        spec:          containers:          - name: update-fingerprints            image: python:3.6.2-slim            command: ["/bin/bash"]            args: ["-c", "python /client/test.py"]            volumeMounts:            - name: application-code              mountPath: /where/ever          restartPolicy: OnFailure          volumes:          - name: application-code            persistentVolumeClaim:              claimName: application-code-pv-claim


For the other question in there: "how does one solve the problem of getting application code into a running CronJob?"

You build your own image that contains the code. This is how it is normally done.

FROM python:3.6.2-slimADD test.py /client/test.pyCMD ['python','-c','/client/test.py']

Build and push to the docker registry.

docker build -t myorg/updatefingerprintsdocker push myorg/updatefingerprints

Use this image in the descriptor.

apiVersion: batch/v2alpha1kind: CronJobmetadata:  name: update_dbspec:  volumes:  - name: application-code    persistentVolumeClaim:      claimName: application-code-pv-claim  schedule: "*/1 * * * *"  jobTemplate:    spec:      template:        spec:          containers:          - name: update-fingerprints            image: myorg/update-fingerprints            imagePullPolicy: Always          restartPolicy: OnFailure

This requires thinking quite differently about configuration management and version control.