Orchestrate secure etcd deployments with Kubernetes and Operator

Applying transport level security (TLS) to your distributed key-value store transactions

Todd Kaplinger
IBM Cloud
8 min readMay 17, 2017

--

Overview

Skill Level: Beginner

Some basic understanding of Node.js, Docker and Kubernetes
etcd is a distributed key value store that provides a reliable way to store data across a cluster of machines. In this recipe, I will demonstrate how to securely store objects in etcd using a simple Node.js Express Application.

Ingredients

To get started, you should have an elementary understanding of Kubernetes and have installed Minikube, Docker and Node.js locally. In this tutorial, I will demonstrate some basic concepts around deploying Docker containers using etcd Operator and a very basic Node.js application to show how to interact with etcd to validate the concepts.

Why etcd Operator?

The origin of this recipe started as a way for me to learn about Kubernetes and Operator. I came across a great GitHub project from CoreOS when reading their blog that introduced the etcd Operator, a data-store for all kinds of Kubernetes data, particularly key values. This provided a great jumpstart for my research as it also had building blocks for how to enable TLS for secure communication between the clients and etcd. This recipe is basically my attempt to assemble all of the steps that were required to demonstrate etcd, Operator and TLS in a developer’s local MiniKube environment. I show all of the necessary source artifacts used in this recipe (and also keep the names the same as the GitHub project) in this recipe but reorganized to keep the article clear and concise.

Author note: If you plan to use this recipe in your own local Minikube environment, I would suggest you git clone the repository into your local developer workspace. This will make editing the various YAML files easier and also give you access to the various digital certificates used in the recipe.

Install etcd Operator

Prior to deploying the etcd cluster, you first need to create the etcd Operator that will register the etcd Operator as a Kubernetes Third Party Resource (TPR). The etcd operator is described by a yaml file and that references the etc-operator image from CoreOS. Create this yaml file:

deployment.yaml

If you haven’t already, start Minikube by running this command.

To install the etcd operator and verify the Operator has been installed by querying the third party resources for your cluster, execute the following kubectl commands.

If the deployment executed correctly, there should now be a third party resource named “cluster.etcd.coreos.com” that will registered with your kubernetes environment.

Creating etcd cluster

Now that you have deployed the third party resource that understands how to perform the Operator functions for etcd, you can now create a cluster that leverages Operator. Create this yaml file:
example-etcd-cluster.yaml

The syntax for deploying a cluster is quite simple and straightforward. In the above yaml, the deployment will be of type “Cluster” and have an initial size of “5” leveraging the etcd Operator from CoreOS.

Now that you have the yaml defined, you can now execute the kubectl command with the “apply” option. This will allow us to modify this yaml file and simply rerun the command again to make changes to the cluster such as scaling up or scaling down the instances.

Once this command has completed, the etcd cluster has been created and etcd is available. To test that the etcd cluster can store and retrieve keys, I wrote the following test shell script to obtain the MiniKube IP address, store the key “message” in etcd and retrieve the key “message” from etcd using etcd’s REST APIs. This sniff test will allow us to verify the basic function of the cluster from an external perspective as Minikube provides a basic HTTP gateway to deployment resources. In this example, the etcd deployment is listening on port 2379 (default port for etcd)

Configuring TLS for the etcd cluster

Up to this point, you have done some basic set up of the etcd cluster and have used Operator to drive the deployment. You are now going to use Operator to update live deployment to enable TLS for the etcd cluster endpoints. Using the original yaml file, you are going to introduce TLS properties that will configure the digital certificates. For the sake of simplicity of this article, you are going to use the pem files that are part of the github project. For reference purposes, these pem files are located in the folder etcd-operator/example/tls.

Prior to updating the cluster, the Kubernetes cluster needs to have some details to know how to handle TLS. These properties will be set as Kubernetes secrets as follows.

Now that these secrets have been created, you need to keep track of what you named them for the next step in which you update the yaml file with those values as show below.

example-etcd-cluster.yaml

As you can see above, you have appended a set of properties for TLS. These secrets will be pulled at runtime from the Kubernetes environment and you are now ready to write an app to test our SSL support. Now lets update the cluster by applying these updates to the cluster.

Creating Node.js Application

Now that you have deployed the changes to the cluster, it would be nice to validate our secure endpoints. Since I am familiar with Node.js, I wrote a simple Express application that can store data and retrieve data from etcd. To prove that the endpoint is secured with TLS, you will set the endpoint to etcd to be accessible via HTTPS and also leverage the same client certificates (operator-etcd-client-tls) set above so that the handshake between the client (Node app) and server (etcd) does not fail. To get started with this application, create a new project in your workspace and create a file named server.js in the root of the project.

server.js

Now that you have created the server.js resource and saved it locally, it is now time to create the package.json. The simplest way to do this is to npm install the 3 modules that were referenced and initialize the package.json for this project. These can be run from the command line of your project workspace.

At this point in time, you can now create the Docker image that can be deployed to Minikube.

Deploying Node.js Container

Since you want secure communication within our cluster, the application above needs to be deployed to the same MiniKube cluster that you have deployed etcd to. To get started, the first step is to package the Node app as a container.

The configuration below packages the Node app as a basic docker container and exposed the port 8080 (same as what the app is listening on above). Inside of the container is the dependencies that were added as part of the npm install, the digital certificates for the client/server handshake and of course the Express app. In the root of your application workspace (peer to server.js and the package.json), create the Docker file with the following.

Dockerfile

Great.. Now you have the Dockerfile written, you now need to build the image and deploy it to MiniKube for testing.

Summary

In the above recipe, I demonstrated how to deploy a secure etcd service to Kubernetes leveraging Operator. The scenario above will be a common use case for enterprise deployments where transporting confidential data requires secured endpoints using digitial certificates. While I only covered a small portion of what the etcd Operator can support, this flow will be foundational for more advanced deployments, such as SSL certificate revocation, deploying new certificates, and securely handling backup and restore of clusters.

I want to thank CoreOS and the etcd-operator community for hosting their project on GitHub. Their assistance with this scenario was extremely helpful and really allowed me to quickly create my environment and validate the scenario.

This article was originally published at https://developer.ibm.com/recipes/tutorials/orchestrate-secure-etcd-deployments-with-kubernetes-and-operator/ on May 17, 2017 (updated May 29).

--

--

Todd Kaplinger
IBM Cloud

Vice President SW Engineering — Chief Architect, Retail Solutions@NCR Voyix. The opinions expressed here are my own. Follow me on Twitter @todkap