how do scripts/files get mounted to kubernetes pods

Viewed 1107

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
1 Answers

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>
Related