How to Use Kompose for Kubernetes Deployments

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Kompose is a tool that makes it easier to convert Docker Compose files into Kubernetes resource deployments. You do the usual development with Docker Compose, then simply run Kompose to convert your work to a set of Kubernetes manifests. In this tutorial, learn more about Kompose, how to install it, and how to use it to build resources for your Kubernetes cluster.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.

  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

  3. Have an active Kubernetes cluster configured with kubectl or a similar tool. You can follow our Linode Kubernetes Engine - Getting Started guide to deploy an LKE cluster from the Linode Cloud Manager. The guide also includes steps for installing and configuring kubectl to manage the cluster.

Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, see the Users and Groups guide.

What Is Kompose?

Kompose converts Docker Compose files to Kubernetes resources. It provides an easier and more accessible route for users familiar with Docker Compose to start working with Kubernetes resources.

Kompose works by reading a docker-compose.yaml file. It’s able to parse out each part of the Compose deployment and create a corresponding Kubernetes YAML file. It outputs a set of Kubernetes resource definitions that are deployable to a Kubernetes cluster using kubectl or a similar tool.

How to Install Kompose

Kompose has a straightforward installation process consisting of only a few steps.

  1. Download the Kompose binary. Replace the version (v1.30.0) in the command below with the latest version on the Kompose releases page on GitHub.

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

    If you are using macOS, replace kompose-linux-amd64 in the command above with one of the following strings:

    • Apple Silicon: kompose-darwin-arm64
    • Intel: kompose-darwin-amd64
  2. Move the Kompose binary to a location on your shell path. Although optional, this step makes the Kompose binary significantly more convenient to access.

    sudo chmod +x kompose
    sudo mv kompose /usr/local/bin/
  3. Verify that Kompose is installed and functioning:

    kompose version
    1.30.0 (9d8dcb518)

How to Convert Docker Compose to Kubernetes with Kompose

With Kompose installed, you can start converting Docker Compose files for use with Kubernetes deployments. The rest of this tutorial walks through a full example. It includes a simple Docker Compose file for running WordPress with a MariaDB backend. The example then shows how to convert that Docker Compose file into Kubernetes resource manifests using Kompose. The last step uses kubectl to deploy those resources to a Kubernetes cluster.

Creating a Docker Compose YAML

Demonstrating Kompose requires an example Docker Compose setup. The best demonstration should use a Docker Compose that defines multiple interconnected containers, but one that also avoids being too complicated. WordPress provides a convenient example. It requires deployment of the WordPress image alongside a supported database, thus interconnection without too much configuration.

Below is a Docker Compose file for a basic WordPress instance backed by MariaDB. The model follows Docker’s official example, which you can reference to learn more.

  1. Create and change into a project directory for the Docker Compose file. This directory later houses the Kubernetes manifests as well.

    mkdir ~/wp-manifests/
    cd ~/wp-manifests/
  2. Create a docker-compose.yaml file within that directory:

    nano docker-compose.yaml
  3. This file defines services for WordPress and its MariaDB database, as well as volumes for persisting the data needed by each service. Give it the contents shown here:

    File: docker-compose.yaml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
    version: '3'
    services:
      db:
        # Database version selected to work across CPU architectures
        image: mariadb:10.6.4-focal
        command: '--default-authentication-plugin=mysql_native_password'
        volumes:
          - db_data:/var/lib/mysql
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=wpdbrootpass
          - MYSQL_DATABASE=wordpress
          - MYSQL_USER=wpdbuser
          - MYSQL_PASSWORD=wpdbpass
        expose:
          - 3306
          - 33060
      wordpress:
        image: wordpress:latest
        volumes:
          - wp_data:/var/www/html
        ports:
          - 80:80
        restart: always
        environment:
          # Points to the `db` service defined above
          - WORDPRESS_DB_HOST=db
          # Credentials must match the database credentials set above
          - WORDPRESS_DB_USER=wpdbuser
          - WORDPRESS_DB_PASSWORD=wpdbpass
          - WORDPRESS_DB_NAME=wordpress
    volumes:
      db_data:
      wp_data:
  4. When done, press CTRL+X, followed by Y then Enter to save the file and exit nano.

