Kubernetes CronJob to run Python script

6/5/2019

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

-- Shashank Sharma
kubernetes
python

1 Answer

6/5/2019

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 you python script to PATH. then you would be able to run it from k8s CronJob

-- P Ekambaram
Source: StackOverflow