How to Use Kompose for Kubernetes Deployments
Traducciones al EspañolEstamos 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.
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.
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.
Have an active Kubernetes cluster configured with
kubectlor 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
kubectlto manage the cluster.
sudo. If you’re not familiar with the
sudocommand, see the Users and Groups guide.
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.
Kompose has a straightforward installation process consisting of only a few steps.
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-amd64in the command above with one of the following strings:
- Apple Silicon:
- Apple Silicon:
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/
Verify that Kompose is installed and functioning:
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.
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.
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/
docker-compose.yamlfile within that directory:
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:
When done, press CTRL+X, followed by Y then Enter to save the file and exit
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.
Make sure you’re still in the directory containing the
docker-compose.yamlfile, then run the Kompose conversion process:
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
Alternatively, you can specify the location of the
docker-compose.yamlfile using the
-foption with the command.
kompose convert -f /example/directory/tree/docker-compose.yaml
Verify the Kubernetes manifests files output by the Kompose conversion process. The
-1option lists the files vertically for easier reading.
db-data-persistentvolumeclaim.yaml db-deployment.yaml db-service.yaml docker-compose.yaml wordpress-deployment.yaml wordpress-service.yaml wp-data-persistentvolumeclaim.yaml
You now have a set of Kubernetes manifests ready to deploy to a cluster. The steps below cover using
kubectl to do that.
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 wordpressoption from the commands in the subsequent steps.
kubectl create namespace wordpress
docker-compose.yamlfile out of the directory. The simplest way to deploy all of the manifests is by having
kubectldeploy everything in the directory, and the
docker-compose.yamlfile might interfere with that.
mv docker-compose.yaml ../
Deploy the resources to the Kubernetes cluster. You could provide a comma-separated list of the resource files output by Kompose with the
-foption. 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
Verify successful deployment. It may take a short amount of time before the deployed pods show the
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
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.
Open the required port in your system’s firewall. The next step uses port
8080to 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
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
Open a web browser and navigate to port
8080on your current machine’s remote address. Typically, this is an IP address, so you would navigate to something like:
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:
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