Unleash the full potential of Containers with the Kubernetes Operator

Yobitel
8 min readApr 20, 2020

--

A Kubernetes Operator aims to capture the knowledge of a human operator who manages services and has extensive knowledge of how the system should behave, how it should be deployed and how to manage problems when they arise.

Engineers use Kubernetes to automate the process and manage repeatable monotonous tasks. It captures how you write codes beyond the advantages of using Kubernetes Operators.

What is the Kubernetes Operator?

An abstraction for the deployment of non-trivial applications on Kubernetes. It is concerned with the operational and deployment of an application using Kubernetes constructs.

The Kubernetes core facilitates the automation of deploying and running the workload. You can also customize how automation happens as per your needs. Kubernetes controllers help you in extending the behavior of a cluster without modifying Kubernetes. Operators behave as controllers for a Custom Resource and are a crucial part of the Kubernetes API.

Kubernetes makes the management and scaling of web apps, mobile backends, and API services easier. Since these are stateless applications, Kubernetes APIs such as Deployments can scale and recover from errors without any additional inputs.

For stateful applications such as databases, caches and monitoring systems, we use the Kubernetes operator to extend the API and preserve the domain knowledge.

Advantages of using a Kubernetes Operator:

● Increases functionality. Pack, deploy and manage a Kubernetes application after taking operational knowledge and encoding it into the software that is packed and shared with clients easily.

● Helps in building Cloud Native Apps and delivers automated advantages like deployment, scaling, backups, restoration, and updation while being able to run everywhere where the Kubernetes is deployed.

● Use Kubernetes’ extensibility via HTTP and kubectl to apply operations on the application.

● Get an automatic offer list, display and edit operations in all your resources with Kubectl and Dashboard.

● Secure your resources with HTTPs as Kubernetes Operators extend the security services provided by Kubernetes API. Security protocols such as HTTPs are available and can be extended to all the resources.

● Use the same authentication and authorization access to all your resources as the API server.

● Translate human knowledge into code and automate it completely.

● Using Kubernetes Operators in multi-cloud and hybrid environments simplify the usage of multiple applications and maintain consistency.

How does it work?

An operator is used to build an application and to drive an application on Kubernetes as it provides domain-specific knowledge to applications and can be used in troubleshooting. A Kubernetes Operator helps in the extension of the types of applications that run on Kubernetes. They help in the installation and management of custom solutions in a Kubernetes cluster. They consist of additional Custom Resource Definitions (CRDs) and a controller that runs within the cluster.

The custom resources (labeled above as 1) represent the instances of the software which are to be installed on a high level.

The controller (labeled above as 2) manages the monitoring of the custom resources while deploying and modifying a particular workload within the Kubernetes cluster to match the specified configuration.

The Advantages of Using an Operator

An operator can automate the following:

● Deployment of an application when needed

● Creating and managing backups of the state of the application

● Upgrading the application code

● Making changes related to the up-gradation like extra configurational settings

● Releasing a service to the application that is not supported by Kubernetes APIs

● Simulation of a failure in the cluster for testing

● Surpassing the member election process by choosing a leader for the distributed application

How are Operators Built?

The Operator Framework is an open-source tool for automatized, scalable and effective building and management of operators. It comprises of the following components:

  1. Operator SDK: The most important component helps us bootstrap our operator project in a short time. It saves the time to get into the Kubernetes APIs and focuses on operational logic. It makes the controller watch the custom resource (CR) for any changes in the project setup.
  2. Operator Lifecycle Manager: It provides us with a descriptive way of installing, upgrading and managing all operators along with their dependants in our Kubernetes clusters.
  3. Operator Metering: It manages the cluster usage for a long time and generates reports to show usage breakdown.

Types of Operators

There are different types of operators that can be built. These are:

  1. Helm based operators: These let us use our existing helm charts and are built using them. They are preferred for a stateless application as they are very easy to build.
  2. Ansible based Operator: These let us use our existing Ansible playbooks and roles and are built using them. They are also preferred for a stateless application that runs on operators.
  3. Go based operators: Used for solving the most complicated cases, these are preferred for stateful applications. In Golang based operators, we have to build the controller logic ourselves and provide it with custom requirements as per our specifications.

Helm based operator

The Operator SDK

Building a helm based operator comes with the advantage of including an existing helm chart to use the Kubernetes resources without writing any code. The function of an operator becomes to read a custom object and get the desired match of what is running.

The Operator SDK is used by developers for assistance in bootstrapping and building an operator without getting into the Kubernetes API. It provides abstractions and high-level APIs to manage operational logic intuitively.

In the beginning, the SDK affiliates the application’s logic with the Kubernetes API to execute the operations.

Over time, the SDK inspires the creation of smarter applications and provides cloud services.

Install the Operator SDK CLI

