kompose (Derived from the amalgamation of Kubernetes and docker-compose names) is a tool to help users familiar with docker-compose move to Kubernetes. It takes a Docker Compose file and translates it into Kubernetes resources (deployments, services, etc). This guide will demonstrate how one migrate Docker Compose Applications to Kubernetes With Kompose.

Super cool Features of Kompose

  • Simplify your development process with Docker Compose and then deploy your containers to a production cluster
  • Convert your docker-compose.yaml with one simple command kompose convert
  • Immediately bring up your cluster with kompose up
  • Bring it back down with kompose down

What is needed before we begin

Before we proceed with the guide, we assume you have a Kubernetes cluster, and the kubectl command-line tool already configured to communicate with your cluster. If these requirements are not met, you can set up a simple single node Kubernetes cluster using minikube by following the guide below. If you are ready to proceed, continue to install kompose.

Install Minikube Kubernetes on CentOS 8 / CentOS 7 with KVM

Step 1: Getting Kompose Installed on Linux, macOS

Download the latest binary release of Kompose. You can check Kompose Github Page for release versions.

--- Linux ---
curl -s https://api.github.com/repos/kubernetes/kompose/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
mv kompose-linux-amd64 kompose


--- macOS ---
curl -s https://api.github.com/repos/kubernetes/kompose/releases/latest | grep browser_download_url | grep darwin-amd64 | cut -d '"' -f 4 | wget -qi -
mv kompose-darwin-amd64 kompose

Make the binary file executable and move it to the /usr/local/bin directory.

chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

Check version once installed:

$ kompose version
1.21.0 (992df58d8)

Step 2: Create a sample image from Dockerfile

We are going to create Nginx reverse proxy that proxies requests to an apache container

Create a file named Dockerfile and add the contents below.

cat > Dockerfile<<EOF
# Sample Dockerfile for Kompose
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 8081
EOF

And this is the config file (nginx.conf) we are copying into the image

cat >nginx.conf<<EOF

    upstream apache-container {
        server 0.0.0.0:80;
    }

    server {
        listen 8081;

        location / {
            proxy_pass         http://apache-container;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
EOF

It is a simple reverse proxy listening for requests at port 8081 then routes the traffic to the upstream server listening at port 80.

Step 3: Build a container image

Open a terminal in your project and type the following command to build our image using podman

$ podman build -t reverseproxy:v1 .
STEP 1: FROM nginx:alpine
Getting image source signatures
Copying blob cbdbe7a5bc2a done  
Copying blob c554c602ff32 done  
Copying config 89ec9da682 done  
Writing manifest to image destination
Storing signatures
STEP 2: COPY nginx.conf /etc/nginx/conf.d/default.conf
--> d86b4e89749
STEP 3: EXPOSE 8081
STEP 4: COMMIT reverseproxy:v1
--> b00d80e0056
b00d80e00560c205e0580d4b751b1a67da7e77e7fc73803b006ff10389bb4732


-- With Docker ---
$ docker build -t reverseproxy:v1 .

After the image has been built, we should be able to see it as follows

$ podman images

REPOSITORY                        TAG          IMAGE ID       CREATED             SIZE
localhost/reverseproxy                            v1        b00d80e00560   19 seconds ago   21.2 MB

Step 3: Push Container image to Image registry

You can push the image to any of the public registries or even to one you have created/private.

This example used Docker Hub image registry.

$ podman tag <local-image> docker.io/<username>/reverseproxy:v1
Example:
$ podman tag localhost/reverseproxy:v1 docker.io/penchant/reverseproxy:v1

Authenticate to your Image registry – for me this is docker.io

$ podman login docker.io
Username: <username> 
Password: <password>
Login Succeeded!

Once logged in, push the image, example:

$ podman push docker.io/penchant/reverseproxy:v1
Getting image source signatures
Copying blob 113ce2720837 done  
Copying blob 3810cc0c140f done  
Copying blob 3e207b409db3 done  
Copying config b00d80e005 done  
Writing manifest to image destination
Storing signatures

You can find more about pushing and pulling of images from DockerHub using this guide:

Publish Container Images to Docker Hub / Image registry with Podman

Step 4: Create a docker-compose.yaml

This is a file that Docker Compose would use to create services and run a multi-container environment for our sample project. Notice the image we are using is the one we have just built and pushed to DockerHub

$ vim docker-compose.yaml

version: "3"
services:
    reverseproxy:
        image: docker.io/penchant/reverseproxy:v1
        ports:
            - 8081:8081
        restart: always

    apache:
        depends_on:
            - reverseproxy
        image: httpd:alpine
        restart: always

After our docker-compose.yaml file is spick and span, there are two options we can exploit while running the project via kompose. First, we can convert the docker-compose.yaml file to separate yaml files such as deployment, service and persistentvolumeclaim then use kubectl to apply them or we can simply use one kompose up command to do the magic. Since I would wish to examine the separate files, we are going to convert first as follows:

$ kompose convert

INFO Kubernetes file "reverseproxy-service.yaml" created 
INFO Kubernetes file "apache-deployment.yaml" created 
INFO Kubernetes file "reverseproxy-deployment.yaml" created

Now we have three yaml files created with names that explain what they do. One is a service, the other are a deployments.

We can proceed to apply these files using kubectl as illustrated below

$ kubectl apply -f reverseproxy-service.yaml,apache-deployment.yaml,reverseproxy-deployment.yaml

service/reverseproxy created
deployment.apps/apache created
deployment.apps/reverseproxy created

Check the service, pods, and deployments

$ kubectl get deployment,svc,pods

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/apache         1/1     1            1           113m
deployment.apps/reverseproxy   1/1     1            1           105m

NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE 
service/kubernetes     ClusterIP   10.96.0.1       <none>        443/TCP    122m
service/reverseproxy   ClusterIP   10.97.210.141   <none>        8081/TCP   113m

NAME                                READY   STATUS    RESTARTS   AGE
pod/apache-7945cd6844-5hpf8         1/1     Running   0          113m
pod/reverseproxy-8646fb7c4f-j6742   1/1     Running   0          105m

As you can see, even before the respective yaml files are fine-tuned to befit specific configurations, they were able to launch kubernetes resources.

Note:

You can change details in the yaml files derived after kompose acts upon Docker-Compose files to your specific needs. The good thing is that kompose does most of the heavy-lifting and you can simply add what you need to add.

Final thoughts

Kompose is a brilliant tool and can get you from absolutely nothing to a working Kubernetes application. Try it out in your projects to see the power that you can take advantage of by leveraging on what has been developed in kompose for you.

Learning courses to try:

$14.18
$153.64
in stock
Udemy.com
$14.18
$118.18
in stock
Udemy.com
$14.18
$153.64
in stock
Udemy.com
$14.18
$153.64
in stock
Udemy.com

If you are quite the explorer, find other guides below for your conquest:

How To run Docker Containers using Podman and Libpod

How To Manually Pull Container images used by Kubernetes kubeadm

Top Minimal Container Operating Systems for running Kubernetes

Add Harbor Image Registry Pull Secret to Kubernetes / OpenShift

Configure Active Directory (AD) Authentication for Harbor Registry

How To Install Minikube on Ubuntu 18.04 / Debian 10 Linux

LEAVE A REPLY

Please enter your comment!
Please enter your name here