How to use Kompose to create a Kubernetes deployment file from a docker-compose file

Daniel AganDaniel Agan
3 min read

Kubernetes, also known as K8s, is an open-source system for automating the deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management and discovery.

Deploying your applications to a Kubernetes cluster might seem overwhelming if you are new to Kubernetes. You might be wondering how to get started and what you would need to get started. In this article, I would be introducing you to a tool called "Kompose" and how to use Kompose to convert your applications into Kubernetes deployments and services.

Prerequisites

Before you begin, you would need the following:

  • Knowledge about docker and docker technologies

  • Your application's docker image pushed to a docker registry

  • Your application docker-compose file

  • Knowledge about Kubernetes

What is Kompose?

Kompose is a conversion tool for Docker Compose to container orchestrators such as Kubernetes. It makes converting and deploying your application to kubernetes easier.

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, spin everything up or tear it all down. It provides an easier way to define and spin up docker containers for applications as everything about your application can be defined in a single YAML file.

When using Kompose to convert your application, It creates all the files and configurations required for your application to work in a Kubernetes cluster. These files and configurations include deployments, services, configmap, persistent volume claims etc.

How to install Kompose

There are various methods for downloading and installing Kompose and these methods can be found on the official Kompose website. The recommended and most preferred method is downloading the binary from the latest GitHub release.

On your linux terminal:

# Linux
curl -L https://github.com/kubernetes/kompose/releases/download/v1.28.0/kompose-linux-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

On your mac terminal:

# macOS
curl -L https://github.com/kubernetes/kompose/releases/download/v1.28.0/kompose-darwin-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

For windows, Download from GitHub and add the binary to your PATH.

For all other OS, you can check the Kompose official website on how to download for your OS.

How to use Kompose

To use Kompose, all you have to do is to navigate to the folder that contains your docker-compose file and run Kompose convert.

$ cd my_app_directory
$ kompose convert -f docker-compose.yaml
INFO Kubernetes file "frontend-service.yaml" created
INFO Kubernetes file "redis-master-service.yaml" created
INFO Kubernetes file "redis-slave-service.yaml" created
INFO Kubernetes file "frontend-deployment.yaml" created
INFO Kubernetes file "redis-master-deployment.yaml" created
INFO Kubernetes file "redis-slave-deployment.yaml" created

All the required configurations will be created and you can go ahead and edit them because sometimes the conversion is not 100% but it always gives you the closest possible configurations and you can edit it to suit your needs. If you are satisfied with the configurations you can then apply them to Kubernetes by running kubectl apply -f . and all the configurations will be applied.

$ kubectl get po
NAME                            READY     STATUS              RESTARTS   AGE
frontend-591253677-5t038        1/1       Running             0          10s
redis-master-2410703502-9hshf   1/1       Running             0          10s
redis-slave-4049176185-hr1lr    1/1       Running             0          10s

Conclusion

Kompose is a tool for developers that is very easy to use and saves a lot of time to do it manually. It also simplifies the development process as you can just push the compose file and it can be converted anywhere and used to deploy applications to Kubernetes.

I hope this article was helpful to you and you can now convert any application and deploy it to Kubernetes.

4
Subscribe to my newsletter

Read articles from Daniel Agan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Daniel Agan
Daniel Agan

I am a Devops engineering, looking to give technical writing a shot and share my knowledge as I grow in this space.