Giter Club home page Giter Club logo

k8s-cronjob-python-helloworld's Introduction

โ—‚ 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 โ–ธ

k8s-cronjob-python-helloworld's People

Contributors

ameyrupji avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.