Working with Microservices-3: Installation and Use of The Kompose Conversion Tool.

Cumhur Akkaya
7 min readJul 24, 2023

--

We will convert our docker-compose.yaml to Kubernetes manifest yaml files using the Kompose tool. So, we easily deploy our microservices app into the Kubernetes cluster. You should definitely try it, it’s great convenience to deploy the app into the Kubernetes cluster. This simple program will save us from writing the 25 Kubernetes files of our Springboot microservice app. Kompose will save us a lot of time. We will do them all step by step.

1. What is the Kompose?

Kompose is a conversion tool for Docker Compose to container orchestrators such as Kubernetes (or OpenShift).

Kubernetes Manifest File is a configuration file written in a format called YAML or JSON, that describes the resources you want in your cluster. We convert docker-compose.yml to Kubernetes manifest yaml file using Kompose in this article. And we use the yaml file that we converted in our Kubernetes cluster to deploy our microservices app. It allows us to easily deploy our Kubernetes cluster, as shown in Figure-1.

Figure-1

We can choose a targeted provider using global option --provider. If no provider is specified, Kubernetes is set by default. (1)

Kompose supports Docker Compose versions: 1, 2 and 3. We have limited support on versions 2.1 and 3.2 due to their experimental nature. A full list on compatibility between all three versions is listed in our conversion document including a list of all incompatible Docker Compose keys. (1)

2. Installing Kompose tool

The kompose tool have multiple ways to install. The most preferred method is downloading the binary from the latest GitHub release. Kompose is released via GitHub, you can see all current releases on the GitHub release page. (2)

Install the Kompose by using the command below, as shown in Figure-2.

curl -L https://github.com/kubernetes/kompose/releases/download/v1.26.1/kompose-linux-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose
kompose version
Figure-2

3. Creating and working in different branches on Jenkins Server

Create a “staging” branch, with “git branch staging” command. And switch to the “staging” branch, with “git checkout staging” command, as shown in Figure 3. (1) In this way, when an error occurs, we can recover the project by reverting to a working branch. This is the purpose and biggest benefit of using git anyway.

4. Creating a docker-compose.yml

On the Development server, we used the “docker-compose.yml” to create and start all the services of our microservices app from our Dockerfiles, check this article.

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. (3) It’s great convenience to deploy the app into the docker container.

If you don’t have the docker-compose file, create a “docker-compose.yml” under the “k8s” folder with the following content to use in the conversion, as shown in Figure-3. With Kompose, we convert docker-compose.yml to the Kubernetes manifest yaml files.

Save the root password and user of the MySQL server in the docker-compose.yml.

version: '3'
services:
config-server:
image: "{{ .Values.IMAGE_TAG_CONFIG_SERVER }}"
ports:
- 8888:8888
labels:
kompose.image-pull-secret: "regcred"
discovery-server:
image: "{{ .Values.IMAGE_TAG_DISCOVERY_SERVER }}"
ports:
- 8761:8761
labels:
kompose.image-pull-secret: "regcred"
customers-service:
image: "{{ .Values.IMAGE_TAG_CUSTOMERS_SERVICE }}"
deploy:
replicas: 2
ports:
- 8081:8081
labels:
kompose.image-pull-secret: "regcred"
visits-service:
image: "{{ .Values.IMAGE_TAG_VISITS_SERVICE }}"
deploy:
replicas: 2
ports:
- 8082:8082
labels:
kompose.image-pull-secret: "regcred"
vets-service:
image: "{{ .Values.IMAGE_TAG_VETS_SERVICE }}"
deploy:
replicas: 2
ports:
- 8083:8083
labels:
kompose.image-pull-secret: "regcred"
api-gateway:
image: "{{ .Values.IMAGE_TAG_API_GATEWAY }}"
deploy:
replicas: 1
ports:
- 8080:8080
labels:
kompose.image-pull-secret: "regcred"
kompose.service.expose: "{{ .Values.DNS_NAME }}"
kompose.service.type: "nodeport"
kompose.service.nodeport.port: "30001"
tracing-server:
image: openzipkin/zipkin
environment:
- JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -Djava.security.egd=file:/dev/./urandom
ports:
- 9411:9411
admin-server:
image: "{{ .Values.IMAGE_TAG_ADMIN_SERVER }}"
ports:
- 9090:9090
labels:
kompose.image-pull-secret: "regcred"
hystrix-dashboard:
image: "{{ .Values.IMAGE_TAG_HYSTRIX_DASHBOARD }}"
ports:
- 7979:7979
labels:
kompose.image-pull-secret: "regcred"
grafana-server:
image: "{{ .Values.IMAGE_TAG_GRAFANA_SERVICE }}"
ports:
- 3000:3000
labels:
kompose.image-pull-secret: "regcred"
prometheus-server:
image: "{{ .Values.IMAGE_TAG_PROMETHEUS_SERVICE }}"
ports:
- 9091:9090
labels:
kompose.image-pull-secret: "regcred"

