Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kubernetes Operators in Python with Kopf

Kubernetes Operators in Python with Kopf

As presented at RedHat Developer's DevNation Day, Sep'2020.

Kubernetes Operators are trending in the field of orchestrating Kubernetes’ built-in resources, infrastructure-related custom resources for the management of the applications, and domain-specific custom resources.

Kopf is a framework to write Kubernetes Operators in Python easily and declaratively, while still supporting arbitrary logic. It takes the infrastructure hassle (e.g. K8s API communication) away from the developers, and implements typical operator patterns, thus leading to a clean codebase with only the clean business logic in it.

In this talk, we will explain the concept of the Kubernetes Operators for Software Developers, the event- and state-driven approaches to the application design, and show what is possible with Kopf & Kubernetes.

Sergey Vasilyev

September 15, 2020
Tweet

More Decks by Sergey Vasilyev

Other Decks in Technology

Transcript

  1. About me • My name is Sergey Vasilyev ◦ https://twitter.com/nolar

    • A Python developer from Siberia. Now in Berlin, Germany. • ~20 years of experience in Software Engineering. • In love with Python since 2009. • Sr. Backend Engineer, Zalando SE. • Running ML apps & infra on Kubernetes for Zalando Pricing & Forecasting.
  2. Defining a resource: YAML-driven development • Required: group/version. • Required:

    kind/plural/singular names. • Required: scope (“Namespaced”). • Optional: short names (aliases). • Optional: list formatting columns. • Optional: categories. • Optional: schema. $ kubectl apply -f crd.yaml $ kubectl apply -f obj.yaml Resource definition Resource itself
  3. The simplest Kubernetes operator And here we are! Creating: {'duration':

    '1m', 'field': 'value', 'items': ['item1', 'item2']} [2019-02-25 14:06:54,742] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Handler create_fn succeeded. [2019-02-25 14:06:54,856] kopf.reactor.handlin [INFO ] [default/kopf-example-1] All handlers succeeded for creation. Name: kopf-example-1 ... Status: create_fn: Message: hello world Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Success 81s kopf Handler create_fn succeeded. $ kopf run scripts.py [--verbose] $ kubectl apply -f obj.yaml $ kubectl describe -f obj.yaml
  4. Resource-watching handlers • As often, as the events arrive from

    K8s API. • Raw data, no interpretation. • Fire-and-forget, errors are ignored. • Similar to `kubectl get --watch`
  5. Error handling • A handler — an atomic unit of

    work. • Retried forever (until succeeded of failed permanently).
  6. Timers • Tick every N seconds. • Idle for the

    resource to be stable for some time.
  7. Other things • Python logging → Kubernetes Events. • Custom

    authentication → @on.login • Configuration → @on.startup / @on.cleanup • Embeddable into applications. • Natively asyncio-based. • Resource hierarchies. • Testing toolkit. • And more… • Other requests? Create a feature request!
  8. ROADMAP (3 major tracks) • Features: ◦ Cluster/resource awareness. ◦

    Admission hooks: validation/mutation. ◦ More patterns: cross-resource handlers. • Community: ◦ Support Kopf-based operators. ◦ Build a community of contributors. • SDK: ◦ YAML from Python — CRDs, RBAC, Deployments.
  9. SUMMARY • Kubernetes operators can be easy. • Kubernetes operators

    can be ad-hoc. • Kubernetes operators can be Pythonic. • Kubernetes is an orchestrator of everything. • Focus on the domain, not on the infrastructure. • Simplicity & human-friendliness as the #1 priority. • Python community as a huge leverage for Kubernetes. • Use Kopf. • Spread the word!
  10. LINKS • Source: https://github.com/nolar/kopf (support it with a ⭐) •

    Examples: https://github.com/nolar/kopf/tree/master/examples • Documentation: https://kopf.readthedocs.io/ • Twitter: @nolar • LinkedIn: https://www.linkedin.com/in/sergeyvasilyev/ • Slides: https://speakerdeck.com/nolar/kubernetes-operators-in-python-with-kopf