Advance Kubernetes: What exactly are Kubernetes Operators?

Kubernetes has gained a lot of traction recently and is one of the standards followed across organizations when it comes to running and managing their containerized workloads. In this article, we are going to talk about Kubernetes operators.

Usage

Operators are used to running applications and tools on Kubernetes, like Redis Operator, Flink Operator, Istio Operator, and many more. You can look at the list of open-source operators present in Operator Hub.

What is Operator?

One of the major advantages of Kubernetes is that it can be extended very easily to write controllers on top of it. These controllers consume Kubernetes APIs to make changes in native Kubernetes objects. These are the exact tasks that the Kubernetes operator can perform and some extra tasks.

In short, the Kubernetes operator is nothing but a code that can interact with native k8s objects and perform some extra actions like executing some commands in the pod to configure the pods, and interacting with other tools with their interfaces, etc. You can very easily write an operator using operator-SDK or any language. You need a way to consume the Kubernetes APIs and operator-SDK has a lot of it already present in it, which makes it very easy to use and implement.

Let’s look at the code.

Let’s take a look at a Kubernetes operator to see what all it does. We will look high level in this Redis Operator, which is an open-source project to manage Redis clusters. We will not be looking at the code structure in this post but in the next post. Instead, we will just verify the two operations that we mentioned above about any operator.

Any operator majorly does 2 things. First is creating and interacting with Kubernetes native objects and next are some operations that can be specific to the tools that the operator is built to run. In the below folder you can see the first kind of objects being created in the code. https://github.com/spotahome/redis-operator/tree/master/service/k8s. Here you can see different files with name deployment.go, pod.go, rbac.go etc are creating the native Kubernetes objects.

The example code below in line 67 creates the deployment.go.

func (d *DeploymentService) CreateDeployment(namespace string, deployment *appsv1.Deployment) error {
	_, err := d.kubeClient.AppsV1().Deployments(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
	if err != nil {
		return err
	}
	d.logger.WithField("namespace", namespace).WithField("deployment", deployment.ObjectMeta.Name).Infof("deployment created")
	return err
}

The next task is making changes related to tools that the operator is built to control or manage. In this case, it is Redis. You can find the redis-related functions present in client.go in redis folder. https://github.com/spotahome/redis-operator/blob/master/service/redis/client.go

Below is the code sample of how a master is created in Redis. Line 218 in redis/client.go

func (c *client) MakeMaster(ip string, password string) error {
	options := &rediscli.Options{
		Addr:     net.JoinHostPort(ip, redisPort),
		Password: password,
		DB:       0,
	}
	rClient := rediscli.NewClient(options)
	defer rClient.Close()
	if res := rClient.SlaveOf("NO", "ONE"); res.Err() != nil {
		return res.Err()
	}
	return nil
}

So we saw how the operator is interacting with the Redis to create masters and slaves [in the next few lines of code]. Most of the time the operators will be doing a similar kind of thing, just the interface to interact with different tools can change.

So this was what Kubernetes operators are and what are the operations that they have to perform. In the next articles, we will try to go through Operator-SDK and the structure that it follows to create any operator.

If you like the article please share and subscribe to the blog.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.