5

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
    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 to understand how to build your own image. Then you need to be able to see your image with kubernetes. Commented Jun 5, 2019 at 9:22
  • @Shashank Sharma did you solve this issue or it's still valid question? Commented Feb 25, 2021 at 11:12

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

I have a question :) According to the above yaml file, it seems to keep printing out "hello world." I ran above yaml file, however, I can't fine "hello world". Where can I find "hello world"?
In the ConfigMap (at the top of the file) you'll find the file script.py which contains "Hello, world!". This is what you can edit with anything you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.