sebgoa sebgoa on docker, kompose, kubernetes

Using your Docker-compose files with Kubernetes

Docker is terrific for developers. It allows everyone to get started quickly with an application that has been packaged in a Docker image and is available on a Docker registry. Just pull an image from Docker Hub for example, and use the docker run command to get it running.

After this first step using Docker, folks usually start building and publishing their own images. Then they start to build a more real application composed of multiple containers. In Docker world, this is made extremely easy with the docker-compose tool. You can run your multi-service application on your local host or target a Swarm cluster and get the containers scheduled on different nodes.

But what if you wanted to use something else than Swarm ? The Docker-compose format is not a standard to define distributed applications. Folks at Rancher for example have built on Compose to match their own orchestrator.

At Skippbox, we started several months ago to develop kompose a tool to launch an application defined in a Docker-compose file onto a Kubernetes cluster. That is, use the same Docker-compose file that you have developed and are using with Docker, but automatically create corresponding Kubernetes resources. We see it as a terrific way to expose Kubernetes principles to Docker users.

Over the summer, Kompose has found a new gear with help from Tomas Kral and Suraj Deshmukh from Red Hat, and Janet Kuo from Google. Together with our own lead kompose developer Nguyen An-Tu they are making kompose even more exciting.

Kompose now supports Docker-compose v2 format, it can also translate to OpenShift configurations and handles more Docker-compose features.

Let's take it for a spin !

The code is open source and available on GitHub. I will skip the build instructions and dive straight into an example.

Guestbook application

The Guestbook application has become the canonical example for Kubernetes. In Docker-compose format, the guestbook can be started with this minimal file:

version: "2"

services:  
  redis-master:
    image: gcr.io/google_containers/redis:e2e
    ports:
      - "6379"
  redis-slave:
    image: gcr.io/google_samples/gb-redisslave:v1
    ports:
      - "6379"
    environment:
      - GET_HOSTS_FROM=dns
  frontend:
    image: gcr.io/google-samples/gb-frontend:v4
    ports:
      - "80:80"
    environment:
      - GET_HOSTS_FROM=dns

It consists of three services. A redis-master node, a set of redis-slave that can be scaled and find the redis-master via its DNS name. And a PHP frontend that exposes itself on port 80. The resulting application allows you to leave short messages which are stored in the redis cluster.

To get it started with docker-compose on a vanilla Docker host do:

$ docker-compose -f docker-guestbook.yml up -d
Creating network "examples_default" with the default driver  
Creating examples_redis-slave_1  
Creating examples_frontend_1  
Creating examples_redis-master_1  

And now open your browser at http://localhost:80 and you will enjoy the guestbook as seen in the screenshot below

guestbook-docker

Using kompose

kompose is a single Golang binary that you build or install from the release on GitHub. It currently has two main commands up and convert. Here for simplicity we will show a single usage to bring up the Guestbook application.

Similary to docker-compose, use the kompose up command pointing to the Docker-compose file representing the Guestbook application. Like so:

$ kompose up -f ./examples/docker-guestbook.yml
We are going to create Kubernetes deployment and service for your dockerized application.  
If you need more kind of controllers, use 'kompose convert' and 'kubectl create -f' instead.

INFO[0000] Successfully created service: redis-master  
INFO[0000] Successfully created service: redis-slave  
INFO[0000] Successfully created service: frontend  
INFO[0000] Successfully created deployment: redis-master  
INFO[0000] Successfully created deployment: redis-slave  
INFO[0000] Successfully created deployment: frontend

Application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc' for details.  

kompose automatically converted the Docker-compose file into Kubernetes objects. In this case, it created one deployment and one service per compose services. In addition it automatically detected your current Kubernetes endpoint and created the resources onto it.

That's it !

Now, if you already know Kubernetes a bit, you are familiar with the client kubectl and you can check what was created on your cluster.

$ kubectl get pods,svc,deployments
NAME                             READY        STATUS        RESTARTS     AGE  
frontend-3780173733-0ayyx        1/1          Running       0            1m  
redis-master-3028862641-8miqn    1/1          Running       0            1m  
redis-slave-3788432149-t3ejp     1/1          Running       0            1m  
NAME                             CLUSTER-IP EXTERNAL-IP   PORT(S)      AGE  
frontend                         10.0.0.34  <none>        80/TCP       1m  
redis-master                     10.0.0.219 <none>        6379/TCP     1m  
redis-slave                      10.0.0.84  <none>        6379/TCP     1m  
NAME                             DESIRED    CURRENT       UP-TO-DATE   AVAILABLE   AGE  
frontend                         1            1             1            1           1m  
redis-master                     1            1             1            1           1m  
redis-slave                      1            1             1            1           1m  

Indeed you see the three services, the three deployments and the resulting three pods. To access the application quickly, run a proxy and access the frontend service locally like so:

$ kubectl proxy
...
$ open http://localhost:8001/api/v1/proxy/namespaces/default/services/frontend:80/

kompose

You should enjoy the Guestbook application that you know and love, but this time it was started from a Docker-compose file.

Hopefully this gave you a quick tour of kompose and got you excited. They are more exciting features, like creating different type of resources and even creating Helm charts.

We are currently in the process of proposing kompose for the Kubernetes incubator, hopefully it will get accepted and one day graduate to an official Kubernetes community tool.