Skip to main content

k8s-Scheduler

A Simple python based scheduler to schedule kubernetes jobs on crontab schedules.

This module will help schedule two kinds of jobs ,

Periodic

These are jobs which are supposed to run on a crontab schedule. This package would launch kubernetes pods as per the crontab schedule and monitor its runs.

Continuous runs

These are jobs which are supposed to be running continuously. Like a stream data input. This package would launch and monitor such continuous runs. If any of the run fails for any reason , this package would reschedule it.

Installation

To install the scheduler run below command,

pip install k8s-scheduler 

Pre requisites

As the name suggests it's a job scheduler for kubernetes , so you would need a k8s instance apart from below mandatory and optional requirements.

Mongodb - This acts as a data store for scheduler and results backend.

EFL / ELK Stack - For logging and retrieving logs for a pod which is complete / running

Example usage

Use the package to create a schedule entry in the backend , and use the run_scheduler method to start the scheduler in backrgound mode.

from k8s_scheduler import Scheduler
from k8s_scheduler.LogHandler import LogHandler

scheduler = Scheduler(host="mongo_db_host",
                      username="user_name",
                      password="password",
                      db_name="test_schedule")

# Create schedules in the mongodb collection
job_id_1 = scheduler.create_schedule(schedule=test_job_1)
job_id_2 = scheduler.create_schedule(schedule=test_job_2)
job_id_3 = scheduler.create_schedule(schedule=test_job_3)

# Start Scheduler application 
scheduler.start_scheduler()

Schedule object type

Below is the template for the scheduler object.

schedule_rec = {
        "schedule_name": "test-schedule-3-always", # Name of the schedule
        "schedule_enabled": 1,  # 1 for enabled 0 for disabled 
        "schedule_description": "test always schedule with nginx and with service",
        "schedule_type": "always", # always is for streaming continuous jobs , periodic is for crontab based schedules 
        "parallel_execution": 0, # If two instances of the same job can run together
        "schedule_crontab": { # Crontab schedule
                            "minute": "*/2",
                            "hour": "*",
                            "day_of_month": "*",
                            "month": "*",
                            "day_of_week": "*"
                            },
        "kubernetes_deployment_options": {
                                        "name": "nginx-always-service",  # name of the deployment
                                        "container_name": "nginx-always", # Container name
                                        "container_image": "nginx:latest", # Image 
                                        "restart_policy": "", # k8s Restart policy 
                                        "ttl_seconds_after_finished": 10, # Seconds until the job needs to be purged 
                                        "env_vars": [], # Env vars for the deployment 
                                        "deploy_service": 1,  # If a service needs to be deployed ? 1=yes 0=No
                                        "service_name": "nginx-service", # Name of the service 
                                        "port": 8080, # Port exposed 
                                        "target_port": 80 # Port exposed 
                                        }
        }

Scheduler options

Below are the defaults when starting the scheduler, any of these params can be modified when creating a scheduler instance

scheduler = Scheduler(db_name="k8s_scheduler",  # Name of the Mongodb Database
                      scheduler_collection_name="test_coll", # Collection name for the scheduler
                      result_db_collection='result_coll', # Collection name for the result store
                      scheduler_poll_interval=10, # Polling interval for scheduler in seconds 
                      k8s_worker_namespace="test-namespace", # k8s namespace in which pods are to be deployed 
                      k8s_config_file_path='/var/snap/microk8s/current/credentials/client.config', # k8s config file if running outside cluster
                      **kwargs # connection args for mongodb 
                      )

Log Retrieval and schedules history.

We can retrieve logs of the jobs by connecting to ELK/EFK stack as below

log_handler = LogHandler(elastic_search_server='10.1.179.109',  # Elastic server
                         elastic_search_port='9200', # port 
                         worker_namespace='test-namespace', # namespace of the workers 
                         index='logstash*') # logstash index 

log_handler.get_logs(schedule_name="nginx-always", # Name of the schedule  
                     output_json=True) # If True Returns log as json object , if False prints log to console 

Testing the application

To test the application we can use microk8s to spin up a k8s cluster and install required backends.

Install microk8s / k8s flavour

sudo snap install microk8s --classic
microk8s enable helm3 ingress dashboard dns storage registry fluentd
sudo snap alias microk8s.kubectl kubectl
sudo snap alias microk8s.helm3 helm

Install backends

helm install test-mongodb bitnami/mongodb -n test-mongodb --create-namespace --set architecture=replicaset

Once the stack is up use make file to create and clean the dns entries so that you can connect to database from outside the cluster.

Metadata

Release files for k8s-scheduler 0.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for k8s-scheduler 0.0.13
File Size Uploaded
k8s-scheduler-0.0.13.tar.gz 15.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for k8s-scheduler 0.0.13
File Interpreter ABI Platform
k8s_scheduler-0.0.13-py3-none-any.whl Python 3 none any Details

Total release size: 29.0 kB

Release files / k8s-scheduler-0.0.13.tar.gz

Download URL k8s-scheduler-0.0.13.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c23f44d8b818f949dcc21d79ea7acbb7c3310eaa2d1bd5f17e57bd04a04b8c84
BLAKE2b-256 checksum
How to use checksums
69b1eea8ca2c3250fe77dd2bcaae1669224b7b1e1c93b3be81c1d5d6f8528965
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

Release files / k8s_scheduler-0.0.13-py3-none-any.whl

Download URL k8s_scheduler-0.0.13-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce2d866e025a7838e7b4162eb5fe77468fe40ecf08ccb58bd4ce33b0bc75296e
BLAKE2b-256 checksum
How to use checksums
f9cfdc1f50e43842deb3d2785281dfd20b28b2bdfeb40306780450f4ef025d10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.0.13 This release

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page