First Steps with the Kubernetes Operator
This article goes through some of the initial steps to installing Kubernetes Operator and some additional tools to help add it to the product.
Join the DZone community and get the full member experience.Join For Free
This blog post demonstrates how you can use the Operator Lifecycle Manager to deploy a Kubernetes Operator to your cluster. Then, you will use the Operator to spin up an Elastic Cloud on Kubernetes (ECK) cluster.
An operator is a software extension that uses custom resources (extension of the Kubernetes API) to manage complex applications on behalf of users.
The artifacts that come with an operator are:
- A set of CRDs that extend the behavior of the cluster without making any change in the code
- A controller that supervises the CRDs, and performs various activities such as spinning up a pod, take a backup, and so on.
The complexity encapsulated within an Operator can vary, as shown in the below diagram:
A Kubernetes cluster (v1.7 or newer) with a control plane and two workers. If you don’t have a running Kubernetes cluster, refer to the “Create a Kubernetes Cluster with Kind” section below.
Create a Kubernetes Cluster with Kind (Optional)
Kind is a tool for running local Kubernetes clusters using Docker container "nodes." Follow the steps in this section if you don't have a running Kubernetes cluster:
- Install kind by following the steps from the Kind Quick Start page.
- Place the following spec into a file named
3. Create a cluster with a control plane and two worker nodes by running the Kind
create cluster command followed by the
--config flag and the name of the configuration file:
4. At this point, you can retrieve the list of services that were started on your cluster:
Install the Operator Lifecycle Manager
In this section, you'll install the Operator Lifecycle Manager ("OLM"), a tool that helps you manage the Operators deployed to your cluster in an automated fashion.
- Run the following commands to install OLM:
Install the ECK Operator
The ECK Operator provides support for managing and monitoring multiple clusters, upgrading to new stack versions, scaling cluster capacity, etc. This section walks through installing the ECK Operator to your Kubernetes cluster:
- Enter the following
kubectl applycommand to install the ECK Operator:
2. Remember that the ECK Operator is a Kubernetes resource. Thus, you can display it by using the following command:
3. To see more details about a specific CRD, run the
kubectl describe CustomResourceDefinition command followed by the name of the CRD:
This output was truncated for brevity.
- You check the progress of the installation with:
Note that the above output was truncated for brevity.
- List the pods running in the
Make sure the status is
Running before moving on.
Deploy an Elasticsearch Cluster
In this section, we'll walk you through the process of deploying an Elasticsearch cluster with the Kubernetes Operator.
- Create a file called
elastic-search-cluster.yamlwith the following content:
Things to note in the above output:
versionparameter specifies the Elasticsearch version the Operator will deploy
countparameter sets the number of database nodes. Make sure it's not greater than the number of nodes in your Kubernetes cluster.
- Create a two-node Elasticsearch cluster by entering the following command:
Behind the scenes, the Operator automatically creates and manages the resources needed to achieve the desired state.
- You can now run the following command to see the status of the newly created Elasticsearch cluster:
Note that the
HEALTH status has not been reported yet. It takes a few minutes for the process to complete. Then, the
HEALTH status will show as
4. Check the status of the pods running in your cluster with:
Verify Your Elasticsearch Installation
To verify the installation, follow these steps.
- The Operator exposes the service with a static IP address. Run the following
kubectl get servicecommand to see it:
2. To forward all connections made to
localhost:9200 to port 9200 of the pod running the
quickstart-es-http service, type the following command in a new terminal window:
3. Move back to the first terminal window. The password for the
elastic user is stored in a Kubernetes secret. Use the following command to retrieve the password, and save it into an environment variable called
4. At this point, you can use
curl to make a request:
This section walks through creating a new Kibana cluster using the Kubernetes Operator.
- Create a file called
kibana.yamlwith the following content:
2. Enter the following
kubectl apply command to create a Kibana cluster:
3. During the installation, you can check on the progress by running:
Note that in the above output, the
HEALTH status hasn't been reported yet.
Once the installation is completed, the
HEALTH status will show as green:
4. At this point, you can list the Kibana pods by entering the following
kubectl get pods command:
Verify Your Kibana Installation
Follow these steps to verify your Kibana installation.
- The Kubernetes Operator has created a
ClusterIPservice for Kibana. You can retrieve it like this:
2. To make the service available on your host, type the following command in a new terminal window:
3. To access Kibana, you need the password for the
elastic user. You've already saved it into an environment variable called
PASSWORD in Step 3 of the Verify Your Elasticsearch Installation section. You can now display it with:
In our example, the password is
vrfr6b6v4687hnldrc72kb4q but yours will be different.
- Now, you can access Kibana by pointing your browser to https://localhost:5601
5. Log in using the
elastic username and the password you retrieved earlier:
Manage Your ECK Cluster with the Kubernetes Operator
In this section, you'll learn how to scale down and up your ECK Cluster.
- To scale down, modify the number of nodes running Elasticsearch by specifying
nodeSets.count: 1in your
elasticsearch.yamlfile. Your spec should look like this:
2. You can apply the spec with:
Behind the scenes, the Operator makes required changes to reach the desired state. This can take a bit of time.
- In the meantime, you can display the status of your cluster by entering the following command:
In the above output, note that there's only one node running Elasticsearch.
- You can list the pods running Elasticsearch:
5. Similarly, you can scale up your Elasticsearch cluster by specifying
nodeSets.count: 2 in your
6. You can monitor the progress with:
Once the desired stats is reached, the
PHASE column will show as
Congratulations, you've covered a lot of ground, and now you are familiar with the basic principles behind the Kubernetes Operator! In a future post, we'll walk through the process of writing our own Operator. Until then, here are some more Kubernetes and Docker best practices for managing and deploying containers.
Thanks for reading!
Published at DZone with permission of Sudip Sengupta. See the original article here.
Opinions expressed by DZone contributors are their own.