Using your Docker compose file with Kubernetes

Table of Contents


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 Kubernetes to run your application defined in a docker-compose file?

Kompose is the tool to do just that. It is currently under discussion in the Kubernetes SIG-apps to join the Kubernetes incubator.

Kompose supports Docker-compose v1 and v2 format as well as the new Docker bundle format It can also translate to OpenShift configurations.

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"

      - "6379"
      - "6379"
      - GET_HOSTS_FROM=dns
      - "80:80"
      - 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

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                 <none>        80/TCP       1m
redis-master            <none>        6379/TCP     1m
redis-slave              <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/

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.


Tags: Kubernetes