Let’s dive into Kubernetes operator creation

A presentation at Config Management Camp in February 2024 in Ghent, Belgium by Horacio Gonzalez

Slide 1

Slide 1

Let’s dive into Kubernetes operator creation Horacio Gonzalez 2024-04-06

Slide 2

Slide 2

Who are we? Introducing myself and introducing Clever Cloud

Slide 3

Slide 3

Horacio Gonzalez @LostInBrittany Spaniard Lost in Brittany

Slide 4

Slide 4

Clever Cloud Our mission: give more speed to your teams and better quality to your projects

Slide 5

Slide 5

Warning Gophers, gophers everywhere!

Slide 6

Slide 6

Last year in Config Management Camp

Slide 7

Slide 7

I proposed a sequel for this year Let’s dive into Kubernetes operator creation This time Aurélie can’t do the talk with me 😢 So I must do it alone… Wish me luck!

Slide 8

Slide 8

And why Gophers? Because we love Gophers, of course! And because Golang and Kubernetes are so linked…

Slide 9

Slide 9

Credit where it is due All the gophers you will see are drawn by Aurélie and Horacio, and are based on the Go mascot designed by Renee French which is licensed under CC BY 3.0.

Slide 10

Slide 10

Kubernetes operators Helping to tame the complexity of K8s Ops

Slide 11

Slide 11

Taming microservices with Kubernetes

Slide 12

Slide 12

What about complex deployments

Slide 13

Slide 13

Tools like Helm helps with complexity

Slide 14

Slide 14

Helm Charts are configuration Operating is more than installs & upgrades

Slide 15

Slide 15

Kubernetes is about automation How about automating human operators?

Slide 16

Slide 16

Kubernetes Operators A Kubernetes version of the human operator

Slide 17

Slide 17

Building operators Basic K8s elements: Custom Resources & Controllers

Slide 18

Slide 18

Custom Resource Definitions Extending Kubernetes API

Slide 19

Slide 19

Extending Kubernetes API By defining new types of resources, internal or external to the cluster

Slide 20

Slide 20

With a CRD you can create CR in the cluster They are the blueprints of the Custom Resources

Slide 21

Slide 21

Custom Resources are simply data All the logic must be in the Controller

Slide 22

Slide 22

Kubernetes Controllers Keeping an eye on the resources

Slide 23

Slide 23

A reconcile loop Controllers watch the state of the cluster, and make or request changes where needed

Slide 24

Slide 24

Kubernetes Operator Automating operations

Slide 25

Slide 25

What’s a Kubernetes Operator?

Slide 26

Slide 26

Example: databases Things like adding an instance to a pool, doing a backup, sharding…

Slide 27

Slide 27

Knowledge encoded in CRDs and Controllers

Slide 28

Slide 28

Custom Controllers for Custom Resources Operators implement and manage Custom Resources using custom reconciliation logic

Slide 29

Slide 29

Operator Capability Model Gauging the operator maturity

Slide 30

Slide 30

A real, open-source example The Clever Operator

Slide 31

Slide 31

Available on Operator Hub & GitHub https://operatorhub.io/operator/clever-operator https://github.com/CleverCloud/clever-operator

Slide 32

Slide 32

Exposing Clever Cloud resources as CRD Allowing your apps to use our DBs as if they were in K8s

Slide 33

Slide 33

How can we write Operators? Which language? Any framework?

Slide 34

Slide 34

They are simply pods and manifests You can simply call Kubernetes APIs or use a compatible client

Slide 35

Slide 35

How to write an Operator

Slide 36

Slide 36

The Operator Framework Open source framework to accelerate the development of an Operator

Slide 37

Slide 37

Operator SDK Three different ways to build an Operator

Slide 38

Slide 38

Our objective Why? Because we can!

Slide 39

Slide 39

What do we want? ● In a simple and easy Kubernetes operator ● Handle cute Gophers ● In Javascript, because it’s very expressive and easy to understand… and I like it 😁

Slide 40

Slide 40

All the code is available https://github.com/LostInBrittany/lets-dive-into-kubernetes-operator-creation

Slide 41

Slide 41

Aurélie’s Gopher repository https://github.com/scraly/gophers

Slide 42

Slide 42

random-gopher container https://hub.docker.com/r/lostinbrittany/random-gopher

Slide 43

Slide 43

random-gopher container import express from ‘express’ ; import { readdir } from ‘node:fs/promises’ ; import path from ‘node:path’ ; let app = express(); let chosenGopher; async function initFiles() { try { const files = await readdir( ‘gophers’ ); const gophers = files.filter( (item) => item.endsWith( ‘png’) || item.endsWith( ‘jpg’) ); const randomIndex = Math.floor((Math.random()*gophers.length)); chosenGopher = gophers[randomIndex]; console.log(chosenGopher); } catch (err) { console.error(err); } } At startup it chooses and exposes a random gopher

Slide 44

Slide 44

random-gopher-deployment apiVersion: apps/v1 kind: Deployment metadata: name: random-gopher spec: selector: matchLabels: run: random-gopher replicas: 10 template: metadata: labels: run: random-gopher spec: containers: - name: random-gopher image: lostinbrittany/random-gopher:0.0.4 ports: - containerPort: 8080 Deploying lots of random-gophers in the cluster

Slide 45

Slide 45

Applying it to the cluster Deploying random-gopher-deployment Deploying the manifest kubectl apply -f manifests random-gopher-deployment.yaml Getting pods’ address kubectl get pods -o wide Create a busybox kubectl run -i —tty —rm debug —image=busybox —restart=Never — sh Asking for a Gopher name wget -qO - [pod_ip]:8080/gopher/name Let’s switch to the terminal…

Slide 46

Slide 46

We also have an API for Gophers https://github.com/LostInBrittany/lets-dive-into-kubernetes-operator-creation /tree/main/gopher-api-and-ui

Slide 47

Slide 47

We also have an API for Gophers

Slide 48

Slide 48

And an UI to see the Gophers in the API https://github.com/LostInBrittany/gophers-api-watcher

Slide 49

Slide 49

To make it easy we deploy on Clever Cloud

Slide 50

Slide 50

In the logs we get the API key

Slide 51

Slide 51

What we want An operator to feed the API with the deployed pods info

Slide 52

Slide 52

And we are doing it in the simplest way In JavaScript, yeah!

Slide 53

Slide 53

Taking as base k8s-operator-node https://github.com/dot-i/k8s-operator-node

Slide 54

Slide 54

Building the gopher operator Let’s switch to VS Code…

Slide 55

Slide 55

That’s all, folks! Thank you all!