I would like to create a cronjob that could get the script file inside PVC and run with it.
Here I have created a PVC inside AKS as follows:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
annotations:
volume.beta.kubernetes.io/mount-options: dir_mode=0777,file_mode=0777,uid=999,gid=999
volume.beta.kubernetes.io/storage-provisioner: kubernetes.io/azure-file
name: cronjob-scripts
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: aks-azure-files
After that, I connected it with a pod general-utils
,
and added a Python script file into there using the below command:
kubectl -n <NAMESPACE> cp cronjob_script.py general-utils:/var/scripts
Once confirmed the script files is there in PVC, I create a cronjob as following:
apiVersion: batch/v1
kind: CronJob
metadata:
name: testing-cronjob
spec:
schedule: "00 0 * * *"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
startingDeadlineSeconds: 300
jobTemplate:
spec:
template:
spec:
imagePullSecrets:
- name: <SECRET_NAME>
restartPolicy: OnFailure
volumes:
- name: cronjob-scripts
persistentVolumeClaim:
claimName: cronjob-scripts
containers:
- name: testing-cronjob
image: <PYTHON_INSTALLED_ALPINE_IMAGE>
command: ["py /var/scripts/cronjob_script.py"]
volumeMounts:
- name: cronjob-scripts
mountPath: "/var/scripts"
imagePullPolicy: Always
After created, I manually trigger the cronjob by running:
kubectl -n <NAMESPACE> create job --from=cronjob/testing-cronjob job-of-testing-cronjob
However, the pod created by the job throws out below error in the describe events:
Warning Failed 13s (x2 over 13s) kubelet
Error: failed to create containerd task: failed to create shim task:
OCI runtime create failed: runc create failed:
unable to start container process: exec: "py /var/scripts/cronjob_script.py":
stat py /var/scripts/cronjob_script.py: no such file or directory: unknown
Not sure if PVC will be connected after the pod is created, so it becomes a dead loop.
If I want to let cronjob getting script file to run, how should I achieve?
After a long investigation, I found that the error is due to the script file path.
We needs to put the path to args
instead of one line
command inside the configuration, i.e.
from
- name: testing-cronjob
image: <PYTHON_INSTALLED_ALPINE_IMAGE>
command: ["py /var/scripts/cronjob_script.py"]
to
- name: testing-cronjob
image: <PYTHON_INSTALLED_ALPINE_IMAGE>
command: ["py"]
args: ["/var/scripts/cronjob_script.py"]