Turn Bitnami Application Containers into a Kubernetes Deployment using Kompose

Introduction

Containers are a popular solution for applications development. Containers are based on micro services architecture: complex applications are the result of the sum of small pieces (components) developed in a separate way. Each of these pieces can live inside a container and scale independently when the needs of the application arise. That way, you can save a lot of resources and scale your application deployments with ease. Docker has become an open source choice for creating, deploying and running applications by using container technology. How containers must be built is defined in a Dockerfile.

What happens when your containerized deployment need to be put into production? You need a tool that allows the deployment of containers across clusters of hosts like Kubernetes.

Since Docker needs the Dockerfiles to know how to build the containers for application deployment, Kubernetes needs manifest files to know how to deploy and manage the application containers within the cluster.

Kompose is a tool that allows you to automatically convert all your Dockerfiles into Kubernetes manifest files. Therefore, it is not necessary to write these files from scratch. Just convert your existing Dockerfiles into Kubernetes resources with a pair of simple commands. Kompose enables developers who are familiar with building Docker images to move quickly and easily to Kubernetes-based deployments.

This guide walks you through the process of running an example WordPress site on a Kubernetes cluster. It uses the latest Bitnami WordPress Docker Compose file and translates it into Kubernetes manifests using Kompose. It also guides you through the steps to follow in order to deploy the application on Kubernetes and access it later, as well as the basics to check the deployment using the kubectl CLI. In addition, you will find an appendix explaining the usage of the kubectl run for those Docker-familiarize users who wish to run their Docker application images directly in a Kubernetes deployment.

Prerequisites

This guide will show you how to turn a Docker deployment into a Kubernetes one running on Minikube. The example is based on a Bitnami WordPress Dockerfile available on GitHub.

This guide makes the following assumptions:

NOTE: This tutorial offers you an example using a ready-made Bitnami Docker image and Docker Compose file. You can also follow these steps to turn your own application images into a Kubernetes deployment. Check out how to develop applications using Bitnami development containers.

Once you have verified that your Docker environment and your Kubernetes cluster are running, you will need to perform the following steps:

  • Step 1: Deploy the application using the Docker Compose CLI
  • Step 2: Install Kompose
  • Step 3: Convert the Docker Compose files into Kubernetes manifests
  • Step 4: Deploy the application on Kubernetes using the Kompose CLI

Step 1: Deploy the application using the Docker Compose CLI

The recommended way to deploy an application using containers is writing a Docker Compose file that contains the instructions on how to build a Docker. As a result, it will be very easy to deploy your application using the docker-compose up command.

A Docker Compose file must specify at least the following elements:

  • Services
  • Networks
  • Volumes

Check the Docker official documentation to find guidelines and recommendations for writing Dockerfiles.

NOTE: All Bitnami Docker images include a docker-compose.yml template for running the application containers just by executing a single command: docker-compose up.

To begin the process, make sure that the Docker environment is running and that you have access to the current Docker Compose file.

  • Check that the Docker daemon is running by executing the following command:

    $ docker info
    

    You should see an output similar to the following:

    Docker daemon running

    As you can see in the image, the number of containers is equal to 0. This is because you haven't run the containers yet.

    TIP: If you see the following output Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?, check how to run and configure the Docker daemon by following the Docker official documentation.
  • Clone the application repository to obtain the example Docker Compose file as shown below:

    $ curl -LO https://raw.githubusercontent.com/bitnami/bitnami-docker-wordpress/master/docker-compose.yml
    
  • Edit the docker-compose.yml file and add the following lines to set the WordPress service type as NodePort:

     labels:
       - kompose.service.type=NodePort
    

    By doing this, the application pod will be exposed as a Kubernetes service and you will be able to access it from outside the cluster once it's created. Your file should look like this:

    Add a service type to the Dockerfile

This Docker Compose file describes two different containers: a MariaDB database that exposes the port 3306 (the port that MariaDB uses by default), and a PHP Web Application (WordPress) which is served through an Apache Server that exposes itself on ports 80/443.

NOTE: You can customize the application instance by specifying environment variables on the first run. Find here the environment values for the proposed WordPress example.
  • Run the application and database containers by executing the docker-compose up command:

    $ docker-compose up
    

    Run application containers

Now, your application is running, the containers have been created and the volumes are mounted.

  • Check the recently created containers by running the following command:

    $ docker ps -a
    

    Check application containers

  • Access your application at 127.0.0.1 or localhost. You should now see your WordPress blog's home page with a sample post, as shown below.

    Access application

