paint-brush
How to Generate Kubernetes Manifests With a Single Commandby@aahil
2,501 reads
2,501 reads

How to Generate Kubernetes Manifests With a Single Command

by AahilOctober 20th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This article will explore Kompose and its benefits and also help you set up Kompose. Using this article, you will learn how to generate manifests using Kompose and deploy your application to Kubernetes.
featured image - How to Generate Kubernetes Manifests With a Single Command
Aahil HackerNoon profile picture

Applications that require more than one Docker container for deployment are called multi-container applications. These multi-container applications can be deployed using Docker Compose.


However, Docker Compose can only be used for local deployment or in production with the native Docker orchestrator, Docker Swarm.


In most cases, Docker Swarm might not be your preferred choice for container orchestration. In that case, you're left with the option of using Kubernetes or Openshift. However, Kubernetes and Openshift do not traditionally support Docker compose.


Leaving you with such a time-consuming task - writing container orchestration manifests manually. This is where Kompose comes in.


Kompose is a tool that automatically converts your Docker compose application into container orchestration (Kubernetes or Openshift) manifests. Using Kompose, you can create the necessary resources - services, deployments, etc., for your application with a single command. This saves you a lot of time and effort when deploying your application.


This article will explore Kompose and its benefits and also help you set up Kompose. Using this article, you will learn how to generate manifests using Kompose and deploy your application to Kubernetes.

What Is Kompose?

Kompose, derived from the name Kubernetes and compose, is a tool that converts Docker compose files into Kubernetes or Openshift manifests. Using a single Kompose command, you can generate all the necessary resources for your application, as defined by Docker Compose.


Kompose reads the Docker compose file and gathers all the information about the application. This information includes the container's name, image, ports, volumes, etc. All this information is converted into a KomposeObject.


All the KomposeObject information is converted into the target format - Kubernetes or Openshift object. With 99% accuracy, Kompose aligns the container information from the KomposeObject to the specific requirements of the container orchestrators.


Finally, Kompose takes this converted information and displays it on the screen or deploys it to container orchestrators like Kubernetes or OpenShift.

Why Use Kompose?

As you read above, Kompose is a conversion tool that converts Docker compose files into Kubernetes or Openshift manifests. This conversion is done with 99% accuracy. This means you can easily use Kompose to convert your Docker compose files into Kubernetes or Openshift manifests.


  • Multi-platform compatibility: Kompose has a wide range of installation options, ensuring that no matter what environment you're working in, you can quickly set it up to facilitate the conversion of container applications from one format to another.


    The straightforward installation process makes it accessible to a broad audience, from developers to system administrators, making your container migration tasks more manageable.


  • Versatility: Another reason to consider using Kompose is its versatility in converting Docker Compose files into Kubernetes or OpenShift objects. Kompose supports the conversion of Docker Compose files of various versions into Kubernetes and OpenShift objects, making it a valuable tool for container application migration.


    Whether you're working with older or newer Docker Compose file formats, Kompose can adapt and handle the conversion process. This flexibility simplifies the migration of your container applications to Kubernetes or OpenShift, regardless of the Docker Compose version you're using.


  • An Official Kubernetes Project: Another compelling reason to use Kompose is that it's an official Kubernetes project. This official status means that Kompose is well-vetted and maintained by the Kubernetes community, assuring users of its reliability and alignment with Kubernetes best practices.


    Using Kompose, you're leveraging a tool actively supported and developed within the Kubernetes ecosystem. This official recognition adds a level of trust and credibility to the tool, making it a valuable choice for converting Docker Compose configurations to Kubernetes, and it's more likely to stay up-to-date with the latest Kubernetes developments and standards.

How to Install Kompose

Kompose is a standalone tool that can be installed on any machine. It is written in Go and is available as a binary for Linux, Windows, and Mac. Kompose is also available as a Docker image, which can be used to run Kompose as a container.


You can install Kompose on Linux, macOS, Windows, CentOS, Fedora, and even through package managers like Homebrew, Chocolatey, and Docker. To install Kompose, follow the instructions in the official documentation.


