Why Kubernetes

Unless you’ve been hiding in a container for the past few months, you’ve probably heard of Kubernetes (often called k8s), the best container orchestration tool around. K8s configuration is a bit more involved than a simple Docker run command or Compose definition. However, in return for this complexity, you get a cluster that is fault tolerant, self-healing, and auto-scalable. If you are looking to move from native Docker tooling to k8s, I’ve created a tool for you!

Why This Project

After getting familiar with the basics of k8s, we decided to make the switch. I knew I needed to make a few files for each container: a Deployment to describe what to run, a Service to help with service discovery, and a ConfigMap to inject configuration.

But we had 20+ microservices, and the k8s docs are massive, which made it hard to find out how to create the right resources for each one. There’s a tool that’ll do it for you if your app has a Docker Compose file, but we didn’t use Compose, so we needed another solution.

I decided to create a new tool that will take an existing container and create all the resources necessary to get it up and running in k8s.

How Does It Work?

Kontainer inspects your Docker container and creates a deployment resource for you with the correct environment variables, ports, and Docker image. If ports are exposed, it creates a service based on the container name. And if volumes are mounted, it reads them and turns them into ConfigMaps.

This tool works with any Docker container, regardless of how you built it. This is useful if you use Ansible or another deployment tool (Chef, Puppet, etc.) because your container may have certain environment variables and files it injected that weren’t directly set by you.

Example

Enough explanation, let’s check it out! I’m assuming you have docker and kubectl installed and pointed to an existing k8s cluster on your system. This example will use a simple file server application which exposes a port and uses a bind mounted file. You can follow along by using these commands in your terminal.

  1. Create a file:
     echo "Hello" > served-file
    
  2. Run a simple file server container. This is the application we’ll create k8s resources for:
     docker run -d -p 8080:8080 -v `pwd`/served-file:/served/file anandkumarpatel/serve-file
    
  3. Run Kontainer to generate k8s files for the file server:
     docker run -it -v /:/host -v `pwd`/out:/output -v /var/run/docker.sock:/var/run/docker.sock anandkumarpatel/kontainer
    

Let’s take a look at what Kontainer generated in the ./out folder. You can see that it generated 3 folders:

  • ./Deployment: Maintains a certain number of containers with a given configuration with Pods and ReplicaSets.
  • ./Service: Provides simple load balancing and a static hostname for service discovery.
  • ./ConfigMap: Contains the contents of the served file.

These files will work out of the box! You can run kubectl create -f on all the files in the out folder to get this app running inside of k8s.

Conclusion

The files generated by Kontainer should get you up and running relatively quickly, but you probably shouldn’t keep these files this way for production. You should use these files as a starting point to make real production-ready configs. I took these files and turned them into templates for Ansible. A few suggestions:

  • ConfigMaps might have secrets in them that should be turned into Kubernetes secrets.
  • Environment variables that hold secrets should also pull their values from Kubernetes secrets.
  • If you bind mount volumes for databases, use the --remove-mounts flag to ignore them.

This project is still a work in progress and is missing a few things at the moment. Volumes that are internal are not turned into ConfigMaps. Creating and defining networks is not supported yet. There are many more Docker features to map. If you want to add something, feel free to open issues or create a Pull Request on GitHub!

I hope this helps you get onboard with Kubernetes faster! If you have any questions, feel free to tweet me @akaDJFaZe.