Taming K8S jobs

with Python











Sławomir Zborowski, Wrocław 2019
image source: https://sunscrapers.com/blog/django-flask-pyramid-choose-right-python-framework-project/

Sławek Zborowski

R&D Architect @


✴ 2010
✝ . . . .

All opinions expressed during this presentation are solely my own and not necessarily those of my employer.

slawomir.net
out motto for today is:

Do What You Do Best
Outsource The Rest

OUTLINE

kubernetes jobs brief introduction working with jobs in Python official k8s client live demo session parallel ray tracing living on consequences & future

OUTLINE

kubernetes jobs brief introduction working with jobs in Python official k8s client live demo session parallel ray tracing living on consequences & future

what jobs are useful for?

  • compiling
  • building
  • batch processing
  • machine learning
  • many more...

in-house solution?

do you know cron/crontab?

have you tried writing an alternative?

usually something's already available: mature, maintained, tested in the field etc.

alternatives




lot of them are suited for specific workload!
k8s seems to be universal workload orchestrator

OUTLINE

kubernetes jobs brief introduction working with jobs in Python official k8s client live demo session parallel ray tracing living on consequences & future

useful resources

https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md

https://kubernetes.io/docs/home/

your other projects (yaml configuration files)

getting in

k8s.config.load_kube_config()
api = k8s.client.BatchV1Api()
                    

creating container

k8s.client.V1Volume(
    name='garage-pvc',
    persistent_volume_claim=k8s.client.V1PersistentVolumeClaimVolumeSource(
        claim_name='garage-pvc',
        read_only=False)
)
k8s.client.V1Container(
    env=envs,
    image=image,
    command=cmd,
    image_pull_policy='Always',
    name='garage',
    volume_mounts=volume_mounts
)

creating pod

k8s.client.V1PodTemplateSpec(
    k8s.client.V1ObjectMeta(
        name='garage',
        labels={'part': 'core'}),
    k8s.client.V1PodSpec(
        active_deadline_seconds=60,
        containers=containers,
        restart_policy='Never',
        security_context=k8s.client.V1PodSecurityContext(),
        volumes=create_volumes(),
        service_account_name='garage-admin'
    )
)

creating job

spec = k8s.client.V1JobSpec(
    active_deadline_seconds=60,
    completions=1,
    parallelism=1,
    backoff_limit=0,
    template=pod_spec
)

meta = k8s.client.V1ObjectMeta(name='name', labels={'part': 'core'})
job = k8s.client.V1Job(spec=spec, metadata=meta)
api.create_namespaced_job(namespace='garage', body=job)

what's next?

  • watch the job using
    read_namespaced_job_status
  • clean up, if required
  • launch other jobs?

OUTLINE

kubernetes jobs brief introduction working with jobs in Python official k8s client live demo session parallel ray tracing living on consequences & future

!! DEMO TIME !!

let's cast some rays

We have just built small-scale, cave-man project similar to Rayscale

WOW. That wasn't even tough!

OUTLINE

kubernetes jobs brief introduction working with jobs in Python official k8s client live demo session parallel ray tracing living on consequences & future

next steps

Use jinja2 to render .yml files and then parse them

If flow becomes complex (e.g. a DAG) - look at kubeflow

conclusions

there's decent tool to run jobs
it can be controlled programmatically
it can interoperate with rest of the cluster
it's easy and convenient

Do What You Do Best
And Outsource The Rest

thanks --all

Hexit

escape room for programmers

letmeout.pl