This article will be using the binary installation method. To install Kompose on Linux, follow the steps below:


  1. On your terminal, use the curl command to download the Kompose binary for Linux:


    curl -L https://github.com/kubernetes/kompose/releases/download/v1.31.1/kompose-linux-amd64 -o kompose
    


    This command fetches the Kompose binary for Linux and saves it as kompose.


  2. Make the downloaded binary executable by running:


    chmod +x kompose
    


    This command gives execution permission to the kompose binary.


  3. Move the kompose binary to a directory included in your system's PATH, such as /usr/local/bin, which allows you to run it from anywhere in your terminal:


    sudo mv ./kompose /usr/local/bin/kompose
    


    Using sudo ensures you have the necessary permissions to move the binary.


After completing these steps, you should have successfully installed Kompose on your Linux system.


To verify the installation, run the following command


kompose version

1.31.1 (46dcb9181)

How to Use Kompose

After installing Kompose, the next step is to learn how to use it. Kompose is a command-line tool, which means that it is operated via the terminal.

This article will focus on using Kompose to generate Kubernetes manifests. To use Kompose to generate Openshift manifests, follow the instructions in the official documentation.

Prerequisites

To use Kompose to create Kubernetes manifests, you need to have the following installed on your system:


  • Minikube: Minikube is a tool that allows you to run Kubernetes locally. It creates a single-node Kubernetes cluster on your local machine. You can install Minikube by following the instructions in the official documentation.


  • Docker compose: Docker compose is a tool that allows you to define and run multi-container applications. You can install Docker Compose by following the instructions in the official documentation.


  • Kubectl: This tool must be configured to communicate with your cluster. You can install Kubectl by following the instructions in the official documentation.


After installing these tools, you will need a docker compose file to work with. For this guide, you will use the Guestbook application. The Guestbook application is a simple application that allows users to leave messages on a website.


The application consists of three containers - a Redis master container, a Redis slave container, and a PHP container.


The docker-compose file for the Guestbook application is shown below:


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: LoadBalancer


This Docker Compose file defines three services. The redis-master and redis-slave services use different container images, exposing port 6379. The frontend service uses the gcr.io/google-samples/gb-frontend:v4 image and maps port 80 of the host to port 80 of the container.


What's significant is that the frontend service has a kompose.service.type label set to LoadBalancer, indicating that when converting this configuration to Kubernetes or OpenShift, it should be exposed as a LoadBalancer service, allowing external access to the application through port 80.

Generating Kubernetes Manifests With Kompose

To generate Kubernetes manifests with Kompose, follow the steps below:


  1. Create a new file called docker-compse.yml, and copy the Docker Compose file above.


  2. Start Minikube by running the command below:


    minikube start
    


    This command starts Minikube and creates a single-node Kubernetes cluster on your local machine.


  3. Change into the directory where the docker-compose.yml is, and run the following command to convert the Docker Compose file to Kubernetes manifests:


    kompose convert
    


    This single command generates Kubernetes manifests from your Docker Compose configuration. The output should look similar to this:


    INFO Kubernetes file "frontend-tcp-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
    


    The manifests have been created, and you can view them using the ls command.


  4. To apply these generated manifests to your Kubernetes cluster, use kubectl apply as follows:


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


    This command will deploy your application on your Kubernetes cluster. The output should indicate that services and deployments have been created.


  5. To access your application, you can use the minikube service if you're using Minikube for development:


    minikube service frontend-tcp
    


  6. After you've finished testing the application, you can clean up the deployed resources using:


    kubectl delete -f frontend-tcp-service.yaml,redis-master-service.yaml,redis-slave-service.yaml,frontend-deployment.yaml,redis-master-deployment.yaml,redis-slave-deployment.yaml
    


These steps will allow you to convert a Docker Compose configuration to Kubernetes manifests and deploy your application in a Kubernetes cluster using Kompose.

Conclusion

Kompose is a valuable tool for converting Docker Compose configurations to Kubernetes or OpenShift manifests. It's a versatile tool that can handle Docker Compose files in various versions and is actively maintained by the Kubernetes community.


With Kompose, you have a smooth workflow when migrating your container applications from Docker Compose to container orchestration platforms like Kubernetes or OpenShift. In this article, you learned about Kompose and its benefits.


You also learned how to install Kompose and use it to generate Kubernetes manifests from a Docker Compose configuration.