Let’s dive into Kubernetes operator creation

A presentation at DevOps Barcelona in November 2023 in Barcelona, Spain by Horacio Gonzalez

Slide 1

Slide 1

Let’s dive into Kubernetes operator creation Horacio Gonzalez 2023-11-09

Slide 2

Slide 2

Who are we? Introducing myself and introducing OVHcloud

Slide 3

Slide 3

Horacio Gonzalez @LostInBrittany Spaniard Lost in Brittany

Slide 4

Slide 4

OVHcloud PRIVATE CLOUD 33 Data Centers in 13 locations 1 Million+ Servers produced since 1999 PUBLIC CLOUD 44 Points of Presence on a 36 TBPS Bandwidth Network 1.6 Million Customers across 140 countries 2800+ Employees worldwide 308 Million euros adjusted EBITDA (2022) WEB CLOUD 117K+ Private Cloud VM running P.U.E. 1,10 – 1,30 Energy efficiency indicator PAAS 340K Public Cloud Instances running Over 20 Years in Business Disrupting since 1999 BARE METAL 400K Physical Servers in our data centers

Slide 5

Slide 5

Warning Gophers, gophers everywhere!

Slide 6

Slide 6

Last year in DevOps Barcelona

Slide 7

Slide 7

We proposed a sequel for this year Let’s dive into Kubernetes operator creation

Slide 8

Slide 8

But at the end Aurélie couldn’t come So I must do it alone… Wish me luck!

Slide 9

Slide 9

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

Slide 10

Slide 10

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 11

Slide 11

Kubernetes operators Helping to tame the complexity of K8s Ops

Slide 12

Slide 12

Taming microservices with Kubernetes

Slide 13

Slide 13

What about complex deployments

Slide 14

Slide 14

Tools like Helm helps with complexity

Slide 15

Slide 15

Helm Charts are configuration Operating is more than installs & upgrades

Slide 16

Slide 16

Kubernetes is about automation How about automating human operators?

Slide 17

Slide 17

Kubernetes Operators A Kubernetes version of the human operator

Slide 18

Slide 18

Building operators Basic K8s elements: Controllers and Custom Resources

Slide 19

Slide 19

Kubernetes Controllers Keeping an eye on the resources

Slide 20

Slide 20

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

Slide 21

Slide 21

Custom Resource Definitions Extending Kubernetes API

Slide 22

Slide 22

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

Slide 23

Slide 23

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

Slide 24

Slide 24

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

Slide 25

Slide 25

Kubernetes Operator Automating operations

Slide 26

Slide 26

What’s a Kubernetes Operator?

Slide 27

Slide 27

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

Slide 28

Slide 28

Knowledge encoded in CRDs and Controllers

Slide 29

Slide 29

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

Slide 30

Slide 30

Operator Capability Model Gauging the operator maturity

Slide 31

Slide 31

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

Slide 32

Slide 32

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

Slide 33

Slide 33

How to write an Operator

Slide 34

Slide 34

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

Slide 35

Slide 35

Operator SDK

Slide 36

Slide 36

Our objective Why? Because we can!

Slide 37

Slide 37

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 38

Slide 38

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

Slide 39

Slide 39

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

Slide 40

Slide 40

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

Slide 41

Slide 41

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 42

Slide 42

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 43

Slide 43

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 44

Slide 44

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

Slide 45

Slide 45

We also have an API for Gophers

Slide 46

Slide 46

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

Slide 47

Slide 47

Deploying the API in an instance Let’s switch to the terminal…

Slide 48

Slide 48

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

Slide 49

Slide 49

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

Slide 50

Slide 50

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

Slide 51

Slide 51

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

Slide 52

Slide 52

We ❤ feedbacks https://bit.ly/devopsbcn23-horacio

Slide 53

Slide 53

That’s all, folks! Thank you all!