How to use Kompose to migrate Docker Compose applications to Kubernetes

The
You can download this article in PDF format to support us through the following link.

Download the guide as a PDF

turn off
The

The
The

Kompose (from the combined name of Kubernetes and docker-compose) is a tool that helps users familiar with docker-compose migrate to Kubernetes. It requires a Docker Compose file and converts it to Kubernetes resources (deployment, services, etc.). This guide will demonstrate how to use Kompose to migrate Docker Compose applications to Kubernetes.

Kompose’s cool features

  • Use Docker Compose to simplify the development process, and then deploy the container to the production cluster
  • Convert your docker-compose.yaml With a simple command kompose convert
  • use immediately kompose up
  • Put it down kompose down

What you need before you start

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

Install Minikube Kubernetes on CentOS 8 / CentOS 7 using KVM

Step 1: Install Kompose on Linux and macOS

Download the latest binary version of Kompose. You can check Kompose Github page Used for release version.

--- 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 then move it to / usr / local / bin table of Contents.

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

Check the version after installation:

$ kompose version
1.21.0 (992df58d8)

Step 2: Create sample images from Dockerfile

We will create a Nginx reverse proxy to proxy requests to the apache container

Create a file called Dockerfile and add the following content.

cat > Dockerfile<

This is the configuration file we want to copy into the image (nginx.conf)

cat >nginx.conf<

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

Step 3: Create a container image

Open a terminal in your project and enter the following command to use podman to build our image

$ 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 generating the image, we should be able to see the following

$ podman images

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

Step 3: Push the container image to the image registry

You can push the image to any public registry, or even to the registry you created / private.

This example uses the Docker Hub image registry.

$ podman tag  docker.io//reverseproxy:v1
Example:
$ podman tag localhost/reverseproxy:v1 docker.io/penchant/reverseproxy:v1

Verify your image registry – for me this is docker.io

$ podman login docker.io
Username:  
Password: 
Login Succeeded!

After logging in, push the image, for 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 use this guide to find more information about pushing and pulling images from DockerHub:

Use Podman to publish container images to Docker Hub / Image Registry

Step 4: Create docker-compose.yaml

This is the file that Docker Compose uses to create the service and run the multi-container environment for the example project. Please note that the image we are using is the image we 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 selected and expanded, we can use two options when running the project through kompose. First, we can convert the docker-compose.yaml file into separate yaml files (such as deployment, services, and persistentvolumeclaim), and then apply it using kubectl, or we can simply use a kompose up command to operate. Since I want to check each file, we will first convert 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 created three yaml files, and their names illustrate their role. One is service and the other is deployment.

We can continue to use kubectl to apply these files as shown 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 services, pods and deployment

$ 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               443/TCP    122m
service/reverseproxy   ClusterIP   10.97.210.141           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 fine-tuning each yaml file to suit a specific configuration, they can start kubernetes resources.

note:

You can change the detailed information in the yaml file that is derived after kompose operates on the Docker-Compose file according to your specific needs. The advantage is that kompose can complete most of the heavy work, you only need to add the content you need to add.

Final thoughts

Kompose is an excellent tool that lets you go from nothing to a usable Kubernetes application. Try it in your project to take advantage of the features developed by kompose for you and understand the features available.

If you are an explorer, please find other guides on conquest below:

How to use Podman and Lippod to run Docker containers

How to manually pull out the container image used by Kubernetes kubeadm

Top-level minimal container operating system 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

The
You can download this article in PDF format to support us through the following link.

Download the guide as a PDF

turn off
The

The
The

Sidebar