mysql-server:
image: mysql:5.7.8
environment:
MYSQL_ROOT_PASSWORD: petclinic
MYSQL_DATABASE: petclinic
ports:
- 3306:3306
Figure-3

5. Running the Kompose tool

Run the Kompose under the K8s folder (Because the docker-compose.yml file is there.), and convert the “docker-compose.yml” into “k8s/petclinic_chart/templates”. The .yaml files are saved under the “k8s/petclinic_chart/templates” folder, as shown in Figure-4.

kompose convert -f docker-compose.yml -o petclinic_chart/templates
Figure-4

6. Updating deployment files

We must update deployment files with “init-containers” to launch microservices in sequence. The reason we do this is because Kompose doesn’t know which container to start in which order. So, we have to specify “the starting order” in the Kubernetes manifest yaml files.

Change the “discovery server, admin-server, api-gateway, customers-service, hystrix-dashboard, vets-service and visits service” by using the command below, as shown in Figures 5–11

For more information check for “Init Containers” at this link.

# for discovery server
initContainers:
- name: init-config-server
image: busybox
command: ['sh', '-c', 'until nc -z config-server:8888; do echo waiting for config-server; sleep 2; done;']
# for admin-server, api-gateway, customers-service, hystrix-dashboard, vets-service and visits service
initContainers:
- name: init-discovery-server
image: busybox
command: ['sh', '-c', 'until nc -z discovery-server:8761; do echo waiting for discovery-server; sleep 2; done;']
Figure 5 - Discovery-server update
Figure 6 - Admin-server update
Figure 7 - Api-gateway update
Figure 8 - Customers-service update
Figure 9 - Hystrix-dashboard update
Figure 10 - Vet-service update
Figure 11 - Visit-service update

We must update “spec.rules.host” field of “api-gateway-ingress.yaml” file and add “ingressClassName: nginx” field under the “spec” field.

This is a “bug” of the Kompose here; some letters are lowercase, and we are also fixing them, as shown in Figure-12.

spec:
ingressClassName: nginx
rules:
- host: '{{ .Values.DNS_NAME }}'
Figure 12 - api-gateway-ingress.yaml file update

7. Pushing created files to the remote repo (GitHub)

Commit the change, then push created files (the docker-compose and Kubernetes manifest yaml files, etc) to the remote repo (GitHub). Then, checkout to the “main” branch. Run the command below, as shown in Figure 30.

The best practice is to create a different branch and continue from the new branch in the next stage.

git add .
git commit -m 'added the docker-compose and kubernetes manifest yaml files for local deployment'
git push --set-upstream origin staging
git checkout main

8. As a result

We easily converted docker-compose.yml to Kubernetes manifest yaml files using Kompose, as shown in Figures 5-12. This simple program saved us from writing 25 Kubernetes files. Kompose has saved us time.

You can review the article below to check the use of Kubernetes manifest yaml files we prepared with the Kompose tool.

Share this article with friends in your network and help others to upskill.
I frequently share articles about Cloud and DevOps tools and resources. Follow me on Medium so you don’t miss future articles.

For more info and questions, please contact me on Linkedin.

9. Next post

In the next article, “Microservice-4: Set up a Helm v3 chart repository in Amazon S3 for CI/CD pipeline in Jenkins”;

We will install Helm, and create Helm charts by integrating the Helm repository into Amazon Simple Storage Service (Amazon S3). We will keep Kubernetes manifest yaml files of the microservices app in Amazon S3 anymore, as shown in Figure-13.

Then we will be able to install the Springboot app consisting of 10 microservices on the Kubernetes cluster with a single command.

Figure-13

--

--

Cumhur Akkaya

✦ DevOps/Cloud Engineer, ✦ Believes in learning by doing, ✦ Dedication To Lifelong Learning, ✦ Tea and Coffee Drinker. ✦ Linkedin: linkedin.com/in/cumhurakkaya