You can stop the deployment by deleting its containers and network using the docker-compose down command:

  $ docker-compose down

You should see an output like this:

Stop running containers

Step 2: Install Kompose

Follow the instructions below to complete the Kompose tool installation:

  • Navigate to the Kompose release page.
  • Select the distribution you wish to download depending on your Operating System.
  • Open a new console window on the local system.
  • To obtain the latest Kompose release, execute the following command depending on your OS. Remember to replace the X.Y.Z and OS_DISTRIBUTION placeholders with the latest version and software distribution of Kompose, respectively.

    $ curl -L https://github.com/kubernetes-incubator/kompose/releases/download/v.X.Y/kompose-OS_DISTRIBUTION -o kompose && chmod +x kompose && sudo mv kompose /usr/local/bin/
    

Step 3: Convert the Docker Compose files into Kubernetes manifests

Kompose is a tool created to allow you to convert the Docker Compose files into Kubernetes deployments and services. It supports either v1 or v2 of Docker Compose file.

Kompose has three main commands: up, convert and down.

To convert the Docker Compose files into Kubernetes manifest files you need to run the kompose convert command as is indicated in the following instructions:

  • Navigate to the directory in which you have the Docker Compose files (in this example, we have located the docker-compose.yml file in a directory called docker-wordpress).

    $ cd docker-wordpress
    $ ls
    docker-compose.yml
    
  • Execute the kompose convert command followed by the file you want to convert:

    $ kompose --file docker-compose.yml convert
    

    You should see an output like this:

    Convert Dockerfles into Kubernetes manifests

Step 4: Deploy the application on Kubernetes using the Kompose CLI

NOTE: At this point, you need to have a Kubernetes cluster running. In this example, a single node cluster has been created using Minikube (v0.18 or highger). For detailed instructions, refer to our starter tutorial.

Once you have converted the Docker Compose file into Kubernetes manifests, you are able to deploy the application into a Kubernetes cluster just using the kompose up command. Follow the instructions below:

  • Deploy the application using the kompose up command in a similar way that you have run the containers by executing the docker-compose up command in the step 1:

    $ kompose up
    

    You should get an output showing that every service, deployment and persistence data volumes have been successfully created:

    Deploy the application in Kubernetes using Kompose

    Learn more about Kompose commands.

Validate the cluster

Now, you are able to use the kubectl command line to validate the cluster and to get some useful information about its components. Test your deployment by executing the following commands:

  • Validate the creation of the deployment by listing it resources. Run the kubectl get deployment to see the number of desired, current, up-to-date and available replicas of the deployment:

    $ kubectl get deployment
    
  • Check the number and the status of the existing pods:

    $ kubectl get pods
    

    Check Kubernetes deployment and pods

  • List all services running in the cluster:

    $ kubectl get svc
    

    Check services

  • Obtain detailed information of specific services:

     $ kubectl describe svc mariadb
     $ kubectl describe svc wordpress
    

    Check database and application services

Access the application

The application is running within the Kubernetes cluster and is ready to use. To access the application choose one of these two options:

  • Check the application URL by listing the services running in the cluster. Then, browse to the specified URL:

     $ minikube service list
    

    List cluster services

    Or,

  • Access directly to the application by checking the application service with the minikube service SERVICE command. Remember to replace the SERVICE placeholder with the name of the service you want to check:

     $ minikube service wordpress
    

    Check application service

    In both cases, you should see the application running. Here's what it should look like:

Access application via service URL

You can turn the application off by deleting its deployments and services using the kompose down command:

  $ kompose -f docker-compose.yml down

You should see an output similar like this:

Stop the application deploymet using Kompose

Appendix: Working with kubectl for Docker users

If you have worked with Docker containers, it is more likely that you are familiarized with the Docker CLI and its commands. This section explains how to use the docker run command to create an application deployment by running isolated containers that share the same network. This scenario is very similar to a Kubernetes one where two or more pods are running in a cluster deployment.

Now that you used Kompose to discover the Kubernetes API, you could start working directly with kubectl. Let's see how the kubectl CLI offers you equivalent functions as the Docker CLI for similar purposes:

Using Docker CLI

In the example below we are going to launch a WordPress deployment by running two containers, one for the MariaDB database and other for the application itself. The first step consists in the creation of a common network in which both the database and the application container will run. Then, we are going to mount the volumes for data persistence and finally, the containers specifying the environment variables needed for running the whole deployment.

