2

I'd like to create a cronjob that runs a python script mounted as a pvc, but I don't understand how to put test.py into the container from my local file system

apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: update_db
spec:
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
jen
  • 21
  • 2

1 Answers1

1

You have a volume called application-code. In there lies the test.py file. Now you mount the volume, but you are not setting the mountPath according to your shell command.

The argument is pyhton /client/test.py, so you expect the file to be placed in the /client directory. You just have to mount the volume with this path:

volumeMounts:
- name: application-code
  mountPath: /client

Update

If you don't need the file outside the cluster it would be much easier to integrate it into your docker image. Here an example Dockerfile:

FROM python:3.6.2-slim

WORKDIR /data

COPY test.py .

ENTRYPOINT['/bin/bash', '-c', 'python /data/test.py']

Push the image to your docker registry and reference it from your yml.

containers:
        - name: update-fingerprints
          image: <your-container-registry>:<image-name>
Chris
  • 5,109
  • 3
  • 19
  • 40
  • thanks! but how does it actually get placed in the /client directory? like where am i mapping my local path to the file to the path it should go in the running pod? do i need to do a k apply or something to get it onto the cluster first? is k apply even possible for a .py file? – jen Jul 10 '20 at 16:11
  • That depends on the platform your Kubernetes is running on. How is your persistentVolumeClaim configured? Can you post the yml? Generally you create a persistentVolume that uses a storageclass to mount a storage, azure file for example. Than you claim space of that volume with the persistentVolumeClaim. There are dozens provisioners, see https://kubernetes.io/docs/concepts/storage/storage-classes/ you can run ``kubectl get sc`` to see your default storageclass. Than simply place the file in the storage's directory. You don't need kubectl for that, it's a physical directory outside the cluster – Chris Jul 10 '20 at 16:52
  • storage class is vmsf3? – jen Jul 10 '20 at 17:18
  • is there any easier way of doing this or is using a pvc the most straightforward way? – jen Jul 10 '20 at 17:18
  • If you don't need the file outside the cluster, I would not recommend using a persistentvolumne. You integrate it into your docker image. I updated my answer. – Chris Jul 10 '20 at 17:39
  • If the answer was helpful, please consider upvoting/accepting. Thanks – Chris Jul 10 '20 at 17:47