Kubernetes Cronjob to Run Python Script

Kubernetes Cronjob to Run Python Script

I am trying to schedule Python Script through Kubernetes CronJob but for some reason I am not able to understand how can I do it. I am able to run simple script like echo Hello World but that's not what I want

I tried using this specification:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "*/1 * * * *"
  concurrencyPolicy: "Forbid"
  failedJobsHistoryLimit: 10
  startingDeadlineSeconds: 600 # 10 min
  jobTemplate:
    spec:
      backoffLimit: 0
      activeDeadlineSeconds: 3300 # 55min
      template:
        spec:
          containers:
            - name: hello
              image: python:3.6-slim
              command: ["python"]
              args: ["./main.py"]
          restartPolicy: Never

But then I am not able to run it because main.py is not found, I understand that relative path is not supported so I hardcoded the path but then I am not able to find my home directory, I tried doing ls /home/ and over there my folder name is not visible so I am not able to access my project repository.

Initially I was planning to run bash script which can do:

  1. Install requirements by pip install requirements.txt
  2. Then run Python script

But I am not sure how can I do this with kubernetes, It is so confusing to me

In short I want to be able to run k8s CronJob which can run Python script by first installing requirements and then running it

2

1 Answer

where is the startup script ./main.py located? is it present in the image. you need to build new image using python:3.6-slim as base image and add your python script to PATH. then you would be able to run it from k8s CronJob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.