Learning Kubernetes Operators with Percona Operator for MongoDB

by Edith Puclla

One of the topics that have resonated a lot for me since the first KubeCon I attended in 2018 is Kubernetes Operators.

The concept of Operators was introduced much earlier in 2016 by the CoreOS Linux development team. They were looking for a way to improve automated container management in Kubernetes.

What do we mean by a Kubernetes Operator?

We use the definition of CNCF. The Kubernetes project defines “Operator” simply: “Operators are software extensions that use custom resources to manage applications and their components“.

This means that among the applications that can be run on Kubernetes, there are applications that still require manual operations to manage them and complete the Kubernetes deployment cycle because Kubernetes itself can’t manage all these manual operations. It is what the Operators take care of, to automate those manual processes of the applications deployed in Kubernetes.

How can this be possible?

The Operators use/extend the Kubernetes API (this API has the basics needed for a user to interact with the Kubernetes cluster) and create custom resources to add new functionality according to the needs of an application to be flexible and scalable.

Once the creation of the custom resource is finished, it creates objects that can be managed using kubectl, as other default Kubernetes resources are managed, such as Deployments, Pods, etc.

Here we see the difference between the workflows with and without operators.

With Operators

With Operators

Without Operators

Without Operators

The above illustration is based on a presentation by Sai Vennam.

It is time for an example! We will see how Percona Operator for MongoDB works.

Percona Operator for MongoDB contains everything we need to quickly and consistently deploy and scale Percona Server for MongoDB instances into a Kubernetes cluster on-premises or in the cloud.

You can find Percona Operator for MongoDB officially in:

Why does Percona Server for MongoDB (a database) need a Kubernetes Operator?

Kubernetes has been designed for stateless applications. Kubernetes in many cases doesn’t require operators for stateless applications because Kubernetes doesn’t need more automation logic. But stateful applications like databases do need operators because they cannot automate the entire process natively.

One of the main benefits of operators is the automation of repetitive tasks that are often handled by human operators, eliminating errors in application lifecycle management.

Installing MongoDB Percona Operator using GKE

This guide shows you how to deploy Percona Operator for MongoDB on Google Kubernetes Engine (GKE). We use GKE which takes less time to set up Kubernetes in Google Cloud just for the purpose of this demo. This demonstration assumes you have some experience with the platform. For more information on the GKE, see Kubernetes Engine Quickstart

As prerequisites, we need Google Cloud shell and Kubectl. You can find the installation guides for AWS and AZURE in the Percona documentation. Let´s start!

  • Creating a GKE cluster with three nodes.
gcloud container clusters create my-cluster-name --project percona-product --zone us-central1-a --cluster-version 1.23 --machine-type n1-standard-4 --num-nodes=3

Overview

  • Now you should configure the command-line access to your newly created cluster to make kubectl able to use it.
gcloud container clusters get-credentials my-cluster-name --zone us-central1-a --project percona-product

Overview

kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user $(gcloud config get-value core/account)

Overview

Install the Operator and deploy your MongoDB cluster

  • Create a new namespace called percona-demo-namespace
kubectl create namespace percona-demo-namespace

Overview

  • Set the context for the namespace
kubectl config set-context $(kubectl config current-context) --namespace=percona-demo-namespace

Overview

  • Deploy the Operator
kubectl apply -f https://raw.githubusercontent.com/percona/percona-server-mongodb-operator/v1.13.0/deploy/bundle.yaml

Overview

  • The operator has been started, and you can deploy your MongoDB cluster:
kubectl apply -f https://raw.githubusercontent.com/percona/percona-server-mongodb-operator/v1.13.0/deploy/cr.yaml

Overview

  • When the process is over, your cluster will obtain the ready status. Check with:
  kubectl get psmdb.

Overview

Note: “psmdb” stands for Percona Server for MongoDB

Verifying the cluster operation

  • You will need the login and password for the admin user to access the cluster. Use kubectl get secrets command to see the list of Secrets objects
kubectl get secret my-cluster-name-secrets -o yaml

Overview

  • Bring it back to a human-readable form to MONGODB_DATABASE_ADMIN_PASSWORD and MONGODB_DATABASE_ADMIN_USER

Overview

  • We check the details of the Services, before testing the connection to the cluster

Overview

  • Run a Docker container with a MongoDB client and connect its console output to your terminal. The following command will do this, naming the new Pod percona-client:
kubectl run -i --rm --tty percona-client --image=percona/percona-server-mongodb:4.4.16-16 --restart=Never -- bash -il

Overview

  • Now run mongo tool in the percona-client command shell using the login (which is normally clusterAdmin)
mongo "mongodb://clusterAdmin:Dgqjc1HElUvvGnH9@my-cluster-name-mongos.percona-demo-namespace.svc.cluster.local/admin?ssl=false"

Overview

Woolaaa! We have deployed MongoDB in Kubernetes using Operator, It works! :)

Now that you have the MongoDB cluster, you have full control to configure and manage MongoDB deployment from a single Kubernetes control plane, which means that you can manage MongoDB instances in the same way you manage default objects in Kubernetes like Deployments, Pods, or Services. For advanced configuration, topics see our guide Percona Operator for MongoDB.

Conclusion

Kubernetes Operators extend the Kubernetes API to automate processes that cannot be achieved natively with Kubernetes. This is the case for stateful applications like MongoDB. Percona develops Percona Operator for MongoDB that contains everything you need to quickly and consistently deploy and scale Percona Server for MongoDB instances into a Kubernetes cluster on-premises or in the cloud. You can try it on different cloud providers and tutorials for more advanced configurations.

You can find Percona Operator for MongoDB in Hacktoberfest! If you’re looking to improve your Kubernetes skills, this is a great project to start contributing to.

We also have a public roadmap of Percona Kubernetes Operators. If you have any feedback or want to draw our attention to a particular feature, feel free to be part of it and vote for issues! :)

Resources:

Edith Puclla

Edith Puclla is a Technology Evangelist of Percona Corporation, studied at 42 Silicon Valley School in California in 2020, and was part of the Outreachy Internship in 2021. She has a background in DevOps and is a Docker and Kubernetes enthusiast.

See all posts by Edith Puclla »

Discussion

We invite you to our forum for discussion. You are welcome to use the widget below.

✎ Edit this page on GitHub