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

Who are we? Introducing myself and introducing OVHcloud

Horacio Gonzalez @LostInBrittany Spaniard Lost in Brittany

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

Warning Gophers, gophers everywhere!

Last year in DevOps Barcelona

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

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

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

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.

Kubernetes operators Helping to tame the complexity of K8s Ops

Taming microservices with Kubernetes

What about complex deployments

Tools like Helm helps with complexity

Helm Charts are configuration Operating is more than installs & upgrades

Kubernetes is about automation How about automating human operators?

Kubernetes Operators A Kubernetes version of the human operator

Building operators Basic K8s elements: Controllers and Custom Resources

Kubernetes Controllers Keeping an eye on the resources

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

Custom Resource Definitions Extending Kubernetes API

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

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

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

Kubernetes Operator Automating operations

What’s a Kubernetes Operator?

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

Knowledge encoded in CRDs and Controllers

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

Operator Capability Model Gauging the operator maturity

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

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

How to write an Operator

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

Operator SDK

Our objective Why? Because we can!

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 😁

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

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

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

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

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

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…

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

We also have an API for Gophers

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

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

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

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

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

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

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

That’s all, folks! Thank you all!