4

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
  • 385
  • 1
  • 4
  • 14
  • 1
    You are not able to run it because you're using the image `python:3.6-slim`. In order to make it work you need to create your own image based on `python:3.6-slim`. Please look the [following link](https://docs.docker.com/engine/reference/builder/) to understand how to build your own image. Then you need to be able to see your image with kubernetes. – silgon Jun 05 '19 at 09:22
  • @Shashank Sharma did you solve this issue or it's still valid question? – PjoterS Feb 25 '21 at 11:12

2 Answers2

1

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

P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
0

It looks like this answer could help you: Kubernetes: Is it possible to mount volumes to a container running as a CronJob?

You can mount a script as a Volume if you don't want to build your own docker image.

Here is an example

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-scripts
data:
  script.py: |
    print('Hello, world!')
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: python:3.11-alpine
            args: ["python", "/tmp/python/script.py"]
            volumeMounts:
            - name: scripts
              mountPath: /tmp/python
          restartPolicy: Never
          volumes:
          - name: scripts
            configMap:
              name: test-scripts
Titouan13
  • 21
  • 3