This section will show you how to create an application containerized deployment using the docker run command. After performing these steps you will be able to access the application in your default browser:

  • Create a network for connecting the application and the database container:

    $ docker network create wordpress-tier
    

    Create a network in a container deployment

  • Create a volume for database data persistence:

    $ docker volume create --name mariadbdata
    
  • Run the database container by executing the docker run command and including the environment variables:

    $ docker run -d --name mariadb -e ALLOW_EMPTY_PASSWORD=yes \
      --net wordpress-tier \
      --volume mariadbdata:/bitnami/mariadb \
      bitnami/mariadb:latest
    
  • Create the application volumes for application data persistence. You need to mount one volume per the application and each components:

    $ docker volume create --name wordpressdata
    $ docker volume create --name apachedata
    $ docker volume create --name phpdata
    
  • Run the application container by executing the docker run command including the environment variables. Add the the network and persistence volume names and paths created in the step above:

    $ docker run -d --name wordpress -p 80:80 -p 443:443 \
      --net wordpress-tier \
      --volume wordpressdata:/bitnami/wordpress \
      --volume apachedata:/bitnami/apache \
      --volume phpdata:/bitnami/php \
      bitnami/wordpress:latest
    

    Run application containers

Now, your application deployment is running, the containers has been created and the persistence volumes are mounted.

  • Check the recently created containers by running the following command:

    $ docker ps -a
    

    Check application containers

  • You can access the application at http://SERVER_IP/. You should now see your WordPress blog's as shown below.

    Access the application

Using Kubectl CLI

In the example below we are going to launch a WordPress deployment within a Kubernetes cluster by running two pods, (one for the MariaDB database and other for the application and their services).

The first step consists in the creation of the database pod. Since pods are only accessible by its internal IP address within the Kubernetes cluster, the pods need to be exposed as a Kubernetes service to make them accessible from outside the cluster. Finally, the same procedure will be repeated to create and to make accessible the pods from outside the cluster. After performing these steps you will be able to access the application in your default browser.

Learn more about the Kubernetes App Architecture.

  • Create the database deployment by running the kubectl run command followed by the environment variables:

    $ kubectl run mariadb --env MARIADB_ROOT_PASSWORD=bitnami --env MARIADB_DATABASE=bitnami_wordpress --env MARIADB_USER=bn_wordpress --env MARIADB_PASSWORD=bitnamiwordpress --image=bitnami/mariadb
    
  • Expose the recently created pod as a service by running the kubectl expose command:

    $ kubectl expose deployment mariadb --port 3306 --type ClusterIP
    
  • Create the application deployment by running the kubectl run command followed by the environment variables:

    $ kubectl run wordpress --env WORDPRESS_DATABASE_PASSWORD=bitnamiwordpress --env MARIADB_HOST=mariadb --env MARIADB_PORT=3306 --image=bitnami/wordpress
    
  • Expose the recently created pod as a service by running the kubectl expose command:

    $ kubectl expose deployment wordpress --port 80 --type LoadBalancer
    

    Run the application in Kubernetes

Validate the cluster

Now, you are able to use the kubectl command line to validate the cluster and to get some useful information about its components. Test your deployment by executing the following commands:

  • Validate the creation of the deployment by listing it resources. Run the kubectl get deployment to see the number of desired, current, up-to-date and available replicas of the deployment:

    $ kubectl get deployment
    
  • Check the number and the status of the existing pods:

    $ kubectl get pods
    

    Check Kubernetes deployment and pods

  • List all services running in the cluster:

    $ kubectl get svc
    

    Check services

  • Obtain detailed information of specific services:

     $ kubectl describe svc mariadb
     $ kubectl describe svc wordpress
    

    Check database and application services

Access the application

The application is running within the Kubernetes cluster and is ready to use. To access the application choose one of these two options:

  • Check the application URL by listing the services running in the cluster. Then, browse to the specified URL:

     $ minikube service list
    

    List cluster services

    Or,

  • Access directly to the application by checking the application service with the minikube service SERVICE command. Remember to replace the SERVICE placeholder with the name of the service you want to check:

     $ minikube service wordpress
    

    Check application service

    In both cases, you should see the application running. Here's what it should look like:

Access application via service URL

You can turn the application off by deleting its deployments and services using the kompose down command:

  $ kompose -f docker-compose.yml down

You should see an output similar like this:

Stop the application deployment using Kompose

To learn more about the topics discussed in this guide, use the links below:

kubernetes

Bitnami Documentation