Getting Started With Kubernetes Operators (Helm Based) - Part 1

Introduction

The concept of operators was introduced by CoreOs in the last quarter of  2016 and post the introduction of operator framework last year, operators are rapidly becoming the standard way of managing applications on Kubernetes especially the ones which are stateful in nature. In this blog post, we will learn what an operator is. Why they are needed and what problems do they solve. We will also create a helm based operator as an example.

What is an Operator?

Whenever we deploy our application on Kubernetes we leverage multiple Kubernetes objects like deployment, service, role, ingress, config map, etc. As our application gets complex and our requirements become non-generic, managing our application only with the help of native Kubernetes objects becomes difficult and we often need to introduce manual intervention or some other form of automation to make up for it.

Operators solve this problem by making our application first class Kubernetes objects that is we no longer deploy our application as a set of native Kubernetes objects but a custom object/resource of its kind, having a more domain-specific schema and then we bake the “operational intelligence” or the “domain-specific knowledge” into the controller responsible for maintaining the desired state of this object. For example, etcd operator has made etcd-cluster a first class object and for deploying the cluster we create an object of Etcd Cluster kind. With operators, we are able to extend Kubernetes functionalities for custom use cases and manage our applications in a Kubernetes specific way allowing us to leverage Kubernetes APIs and Kubectl tooling.

Operators combine crds and custom controllers and intend to eliminate the requirement for manual intervention (human operator) while performing tasks like an upgrade, handling failure recovery, scaling in case of complex (often stateful) applications and make them more resilient and self-sufficient.

How to Build Operators ?

For building and managing operators we mostly leverage the Operator Framework which is an open source tool kit allowing us to build operators in a highly automated, scalable and effective way.  Operator framework comprises of three subcomponents:

  1. Operator SDK:  Operator SDK is the most important component of the operator framework. It allows us to bootstrap our operator project in minutes. It exposes higher level APIs and abstraction and saves developers the time to dig deeper into kubernetes APIs and focus more on building the operational logic. It performs common tasks like getting the controller to watch the custom resource (cr) for changes etc as part of the project setup process.

  2. Operator Lifecycle Manager:  Operators also run on the same kubernetes clusters in which they manage applications and more often than not we create multiple operators for multiple applications. Operator lifecycle manager (OLM) provides us a declarative way to install, upgrade and manage all the operators and their dependencies in our cluster.

  3. Operator Metering:  Operator metering is currently an alpha project. It records historical cluster usage and can generate usage reports showing usage breakdown by pod or namespace over arbitrary time periods.

Types of Operators

Currently there are three different types of operator we can build:

  1. Helm based operators: Helm based operators allow us to use our existing helm charts and build operators using them. Helm based operators are quite easy to build and are preferred to deploy a stateless application using operator pattern.

  2. Ansible based Operator: Ansible based operator allows us to use our existing ansible playbooks and roles and build operators using them. There are also easy to build and generally preferred for stateless applications.

  3. Go based operators: Go based operators are built to solve the most complex use cases and are generally preferred for stateful applications. In case of an golang based operator, we build the controller logic ourselves providing it with all our custom requirements. This type of operators is also relatively complex to build. 

Building a Helm based operator

1. Let’s first install the operator sdk

      go get -d github.com/operator-framework/operator-sdk
      cd $GOPATH/src/github.com/operator-framework/operator-sdk 
      git checkout master
      make dep
      make install

Now we will have the operator-sdk binary in the $GOPATH/bin folder.      

2.  Setup the project 

For building a helm based operator we can use an existing Helm chart. We will be using the book-store Helm chart which deploys a simple python app and mongodb instances. This app allows us to perform crud operations via. rest endpoints.

Now we will use the operator-sdk to create our Helm based bookstore-operator project.

In the above command, the bookstore-operator is the name of our operator/project. --kind is used to specify the kind of objects this operator will watch and --api-verison is used for versioning of this object. The operator sdk takes only this much information and creates the custom resource definition (crd) and also the custom resource (cr) of its type for us (remember we talked about high-level abstraction operator sdk provides). The above command bootstraps a project with below folder structure

