Docker Compose to Kubernetes: Step-by-Step Migration

Docker Compose to Kubernetes migration FI
Kubernetes

Share Post Now :

HOW TO GET HIGH PAYING JOBS IN AWS CLOUD

Even as a beginner with NO Experience Coding Language

Explore Free course Now

Table of Contents

Loading

Docker Compose has been a popular choice for developers to define and manage multi-container applications. It allows for easy local development and testing by defining services, networks, and volumes in a simple YAML format. However, as applications scale and evolve, it becomes necessary to adopt more robust and scalable orchestration platforms like Kubernetes.

This post will outline some of the benefits of switching from Docker Compose to Kubernetes, offer a step-by-step tutorial on how to do it using Kompose.

Why Migrate from Docker Compose to Kubernetes?

Moving from Docker Compose to Kubernetes offers several advantages, especially when considering more complex and production-oriented applications. While Docker Compose is a useful tool for managing simple containerized applications on a single host, Kubernetes provides a more robust and scalable solution for managing containerized applications in a distributed environment.

Understand Kubernetes Concepts

Before diving into migration, it’s crucial to familiarize yourself with Kubernetes concepts. Key elements such as Pods, Deployments, Services, ConfigMaps, and Secrets form the building blocks of a Kubernetes application. Understanding these components will help you design a robust Kubernetes architecture that meets your application’s requirements.

Before You Begin

Before proceeding with this tutorial, ensure that you have access to a functioning Kubernetes cluster, and that the kubectl command-line tool is properly configured to interact with the cluster. It is advisable to run this guide on a Kubernetes cluster that consists of at least two nodes, with none of them acting as control plane hosts.

1. Create a k8s cluster by referring to How To Setup A Three Node Kubernetes Cluster Step By Step

2. Once the k8s cluster is installed then please install docker in the master node as we will need it to run compose files.

Install Docker by referring to How to Install Docker on Ubuntu: Step-By-Step Guide

3. Install Docker Compose:

$ sudo apt install docker-compose

Docker Compose Configuration

Docker Compose is a tool that coordinates multi-container applications using a single configuration file. This file enables you to specify various container types with details like build settings, restart policies, volumes, and network configurations. Below is the Docker Compose file for the application that we will be translating.

docker-compose.yaml:

version: "2"

services:

  redis-master:
    image: registry.k8s.io/redis:e2e
    ports:
      - "6379"

  redis-slave:
    image: gcr.io/google_samples/gb-redisslave:v3
    ports:
      - "6379"
    environment:
      - GET_HOSTS_FROM=dns

  frontend:
    image: gcr.io/google-samples/gb-frontend:v4
    ports:
      - "80:80"
    environment:
      - GET_HOSTS_FROM=dns
    labels:
      kompose.service.type: NodePort

This Docker Compose configuration file defines three services: redis-master, redis-slave, and frontend. Let’s understand what this configuration file will do when executed with Docker Compose:

  • redis-master Service: It will create a container using the image registry.k8s.io/redis:e2e, which likely represents a Redis server for testing or development purposes. The Redis server container will be accessible from the host and other containers on the same network through port 6379, as it is exposed in the container. This service will operate as the master Redis node.
  • redis-slave Service: It will create another container using the image gcr.io/google_samples/gb-redisslave:v3, which represents a Redis slave node.
    Similar to the redis-master service, the Redis slave container will expose port 6379 to enable communication from other services or applications. The container’s environment is configured with GET_HOSTS_FROM=dns, which suggests that it might use DNS resolution to find and connect to other services. In this case, it might use DNS to discover the redis-master service.
  • frontend Service: This service sets up a frontend application using the image gcr.io/google-samples/gb-frontend:v4.
    The container’s port 80 is mapped to port 80 of the host machine, making the frontend accessible on port 80 of the host.
    Similar to the redis-slave service, the container’s environment is configured with GET_HOSTS_FROM=dns, which could imply that the frontend application might also use DNS resolution to discover and connect to other services.
  • Labels: The label kompose.service.type is set to NodePort, which is likely used by another tool (e.g., Kubernetes with Kompose) to define the service type. It’s worth mentioning that this configuration is written in the Docker Compose format, but the labels used (kompose.service.type) indicate that it might be intended to be used with another container orchestration system like Kubernetes, possibly with Kompose (a tool to convert Docker Compose files to Kubernetes resources).

