0

Is there a way to run a scheduled job that would pull some files regurarly on a mounted shared volume?

I have tried cronjob but apparently it doesn't supposed external filesystems

thanks in advance.

zozo6015
  • 557
  • 2
  • 11
  • 27
  • What do you mean with the external filesystem thing? A cronjob has a template for the Pod which can mount volumes, if you have your external filesystem as a pvc, it should most definitely work – AndD Feb 07 '21 at 09:59
  • Yes, by external filesystem I mean PVC's any link to some documentation or examples? – zozo6015 Feb 07 '21 at 11:43

1 Answers1

2

CronJobs should be able to mount PVC just as any other resource which spawn Pods, you can just add a volumeMounts section under the container template, and then a volume section under template.

Something like the following:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: example-name
spec:
  schedule: '0 * * * *'
  jobTemplate:
    spec:
      completions: 1
      template:
        spec:
          containers:
          - name: example-container-name
            image: your-docker-repo/your-docker-image:the-tag
            volumeMounts:
            - name: data
              mountPath: /internal/path/to/mount/pvc
          volumes:
          - name: data
            persistentVolumeClaim:
              claimName: example-claim

This should mount example-claim PVC to the CronJob's Pod when the Pod is spawned.

Basically there are two sections.. under each container volumeMounts list the volumes mounted by the container, at which path and a few more configuration. All the volumeMounts entries should be defined once in the volumes section which associate names (that act as keys for the spec) and claims or empty-dir.

As for creating the PVC, let me link you the documentation (https://kubernetes.io/docs/concepts/storage/persistent-volumes/)

What you want to do basically is to create a Persistent Volume which points to your mounted shared volume (what is it, a nfs storage? The declaration changes slightly, depending on what exactly you want to mount) and then a Claim (PVC) in the same namespace of the CronJob which will bound to the PV.

If you are unsure about the correct indentation of the various objects or where to put things, check the practical API Reference Docs (https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#cronjob-v1beta1-batch)

AndD
  • 2,281
  • 1
  • 8
  • 22
  • I got this error when running on kubernetes 1.19.7: [ValidationError(CronJob.spec.jobTemplate.spec.template.spec): unknown field "volumeMounts" in io.k8s.api.core.v1.PodSpec, ValidationError(CronJob.spec.jobTemplate.spec): unknown field "volumes" in io.k8s.api.batch.v1.JobSpec]; if you choose to ignore these errors, turn validation off with --validate=false – zozo6015 Feb 07 '21 at 16:31
  • 1
    From your errors, it seems like you have the wrong indentation. volumeMounts should be under CronJob.spec.jobTemplate.spec.template.spec.containers and under your container (it's a list) while volume should be under CronJob.spec.jobTemplate.spec.template. If you post your whole yaml definition I can try it out, mine works, I tried (on v1.18) but checed the API of v1.19 and should work as well – AndD Feb 07 '21 at 17:57