bookstore-operator/
  |
  |- build/    # Contains the Dockerfile to build the operator image
  |- deploy/   # Contains the crd,cr and  manifest files for deploying operator
  |- helm-charts/  # Contains the helm chart we used while creating the project
  |- watches.yaml    # Specifies the resource the operator watches (maintains the state of)

We had discussed the operator-sdk automates setting up the operator projects and that is exactly what we can observe here. Under the build folder, we have the Dockerfile to build our operator image. Under deploy folder we have a crd folder containing both the crd and the cr. This folder also has operator.yaml file using which we will run the operator in our cluster, along with this we have manifest files for role, rolebinding and service account file to be used while deploying the operator.  We have our book-store helm chart under helm-charts. In the watches.yaml file.

We can see that the bookstore-operator watches events related to BookStore kind objects and executes the helm chart specified.

If we take a look at the cr file under deploy/crds (velotio_v1alpha1_bookstore_cr.yaml) folder then we can see that it looks just like the values.yaml file of our book-store helm chart.

In the case of Helm charts, we use the values.yaml file to pass the parameter to our Helm releases, Helm based operator converts all these configurable parameters into the spec of our custom resource. This allows us to express the values.yaml with a custom resource (CR) which, as a native Kubernetes object, enables the benefits of RBAC applied to it and an audit trail. Now when we want to update out deployed we can simply modify the CR and apply it, and the operator will ensure that the changes we made are reflected in our app.

For each object of  `BookStore` kind the bookstore-operator will perform the following actions:

  1. Create the bookstore app deployment if it doesn’t exists.

  2. Create the bookstore app service if it doesn’t exists.

  3. Create the mongodb deployment if it doesn’t exists.

  4. Create the mongodb service if it doesn’t exists.

  5. Ensure deployments and services match their desired configurations like the replica count, image tag, service port etc. 

3. Build the Bookstore-operator Image

The Dockerfile for building the operator image is already in our build folder we need to run the below command from the root folder of our operator project to build the image.

4. Run the Bookstore-operator

      As we have our operator image ready we can now go ahead and run it. The deployment file (operator.yaml under deploy folder) for the operator was created as a part of our project setup we just need to set the image for this deployment to the one we built in the previous step.

After updating the image in the operator.yaml we are ready to deploy the operator.

       kubectl create -f deploy/service_account.yaml
       kubectl create -f deploy/role.yaml  
       kubectl create -f deploy/role_binding.yaml
       kubectl create -f deploy/operator.yaml 

Note: The role created might have more permissions then actually required for the operator so it is always a good idea to review it and trim down the permissions in production setups.

Verify that the operator pod is in running state.

kubernetes Helm 1.png

5. Deploy the Bookstore App

Now we have the bookstore-operator running in our cluster we just need to create the custom resource for deploying our bookstore app. 

First, we can create bookstore cr we need to register its crd.

Now we can create the bookstore object.

Now we can see that our operator has deployed out book-store app.

Now let’s grab the external IP of the app and make some requests to store details of books.

kubernetes Helm 3.png

Let’s hit the external IP on the browser and see if it lists the books we just stored:

kubernetes Helm 4.png

The bookstore operator build is available here.

Conclusion

Since its early days kubernetes was believed to be a great tool for managing stateless application but the managing stateful applications on kubernetes was always considered difficult. Operators are a big leap towards managing stateful applications and other complex distributed, multi (poly) cloud workloads with the same ease that we manage the stateless applications. In this blog post, we learned the basics of Kubernetes operators and build a simple helm based operator. In the next installment of this blog series, we will build an Ansible based Kubernetes operator and then in the last blog we will build a full-fledged golang based operator for managing stateful workloads.

Related Reads:

About the Author

Akash Gautam.jpg

Akash is an AWS certified developer and solution architect with expertise in infrastructure automation, containerized deployments, and microservice design patterns. He is also a gopher and has built custom kubernetes controllers and operators . In his free time, he likes to read books.