Converting from Docker Compose to Kubernetes

Kompose can now be run with the convert command within the project directory. Kompose automatically finds and reads the docker-compose.yaml file within the current directory to create the necessary Kubernetes resources.

  1. Make sure you’re still in the directory containing the docker-compose.yaml file, then run the Kompose conversion process:

    kompose convert
    INFO Kubernetes file "db-service.yaml" created
    INFO Kubernetes file "wordpress-service.yaml" created
    INFO Kubernetes file "db-deployment.yaml" created
    INFO Kubernetes file "db-data-persistentvolumeclaim.yaml" created
    INFO Kubernetes file "wordpress-deployment.yaml" created
    INFO Kubernetes file "wp-data-persistentvolumeclaim.yaml" created
    Note

    Alternatively, you can specify the location of the docker-compose.yaml file using the -f option with the command.

    kompose convert -f /example/directory/tree/docker-compose.yaml
  2. Verify the Kubernetes manifests files output by the Kompose conversion process. The -1 option lists the files vertically for easier reading.

    ls -1
    db-data-persistentvolumeclaim.yaml
    db-deployment.yaml
    db-service.yaml
    docker-compose.yaml
    wordpress-deployment.yaml
    wordpress-service.yaml
    wp-data-persistentvolumeclaim.yaml

Deploying the Kubernetes Resource

You now have a set of Kubernetes manifests ready to deploy to a cluster. The steps below cover using kubectl to do that.

  1. Create a namespace for the WordPress deployment. While this is entirely optional, it makes it easier to review and manage the deployment later. If you choose not to complete this step, remove the --namespace wordpress option from the commands in the subsequent steps.

    kubectl create namespace wordpress
  2. Move the docker-compose.yaml file out of the directory. The simplest way to deploy all of the manifests is by having kubectl deploy everything in the directory, and the docker-compose.yaml file might interfere with that.

    mv docker-compose.yaml ../
  3. Deploy the resources to the Kubernetes cluster. You could provide a comma-separated list of the resource files output by Kompose with the -f option. However, since the directory is clear of any other files, use . to deploy all files.

    kubectl apply -f . --namespace wordpress
    persistentvolumeclaim/db-data created
    deployment.apps/db created
    service/db created
    deployment.apps/wordpress created
    service/wordpress created
    persistentvolumeclaim/wp-data created
  4. Verify successful deployment. It may take a short amount of time before the deployed pods show the Running status.

    kubectl get pods --namespace wordpress
    NAME                         READY   STATUS    RESTARTS   AGE
    db-84b85bb594-sbll2          1/1     Running   0          59s
    wordpress-785f8f7d4f-khqmd   1/1     Running   0          59s

Viewing the Deployed Application

The resources have been deployed, meaning your Kubernetes cluster should be running a WordPress instance. Verify this by visiting the new WordPress site using the additional steps below.

  1. Open the required port in your system’s firewall. The next step uses port 8080 to map the WordPress service to.

    Refer to our How to Configure a Firewall with UFW guide, and use the following commands to open the HTTP port.

    sudo ufw allow 8080/tcp
    sudo ufw reload

    Refer to our Configure a Firewall with Firewalld guide, and use the following commands to open the HTTP port:

    sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
    sudo firewall-cmd --reload
  2. Initiate port forwarding from the WordPress service. This allows you to connect to the WordPress site using your current machine’s single URL.

    kubectl port-forward --address 0.0.0.0 svc/wordpress 8080:80 --namespace wordpress
  3. Open a web browser and navigate to port 8080 on your current machine’s remote address. Typically, this is an IP address, so you would navigate to something like: http://192.0.2.0:8080.

    Setup page for a Kubernetes-hosted WordPress site

Conclusion

Kompose is a powerful tool. Whether you are a Docker Compose aficionado beginning with Kubernetes, or simply looking to use Docker Compose to streamline Kubernetes development, Kompose can help.

The links below are helpful resources for learning more about working with Kompose. Additionally, you may be interested in moving forward with more on Kubernetes and Docker Compose:

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.