How to Build a Kubernetes Operator
Join the DZone community and get the full member experience.Join For Free
This is the second part of our series focusing on Kubernetes Operators, and it shows how you can build a Kubernetes Operator based on the Bitnami Apache Helm chart. Note that you can refer to the steps in this tutorial to build an operator for your own applications.
- We assume you followed the first part of the series. Thus, you should have a Kubernetes cluster (v1.7 or newer) with a control plane and two workers running on your computer. Also, the Operator Lifecycle Manager should be installed on your system. You can enter the following command to verify that everything is set up:
- The Operator SDK is installed on your machine. For details about installing the Operator SDK, refer to the Install the Operator SDK CLI page.
- Helm CLI is installed on your computer. To install Helm CLI, follow the instructions from the Installing Helm page.
- Docker. For details about installing Docker, refer to the Install Docker page.
- You need a quay.io account.
Set Up The Apache Operator
In this section, we'll walk you through the process of setting up the Apache Operator using Bitnami's Helm chart.
- Add the Bitnami Helm repository to your Helm client by running the following command:
- Create a Helm-based Operator by running the
operator-sdk newcommand, and passing it the following arguments:
- The name of your operator (
--api-versionflag with the Kubernetes apiVersion. The format is
$GROUP_NAME/$VERSION. In this tutorial, we'll be using
--kind flagwith the name of the Kubernetes CRD (
--typeflag with the type of operator. We'll be using
helm. Other valid types are
helm-chartflag with the name of the Helm chart (
This command creates the following directory structure:
Things to note:
- Kubernetes compares the actual state of the cluster with the desired state. Then, it takes action to match these states. The Operators extend this pattern by watching a specific custom resource type and taking actions to match the spec in that resource. In this example, the Operator watches the Apache resource as defined in the
quay.io/operator-framework/helm-operator:v0.15.1image as the base, and then it copies
watches.yamlfile and the Helm charts:
- Now you can build the Apache Operator by moving into the
apache-operatordirectory, and then entering the following
- Verify that the Docker image was created with:
You can think of Quay as something similar to GitHub, but for Docker images. It's a registry where you can host images and share them. There are a couple of ways to set up
quay.io with Docker. For the sake of simplicity, you'll use the
docker login command. It's important to note that the
docker login command stores the password you enter as plain-text. Thus, you should first generate an encrypted password.
- Point your browser to https://quay.io/, and then navigate to Account Settings:
- From the Account Settings page select Generate Encrypted Password:
- You will be prompted to enter your
- Select Docker Login and then copy the command containing your Docker encrypted password:
- In a terminal window, log in to
quay.ioby entering the following command:
Push the Apache Operator Image to quay.io
To do this, you first need to properly tag the image with the hostname of the registry, and the name of your repository. Then, you can push the image.
- Enter the following command to tag the local image named
Note that the image name is comprised by a slash-separated list of the:
- Registry hostname (
- Repository (
- Operator name (
☞ The name of our repository is
andreipope, but yours will be different.
- To push the image that we created in the previous section, run the following command
- Point your browser to https://quay.io/, navigate to the apache-operator repository, and make repository public:
Deploy the Apache Operator
You are now ready to deploy the Apache Operator. Before that, you must customize the specs.
- Open the
deploy/operator.yamlfile in a plain-text editor and update the placeholder
image: REPLACE_IMAGEwith the location of your image (
deploy.operator.yaml file should look similar to the following:
- Enter these
kubectl createcommands to deploy the Apache Operator:
- Check the status of the deployment:
☞ Note that the deployment doesn't define the spec for the Apache cluster. You’ll describe the Apache cluster in the next section, once the Operator is running.
- The Operator is a pod running in this deployment. To see it, type the following command:
Deploy the Apache Cluster
In the Set Up the Apache Operator section, you created a CRD defining a new kind of resource, an Apache cluster. Now, you will apply that spec so that the Operator starts watching the Apache resources. Then, you will deploy the Apache cluster itself.
- First, let's deploy the CRD that defines the resources the Operator will monitor:
- At this point, you are ready to deploy the Apache cluster:
- The deployment takes a bit of time to complete. Once everything is ready, you should see a new pod running Apache:
- You can also check that the deployment was created by entering the following command:
At this point, you have a running Apache cluster. To add another instance, you must modify the
replicaCount field in the
deploy/crds/appfleet.com_v1alpha1_apache_cr.yamlapache.appfleet.com/example-apache file. Then, you need to apply the new spec.
- Open the
deploy/crds/appfleet.com_v1alpha1_apache_cr.yamlapache.appfleet.com/example-apachefile in a plain-text editor, and specify
The updated file should look similar to the following:
- You can apply the updated spec with:
- After applying the updated spec, the state of the cluster differs from the desired state. The Operator starts a new instance of the Apache webserver to reconcile the two, scaling up the cluster:
Wait a bit until the second container is created:
In the above output, note that two
example-apache pods are running.
Verify Your Installation
The Apache Operator creates a Kubernetes service which is basically an endpoint where clients can obtain access to the group of pods running Apache. You will use this service to access your cluster using a web browser.
- Make sure that the service is up with:
- Forward the connections made to http://localhost:80 to the pod running the
- Point your browser to http://localhost:80. If everything worked well, you should see something like the following:
Now you should have a good understanding of how Kubernetes Operators work. Furthermore, by completing this tutorial you learned how create an operator for your application using a Helm chart.
Thanks for reading!
Published at DZone with permission of Sudip Sengupta. See the original article here.
Opinions expressed by DZone contributors are their own.