Kubernetes Operators Patterns and Best Practices

Using operators to manage the lifecycle of multi-cloud, Kubernetes-based applications

Alain Airom (Ayrom)
5 min readJul 8, 2022

With Kubernetes, organizations can automate the deployment and management of distributed, multi-cloud applications. While Kubernetes includes many capabilities to manage workloads, it is not aware of operational requirements of specific applications, meaning manual tasks can still become a burden. Fortunately, Kubernetes is a platform designed for extension, and its automation capabilities can be augmented to encapsulate domain specific knowledge. Kubernetes operators, which extend Kubernetes in a consistent way, allow software developers to capture in code the human knowledge of how to deploy and manage specific enterprise applications.

Reasons to use Kubernetes Operators

Operators should be an integral part of the development of enterprise applications so that they can be used to automate potentially complex error-prone human tasks during installation, maintenance, or monitoring.

While developers have a variety of technologies available to them to install and operate software in Kubernetes (such as kubectl, oc, Helm, Kustomize, CI/CD, GitOps, and more), enterprises should consider the following reasons to use operators:

-Automation of Day 2 operations

-Reusability of software

  • Hubs and Marketplaces
  • Internal and external multi-tenancy applications

-Leverage of the Kubernetes community

  • Ecosystem, tools, and offerings
  • Kubernetes is an industry standards

For a deeper understanding of Kubernetes operators, review the “Why and when to use Kubernetes Operators” learning path. Also, you can get started with a very simple project in the Get started using Kubernetes Operators learning path.

Sample application deployable as operators

To help you understand the benefits of using Kubernetes operators, the Kubernetes Operator Patterns and Best Practices project provides a reference that includes a sample application that is deployed and managed by sample operators. The project demonstrates how to use an operator to create Kubernetes resources for a sample two-tier application. The project is available via open source in this public GitHub repo. In addition, the project demonstrates uses cases which represent the entire range of resources lifecycle management capabilities.

The sample project describes the different approaches for developing and deploying operators, ranging from local debugging through the use of the Operator Lifecycle Manager (OLM), which is a requirement for managing the lifecycles of operators installed to a cluster and for inclusion in marketplaces and catalogs.

The project introduces a way of organizing operator code, written in Go, to provide a more manageable abstraction.

Sample application architecture

Here is an architecture diagram of our sample application:

The Operator Sample Go project contains two operators and four applications:

Sample operator capabilities

The sample operators demonstrate the following capabilities:

  • Day 1 deployment — The operators define custom resource definitions, defining new resources and APIs on the cluster. This capability makes it easy for administrators to create abstracted resources which represent an entire application, without the need to create many individual Kubernetes resources. The operators demonstrate how to code a Kubernetes controller which uses the Kubernetes API from Go to automate the creation and configuration of many common resources such as deployments, statefulSets, secrets, and services.
  • Day 2 Auto Backup — This common Day 2 activity often requires manual activities. The sample operators define a custom resource which allows the administrator to define an automated backup strategy. The operator reconciles this by creating a scheduled CronJob resource which launches a stand-alone application to query the database and backup the data to a storage repository.
  • Day 2 Auto Scaling — This common Day 2 activity can require human operators to study metrics and alerts. Although existing Kubernetes technologies like Horizontal Pod Autoscaler are available, the sample operators take a different approach by creating a CronJob resource which launches a stand-alone application to make scaling decisions. This provides complete flexibility, as the application can consider multiple cluster metrics or even external systems to make a scaling decision.

Deploying the sample operators

To set up and build your own environment, you can review the prerequisites, required CLIs, environment configuration in the documentation in the project overview.

The sample operators can be deployed to Red Hat OpenShift, as an enterprise-grade Kubernetes platform. OpenShift provides the OperatorHub, which is a built-in operator marketplace that makes enterprise software more easily discoverable for developers and cluster administrators.

The easiest way to deploy the sample operators in our project is to use the automation scripts.

However, if you’d like to understand the mechanics behind the operator implementation, you can follow each of these steps to:

Summary and next steps

The operator sample application provides a step-by-step guide to build and deploy a set of operators, testing either locally or deploying to a cluster using Operator Lifecycle Manager (OLM). You can use the guidance provided to gain hands-on experience with developing and deploying operators.

The project also introduces several advanced capacities such as integrating with Prometheus to gather metrics or using webhooks to validate custom resources.

Finally, check out how to make your solutions run as a service and add them to service catalogs. Or, you can explore how to design, build, and deploy universal application images in this learning path.

To help you understand the benefits of using Kubernetes operators, the Kubernetes Operator Patterns and Best Practices project provides a reference that includes a sample application that is deployed and managed by sample operators. The project demonstrates how to use an operator to create Kubernetes resources for a sample two-tier application. The project is available via open source in this public GitHub repo. In addition, the project demonstrates uses cases which represent the entire range of resources lifecycle management capabilities.

--

--

Alain Airom (Ayrom)

IT guy for a long time, looking for technical challenges everyday!