Developing Kubernetes Operators from the Ground Up
We cover what you need to know to develop a Kubernetes Operator, as well as share additional resources you can use to learn more on the subject.
Jun 08, 2023 • 4 Minute Read
Kubernetes is a great platform for building other platforms. This means it is perfect for creating workflows that suit your needs. The operator pattern in Kubernetes is the key to enable custom workflows, and developing operators enables you to tap into Kubernetes as a platform provider.
We will be covering the process of developing a Kubernetes operator in-depth in my course “Developing Kubernetes Operators from the Ground Up”, but if you want to jump in and get a head start developing an operator, then look no further than this quick guide!
What You Will Need
First things first, we need to make sure we have the tools necessary for developing an operator. We will go over how to create a Go-based Kubernetes Operator. Here are the tools you will need:
- Go: This will be the programming language that is used to build the operator.
- Docker: This will be used to build and push images and will be needed to install kind (Kubernetes in Docker) if you choose to use it for your local Kubernetes cluster.
- Operator SDK: This has two components, listed below:
- The Operator-SDK: The command-line interface (CLI) tool and SDK facilitate the development of operators.
- Operator Lifecycle Manager: This facilitates installation, upgrade and role-based access control (RBAC) of operators within a cluster.
- Kubernetes cluster: This should be running locally. I recommend kind for this, but any local Kubernetes cluster will do.
- For local development and testing, use with cluster-admin permissions.
- You will need kubectl installed as well so you can interact with your cluster.
- Image registry: For example, use hub.docker.com to publish images.
After installing these tools, the fun begins.
Creating the Operator Project
First things first, we need to create the project directory for our operator dev project. We need to create it and then move into the project directory.
mkdir memcached-operator cd memcached-operator
After we move into the directory, we are then ready to initialize the project directory and pull down the scaffoldings files to help you create the the operator. Here is where we will start using the Operator SDK. You will use the operator-sdk init
command with the --domain
and --repo
flags. In my course, I will go in more detail on why these options are necessary.
operator-sdk init --domain example.com --repo github.com/example/memcached-operator
Creating the API and Controller
Our next step is to create a simple API and Controller for our operator. In the course we will create a more robust Memcached operator, but for this we just want to get things started so we can simply use the operator-sdk create api command
. To create the controller and other resources for this API, we will also use the --resource
and --controller
options.
perator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller
Creating and Deploying the Operator Image
Next we will use docker to build and push our operator image to our image registry. In the course we will simply use Docker Hub, but you can use which ever registry you want. You will use the following command to build and push the image.
make docker-build docker-push IMG="example.com/memcached-operator:v0.0.1"
Now that we have built our operator image, we have a couple of options on how we want to deploy our operator.
Deploy with OLM
Our first method is using the Operator Lifecycle Manager to deploy our operator. OLM allows you too easily manage, version, and upgrade your operator using the Operator Framework. First we will need to enable OLM in our cluster.
operator-sdk olm install
Next we will make the bundle image for our operator, then we will build and push the image. You will use the make bundle, make bundle-build
, and make bundle-push
commands to accomplish this.
make bundle IMG="example.com/memcached-operator:v0.0.1" make bundle-build bundle-push BUNDLE_IMG="example.com/memcached-operator-bundle:v0.0.1"
Next we will deploy our operator to our cluster using the operator-sdk run bundle command
.
operator-sdk run bundle <some-registry>/memcached-operator-bundle:v0.0.1
You can then create a sample Custom Resource using our scaffolding that we downloaded.
kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml
This will display an output like the following:
memcached.cache.example.com/memcached-sample created
Next if we wanted to uninstall our operator, you would use the following command:
operator-sdk cleanup memcached-operator
Deploy Directly to the Cluster
Our next method to deploy our operator is a direct deploy to the cluster. We will use the make deploy command.
make deploy IMG="example.com/memcached-operator:v0.0.1"
After the operator has been deployed, we can then create our Custom Resource.
kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml
Next to uninstall you would simple run the following:
make undeploy
Run Locally Outside of the Cluster
Finally our last method is more for testing our your operator before deploying to the cluster. You will simple use this command to do so:
make install run
This will run it in your terminal and to quit it just do a ctrl+c.
So What’s Next?
Now that you have the basics down, I am sure we have peaked your interest! If this is a topic that interests you and you would like to dive deeper into the subject, then check out my course “Developing Kubernetes Operators from the Ground Up”. I will go further in-depth on the tools used and the process of building your first Go-based Kubernetes Operator.
Stay awesome gurus, and learn all the things!