The CLI tool helps us to create and deploy an Operator project. You should install the SDK CLI to author your own operators. Follow these steps to install the Operator SDK CLI:

  1. Set the release version variable
$ RELEASE_VERSION=v0.16.0

2. Download the release binary:

$ curl -LO https://github.com/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu

3. Verify the downloaded release binary:

$ curl -LO https://github.org/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu.asc$ gpg — verify operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu.asc

4. Install the release binary in your PATH

$ chmod +x operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu && sudo mkdir -p /usr/local/bin/ && sudo cp operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu /usr/local/bin/operator-sdk && rm operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu

For more details, refer to this installation guide

Creating the Helm-Operators

We are using the grafana helm chart to create grafana-operators in this guide.

To download the application, please visit our Github repository.

$ operator-sdk new grafana-operator \ — kind=grafana-Service \ — api-version=example.org/v1alpha1 \ — type=helm — helm-chart ./chart/

The above command creates the grafana-operator project. As this operator incorporates the grafana helm chart, it is designed to specifically monitor the grafana resource with the APIVersion example.org/v1alpha1 and Kind Grafana-Service.

Build and run the operator

In order to run the operator, Kubernetes ought to incorporate the new Custom Resource definition that the operator is looking for.

An operator may be run:

● As a pod within a Kubernetes cluster

● As a go program outside the cluster utilizing the operator-sdk

DevOps like running the associate operator as a pod within a Kubernetes cluster for production and thus that is the method we are using in this guide.

Build the Grafana-Operator Image and Push it into a Registry

The command below builds the grafana-operator helm chart and pushes it to the docker hub registry, yobitel/grafana-operator.

$ operator-sdk build yobitel/grafana-operator:v0.0.1$ docker push yobitel/grafana-operator:v0.0.1

Kubernetes deployment manifests are generated in deploy/operator.yaml.

The command below deploys the image in this file and modifies it from the placeholder REPLACE_IMAGE to the previously built image.

$ sed -i ‘s|REPLACE_IMAGE|yobitel/grafana-operator:v0.0.1|g’ deploy/operator.yaml

Deploy the grafana-operator

Now we will install and use our newly built operator. The SDK has already created files in the deploy folder to create the operator in Kubernetes.

The commands below modify and run the YAML files as per the user requirements.

$ kubectl create -f deploy/crds/example.org_operators_crd.yaml$ 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

Verify the Grafana-Operator

$ kubectl get deployment

Deploy the grafana custom resource:

Understanding the Grafana CR spec:

Helm uses values in order to provide customization to a Helm chart’s default values (defined in the Helm chart’s values.yaml file).

To override these defaults, we set the desired values in the CR spec.

Here we will use the number of replicas as an example.

Upon inspection of the helm-charts/grafana/values.yaml, we see that the chart has a value called replicaCount and it is set to 1 by default. Suppose we want to have 2 grafana instances in our deployment, then we will need to make sure that our CR spec contains replicaCount: 2.

Update deploy/crds/example.org_v1alpha1_grafana_cr to get the following:

Run the file to ensure that the grafana-operator creates the deployment for the CR by using the following command.

$ kubectl apply -f deploy/crds/example.org_v1alpha1_grafana_cr.yaml$ kubectl get deployment

Here, the pods have been updated to 2 as we desired.

$ kubectl get pods

CRD helps in changing the replica and is useful in application scaling. You can simply update the CR file as per any changes and the application will be updated.

Summary

● Kubernetes Operators are showing a substantial growth in quantity and popularity.

● They are constantly developing and hence DevOps teams need to keep a close eye on their updates and incorporate them into their own systems. Continuous improvement goes a long way in best managing the application.

● Operators provide a huge productivity boost and can generate a huge boost.

● They are not just for databases. The versatility of cloud-based operator solution goes to security (the Aqua Security Operator), networking (Istio Operator) and CI/CD (the Spinnaker Operator) as well as the management of difficult databases such as MongoDB, Cassandra, and Redis.

● The research of Kubernetes Operators is valued at a booming $4.3 billion at a 30% compound annual growth from 2017–22.

● A whopping 87 percent of respondents of 501 IT pros are running container technologies, up from 55 percent in 2017.

● 90% of IT companies are using the technology in production.

For more details about Cloud-Native Kubernetes Operators and Cloud-Native Stacks, please refer to Yobitel Communications for more information.

Resources

https://github.com/operator-framework/operator-sdk

https://docs.openshift.com/container-platform/4.1/applications/operator_sdk/osdk-helm.html

https://blog.codecentric.de/en/2019/06/kubernetes-operators-helm/

http://kubernetes.io

https://www.magalix.com/blog/kubernetes-101-concepts-and-why-it-matters

--

--

Yobitel

Yobitel a Cloud-Native SaaS Multi-Cluster Containerized Serverless Application Services Provider.