Skip to main content

How to create a Kubernetes operator

The Kubernetes Operator Framework is an open source toolkit that manages Kubernetes operators in an effective, automated, and scalable way.
Image
Ship wheel on a wooden wall

Kubernetes operators are a way to create, configure, and manage complex applications on top of Kubernetes. Our previous article explained operators—how they came about and when and why they're useful. In this article, we'll show you how to create your own operator.

[ Learn the basics in the Kubernetes cheat sheet. ]

Create your own operator

Although the learning curve can be steep, there are tools and communities that make getting started straightforward. Operator Framework is an open source toolkit that manages Kubernetes operators in an effective, automated, and scalable way. It consists of three major toolsets: Operator SDK, Operator Lifecycle Manager (OLM), and OperatorHub.io.

Image
Image of the Operator framework.
Operator Framework components (Manna Kong, CC BY-SA 4.0)

Operator SDK

Operator SDK provides tools to build, test, and package operators. The framework uses the controller-runtime library to make writing operators easier by providing high-level APIs to write operational logic intuitively, tools for scaffolding and code generation to bootstrap a new project quickly, and extensions to cover common operator use cases.

To install Operator SDK, clone the source code and then compile:

$ git clone https://github.com/operator-framework/operator-sdk
$ cd operator-sdk
$ git checkout master
$ make install

[ Get this complimentary eBook from Red Hat: Managing your Kubernetes clusters for dummies. ]

Operator Lifecycle Manager (OLM)

OLM extends Kubernetes to provide a declarative way to install, manage, and upgrade operators on a cluster. OLM includes over-the-air updates and catalogs that keep operators up to date automatically. It has a dependency model that allows operators to express dependencies on the platform and on other operators. OLM makes operators and their services available for cluster users to select and install. It provides cluster stability by preventing conflicting operators from owning the same APIs.

OLM Architecture

Image
Image of the OLM workflow
(Manna Kong, CC BY-SA 4.0)

OLM runs on two operators:
  • olm-operator
  • catalog-operator

These operators manage the CRDs that are the basis of the OLM framework. The CRDs managed by olm-operator and catalog-operator are:

  • ClusterServiceVersion
  • InstallPlan
  • CatalogSource
  • Subscription
  • OperatorGroup

OLM Operator

OLM Operator deploys the applications defined by a ClusterServiceVersion (CSV) after the required resources specified in the CSV are in the cluster.

A CSV is a YAML manifest created from operator metadata that assists OLM in running the operator in a cluster. Since the OLM Operator is not concerned with creating the resources, a user can choose to either create these resources manually using the command-line interface (CLI) or opt to use Catalog Operator to create them.

OLM Operator is configured in such a way that it watches all namespaces. It watches for a CSV and checks that all requirements are met. If so, it runs the install strategy for the CSV.

Catalog Operator

The Catalog Operator resolves and installs CSVs and the resources they require. Catalog Operator watches for CatalogSource and Subscriptions and creates InstallPlans based on them.

  • CatalogSource is the collection of metadata defined in CSV. 
  • Subscriptions describe which channel of an Operator package to subscribe to and whether to perform updates automatically or manually. They also define the operator's name and namespace.

Catalog Operator also watches for resolved and unresolved InstallPlans created by the user and creates all the discovered resources for them.

[ Getting started with containers? Check out Deploying containerized applications: A technical overview. ]

OperatorHub.io

OperatorHub.io is a home for the Kubernetes community to share operators. It was launched and created by Red Hat with Amazon, Microsoft, and Google as initial supporters. OperatorHub.io aims to be the central location where users can find a wide array of community-built operators.

The OperatorHub.io library was made for both operator developers and end users. It provides developers a place to publish and update their work and defines criteria and requirements for publication. Operators on OperatorHub.io are standardized to provide end users with basic documentation and easy access to deploy and manage operators from their own Kubernetes cluster.

OperatorHub.io is the frontend for the Community Operator Repository on GitHub, so to get an operator published to OperatorHub.io, developers need only to package their operator via OLM and submit a pull request to the repository to have it reviewed. For more information on how you can contribute, check out the OperatorHub.io contribution page.

Where does OpenShift Container Platform fit in?

Image
Image of a Red Hat container platform.
(Manna Kong, CC BY-SA 4.0)

RedHat OpenShift Container Platform (RHOCP) is a consistent hybrid cloud foundation for building and scaling containerized applications. This platform helps developers build applications offering automated installation, upgrades, and lifecycle management throughout the container stack. It uses microservices-based architecture with smaller, decoupled units that work together.

The Red Hat Openshift Container Platform runs the Red Hat Enterprise Linux (RHEL) operating system and functions as a set of Docker-based application containers managed with Kubernetes orchestration. This platform is mainly used in hybrid cloud scenarios.

[ Learn the difference between Red Hat OpenShift and Kubernetes. ]

Scaffolding your own operator

The official Operator Framework tutorial gets you set up to create your own operator. Like most of the framework, it's easier than you might expect:

$ mkdir -p $HOME/projects/memcached-operator
$ cd $HOME/projects/memcached-operator

$ operator-sdk init --domain example.com \
  --repo github.com/example/memcached-operator

$ operator-sdk create api --group cache --version v1alpha1 \
  --kind Memcached --resource --controller

The Operator Framework helps you manage your Kubernetes and cloud operators. It lets you automate the vital tasks required to keep your cloud healthy, it's easy to learn, and simple to use. Install the Operator Framework and try it today.

Topics:   Kubernetes   Containers  
Author’s photo

Manna Kong

Manna is a Hmong-American woman located in the Midwest. She works as an Associate Software Engineer on the Operator Enablement and Tooling team within the Red Hat Ecosystem Experience Engineering organization. More about me

Author’s photo

Soundharya Pabba

I'm a 23-year-old international professional working as a Software Engineer at RedHat in the Operator Enablement Team. My team works on OpenShift, Kubernetes, and Operators, helping partners build their Kubernetes Operators and conducting various operator development workshops. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.