Kubernetes Cronjob To Run Python Script

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 # 55 min

template:

   spec:

   containers:

   -name: hello

image: python: 3.6 – slim

command: [“python”]

args: [“./main.py”]

restartPolicy: Never

The simple pythonhello world script is being taken further. This example creates a container containing a hello world python script and it is deployed as a CronJob to a cluster using helm.

kubectl – n

default create serviceaccount tiller

kubectl create clusterrolebinding tiller\

–clusterrole cluster – admin\

   –serviceaccount =

   default: tiller

helm init–service – account tiller–tiller – namespace

default

docker build – t python – helloworld: v1.

apiVersion: batch / v1beta1

kind: CronJob

metadata:

   name: {

      {

         .Values.cronjob.name

      }

   }

labels:

   app.kubernetes.io / name: {

      {

         .Values.cronjob.name

      }

   }

helm.sh / chart: {

   {

      include “python-helloworld.chart”.

   }

}

spec:

   schedule: “{{ .Values.cronjob.schedule }}”

jobTemplate:

   spec:

   template:

   spec:

   containers:

   -name: {

      {

         .Chart.Name

      }

   }

image: “{{ .Values.image.repository }}:{{ .Values.image.tag }}”

restartPolicy: OnFailure

image:

   repository: “python-helloworld”

tag: “v1”

pullPolicy: IfNotPresent

cronjob:

   name: python – helloworld

schedule: “*/1 * * * *”

I had originally planned to run bash script anycodings_kubernetes which could do:

txt ,In short I want to be able to run k8s anycodings_kubernetes CronJob which can run Python script by first anycodings_kubernetes installing requirements and then running it,where is the startup script.

/main.py anycodings_python located? is it present in the image. you anycodings_python need to build new image using anycodings_python python:3.6-slim as base image and add anycodings_python your python script to PATH.

then you anycodings_python would be able to run it from k8s CronJob

I Rried 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 # 55 min

template:

   spec:

   containers:

   -name: hello

image: python: 3.6 – slim

command: [“python”]

args: [“./main.py”]

restartPolicy: Never

One CronJob object is similar to one line of a crontab file. Cron format is used to write a job periodically on a schedule. ,A CronJob creates Jobs on a repeating schedule.

,To generate CronJob schedule expressions, you can also use web tools like crontab.guru. ,CronJob is a part of the Kubernetes platform.

You should read the CronJob object definition to understand the Kubernetes cron jobs.

apiVersion: batch / v1

kind: CronJob

metadata:

   name: hello

spec:

   schedule: “* * * * *”

jobTemplate:

   spec:

   template:

   spec:

   containers:

   -name: hello

image: busybox: 1.28

imagePullPolicy: IfNotPresent

command:

   -/bin/sh –

   -c –

   date;

echo Hello from the Kubernetes cluster

restartPolicy: OnFailure

apiVersion: batch / v1

kind: CronJob

metadata:

   name: hello

spec:

   schedule: “* * * * *”

jobTemplate:

   spec:

   template:

   spec:

   containers:

   -name: hello

image: busybox: 1.28

imagePullPolicy: IfNotPresent

command:

   -/bin/sh –

   -c –

   date;

echo Hello from the Kubernetes cluster

restartPolicy: OnFailure

Cron Schedule Syntax

#┌───────────── minute(0 – 59)

#│┌───────────── hour(0 – 23)

#││┌───────────── day of the month(1 – 31)

#│││┌───────────── month(1 – 12)

#││││┌───────────── day of the week(0 – 6)(Sunday to Saturday; #│││││ 7 is also Sunday on some systems)

#│││││ OR sun, mon, tue, wed, thu, fri, sat

#│││││

# * * * * *

Every CronJob has a CronJob Controller that checks how many times it missed a schedule. If there are more than 100 missed schedules, then the job won’t start.

Cannot determine

if the job needs to be started.Too many missed start time( > 100).Set or decrease.spec.startingDeadlineSeconds or check clock skew.

Abdullah
Abdullah
Articles: 33

Leave a Reply

Your email address will not be published. Required fields are marked *