This content originally appeared on Level Up Coding - Medium and was authored by Vaibhav Rajput
Operators : Extending Kubernetes Capabilities

Operators are software extensions to Kubernetes that make use of custom resources to manage applications and their components. So what it means is that there are some applications whose deployment and management might require manual intervention and operators is the solution to automate it.
Let’s say you need to deploy a database cluster for which each pod needs to be brought in sync after deployment, or say you need to perform a security scan whenever a new component is deployed, or maybe some configuration needs to be populated based on some event. Such a functionalities are not available in Kubernetes out of the box but can be implemented using operators.
Let us go through a journey of creating an operator and with each step we’ll understand how it works in detail
STEP 1: Think of an idea for controller
Pretty obvious, right?
So here we’ll be creating a custom resource which will specify a location and our operator will read that event and call OpenWeather API to fetch weather details for that location and populate it in a config map.
STEP 2: Create a custom resource definition
Start by creating a custom resource definition in which we will define the configurations for our custom resources. So basically for our use case, we will define that our custom resource can hold a location variable in its spec section.

RajputVaibhav/openweather_operator
Here as you can notice that we have created a custom API group and we’ll be using the same to create custom resources.
Deploy this file using the kubectl apply -f crd.yaml command.
STEP 3: Create the main operator logic
You can create a kubernetes operator in a variety of languages by picking a client from here. I’ll be using python here

RajputVaibhav/openweather_operator
Let’s break down this code…
We need a code which could work like the kubernetes control loop by constantly observing, checking difference and taking actions to bring the actual state to a desired state. You can surely implement this on your own but for python, you can directly use kopf library which abstracts all this logic and make it easy for you to focus on your requirements.
Now let’s pack it into a simple Dockerfile

RajputVaibhav/openweather_operator
STEP 4: Deploy the operator
We will be deploying the operator in the form of a pod created through deployment.

RajputVaibhav/openweather_operator
Now deploy it using the kubectl apply -f deployment.yaml
STEP 5: Provide permission to the operator
To ensure security, you need to explicitly provide the operator with the needed permissions. For this, we will create a cluster role

RajputVaibhav/openweather_operator
And deploy it using the kubectl apply -f rbac.yaml
STEP 6: Create a custom resource
Now that our definition is ready, we’ll use one of the allowed values of location to create a custom resource

RajputVaibhav/openweather_operator
Once deployed using the kubectl apply -f weather_report.yaml command, you can view the custom resource using this command kubectl api-resources --api-group=operators.rajputvaibhav.github.io
Upon creation, you should shortly be able to see a config map with the weather details of Delhi.

Parting note
Here we saw how we can trigger a set of commands based on an event in the kubernetes cluster. Similarly you can find a variety of applications for operators to extend the capabilities of your cluster.
You can also find a long list of existing operators created by the kubernetes community at operatorhub.io. Moreover, you can create your own operator and list it there to contribute to the community.
Operators : Extending Kubernetes Capabilities was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Vaibhav Rajput

Vaibhav Rajput | Sciencx (2021-06-13T13:45:02+00:00) Operators : Extending Kubernetes Capabilities. Retrieved from https://www.scien.cx/2021/06/13/operators-extending-kubernetes-capabilities/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.