To test the application, run the following command:

$ docker-compose -f docker-compose.yaml up

docker-compose up - Docker Compose Configuration

Introducing Kompose

Kompose is an excellent tool that helps streamline the migration process from Docker Compose to Kubernetes. It automatically converts Docker Compose files into Kubernetes manifests (YAML files), reducing manual effort and potential errors during the migration process.

Kompose allows users to take an existing Docker Compose file and translate it into Kubernetes manifests, enabling the deployment of Docker Compose-defined applications to a Kubernetes cluster without the need to manually rewrite the configurations.

The primary use case of Kompose is to simplify the process of migrating applications from Docker Compose to Kubernetes. It is especially helpful for users who are familiar with Docker Compose and want to leverage Kubernetes’ more advanced features and capabilities for container orchestration and management.

Install Kompose for Migration from Docker Compose to Kubernetes

There are multiple ways to install Kompose. The preferred method is downloading the binary from the latest GitHub release.

As we are dealing with Linux, we will install it by running the 3 below commands:

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

Kompose- Install Kompose for Migration from Docker Compose to Kubernetes

Convert Docker Compose to Kubernetes Manifests

As we have installed Kompose,we will execute the Kompose command to convert our Docker Compose file into Kubernetes manifests:

$ kompose -f docker-compose.yaml convert

converted by Kompose - Convert Docker Compose to Kubernetes Manifests

Now let’s verify whether the files are converted or not:

$ ls

Controlplane $ ls - Convert Docker Compose to Kubernetes Manifests

As we can see the compose file into Kubernetes manifest files.

Once you’re confident in your Kubernetes setup, it’s time to clean up your Docker Compose environment. Removing the old setup ensures you avoid any potential conflicts and fully embrace the advantages of Kubernetes.

Deploy Resources to Kubernetes Cluster

Finally, to define the desired configuration for your cluster, you can utilize the kubectl apply command and provide all the Kubernetes resource files generated by Kompose. By doing so, you can create the necessary resources in your cluster, allowing you to achieve the desired state for your applications and services.

$ kubectl apply -f frontend-deployment.yaml,frontend-service.yaml,redis-master-deployment.yaml,redis-master-service.yaml,redis-slave-deployment.yaml,redis-slave-service.yaml

Deploy Resources to Kubernetes Cluster

Now as we have deployed our application we will access it using the service Node port.

$ kubectl get svc

frontend - Deploy Resources to Kubernetes Cluster

Note the Nodeport (in our case 32431) and access the application by browsing along with the public IP of any of the cluster nodes.

http:<Public IP of Node>:<NodePort>

guestbook

Conclusion

Migrating from Docker Compose to Kubernetes might seem like a daunting task at first, but with the assistance of Kompose, the process becomes smoother and more efficient. By following this step-by-step guide, you can successfully transition your application from a single-host environment to a scalable, production-ready Kubernetes cluster. Embrace the power of Kubernetes, and unlock the full potential of container orchestration for your application’s future growth. Happy Kuberneting!

Related Post

Join FREE Masterclass

Discover the Power of Kubernetes, Docker & DevOps – Join Our Free Masterclass. Unlock the secrets of Kubernetes, Docker, and DevOps in our exclusive, no-cost masterclass. Take the first step towards building highly sought-after skills and securing lucrative job opportunities. Click on the below image to Register Our FREE Masterclass Now!

Mastering Kubernetes Docker & DevOps Freeclass

Picture of mike

mike

I started my IT career in 2000 as an Oracle DBA/Apps DBA. The first few years were tough (<$100/month), with very little growth. In 2004, I moved to the UK. After working really hard, I landed a job that paid me £2700 per month. In February 2005, I saw a job that was £450 per day, which was nearly 4 times of my then salary.