Skip to content

Python hello world script deployed as a CronJob on local Kubernetes using helm

License

Notifications You must be signed in to change notification settings

ameyrupji-k8s/k8s-cronjob-python-helloworld

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

◂ Previous

k8s-cronjob-python-helloworld

Taking the simple python hello world script further. This example creates a docker container containing a python hello world script thar prints "Hello World!" which gets deployed as a CronJob to a kubernetes cluster using helm.

To learn more about CronJobs click here.

Note: Refer docker-python-helloworld repo.

Prerequisites

  • Python - >=3.5
  • Visual Studio Code
  • Docker
  • Helm
  • Tiller on the Kubernetes cluster

System Configuration at time of test

  • macOS Catalina - Version 10.15.3
  • Python - Version 3.7.6
  • Docker Desktop - Version 2.2.0.4 (43472)
  • Kubernetes - v1.15.5
  • Helm - v2.14.3

Initial Setup

Installing tiller

Run the following commands to install tiller if already not installed on your local cluster.

kubectl -n default create serviceaccount tiller

kubectl create clusterrolebinding tiller \
  --clusterrole cluster-admin \
  --serviceaccount=default:tiller

helm init --service-account tiller --tiller-namespace default

Creating Python code

Follow the steps outlined in docker-python-helloworld GitHub project to create a docker container.

The basic commands required to create a container are"

docker build -t python-helloworld:v1 .

Adding Helm charts

Making the docker container ready to be deployed on kubernetes using helm create the necessary charts by running command helm create {chart name} in this case the chart name should be same as the the project name in this case python-helloworld. This will create a folder with that name and add default chart templates. Here is the link to the documentation of the files created.

Look the modified code in the python-helloworld directory. Delete the following files templates/deployment.yaml, templates/service.yaml, templates/NOTES.txt, templates/ingress.yaml, templates/tests/test-connection.yaml and values.yaml.

Create a new file templates/cronjob.yaml with the following contents:

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

Create a new file values.yaml with the following contents:

image:
  repository: "python-helloworld"
  tag: "v1"
  pullPolicy: IfNotPresent

cronjob:
  name: python-helloworld
  schedule: "*/1 * * * *"

Lets understand the schedule in more detail with this illustration:

image-schedule-explained

So for the python CronJob that we have created this will run every min.

Validate the created charts

To validate the created helm charts run the following command:

helm lint ./python-helloworld/

terminal helm lint

Run Helm Install

To install the CronJob using kubernetes run the following command:

helm install --name python-helloworld -f ./python-helloworld/values.yaml ./python-helloworld/ --tiller-namespace default

terminal-helm-install

Alternatively you can also use upgrade command with the --install flag:

helm upgrade python-helloworld ./python-helloworld/ -f ./python-helloworld/values.yaml --install --tiller-namespace default

Test

Verify that the CronJob is installed

Verify that the CronJob is installed properly using helm using the command helm list --tiller-namespace defaut terminal-helm-list

Now using the kubectl command kubectl get pods --all-namespaces

terminal-kubectl-get-pods

Alternately using the kubectl command kubectl get jobs --all-namespaces

terminal-kubectl-get-jobs

This pod is configured to run every one min so trying back after a min you will see two pods in the Completed state using command kubectl get pods --all-namespaces

terminal-kubectl-get-pods-multiple-cronjob-runs

Check Logs

Check run logs kubectl logs {pod_name} -n default

terminal-cronjob-pod-logs

Manually run CronJob

To manually test the CronJob you can use the following command:

kubectl create job --from=cronjob/python-helloworld python-helloworld-manual-run

To verify if that CronJob ran successfully run kubectl get pods --all-namespaces

terminal-kubectl-create-jobs-manually

Cleanup

To delete the container that was created use this command: helm delete --purge python-helloworld --tiller-namespace default

terminal-helm-delete

To delete the docker image that was created: docker rmi python-helloworld:v1

Useful Links

Next ▸

About

Python hello world script deployed as a CronJob on local Kubernetes using helm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published