The Rails framework is a popular choice with developers who want to create web applications with ease. It is based on the Ruby language and it allows you to write less code with its predefined layout and additional libraries for simplifying the programming of web applications.
If you have chosen Docker as the platform for building your Rails application image, what could be the next step? Running your application in a production environment. Kubernetes is the best way for running application deployments across clusters of hosts. This solution allows you to automate the deployment, the scaling and management of application containers.
To more easily deploy and manage the application containers in a Kubernetes cluster, you can use Helm charts. In the same way that a Dockerfile contains instructions on how to build images in running containers, the charts define how the application will be deployed in Kubernetes. They specify the objects definitions, services, dependencies, number of pods, and so on.
This guide walks you through the process of running an example Rails application on a Kubernetes cluster. It uses a simple “Hello World!” Rails application based on the latest Bitnami Rails image. The next step is to create a Dockerfile and use it as a starting point for creating a custom Helm chart to automate the application deployment in a Kubernetes cluster. Once the application is deployed and working, this guide also explores how to modify the source code for publishing a new application release and how to perform rolling updates in Kubernetes using the Helm CLI.
This guide will show you how to deploy an example Rails application in a Kubernetes cluster running on Minikube. The example application is a typical “Hello World!” application.
This guide makes the following assumptions:
- You have basic knowledge of how to build Docker images.
- You have basic knowledge of Helm charts and how to create them.
- You have a Docker environment running.
- You have Minikube installed in your local computer.
- You have a Kubernetes cluster running.
- You have the kubectl command line (kubectl CLI) installed.
- You have Helm v3.x installed.
To create your own application in Rails and deploy it in a Kubernetes cluster using Helm you will typically follow these steps:
- Step 1: Create the Helm Chart
- Step 2: Deploy the example application in Kubernetes
Step 1: Create The Helm Chart
It is very easy to get started with Helm charts by using the helm create command. It creates a scaffold with sample files that you can modify to build your custom chart. The following instructions will take you through the process of creating a new chart and customizing it to include your Rails sample application.
- In the same directory you are working in, execute the helm create command to create a new chart named
$ helm create my-rails-app
- Change to
$ cd my-rails-app
- Helm will have created a directory with the file structure shown below:
my-rails-app |-- Chart.yaml |-- charts |-- templates | |-- NOTES.txt | |-- _helpers.tpl | |-- deployment.yaml | |-- ingress.yaml | `-- service.yaml `-- values.yaml
- Edit the
values.yamlfile and replace the existing values with the following:
- Edit the
templates/deployment.yamlfile to add the database environment variables as shown below:
- Edit the
templates/_helpers.tplfile and append these lines at the end of the file:
- Your file should look like this:
- In the
my-rails-app/parent folder, create a new file named
requirements.yamlthat includes the following content:
- Install missing dependencies with
helm dep. The Helm chart used in this guide includes MariaDB dependencies specified in the recently-created
requirements.yamlfile. So you need to download and install them before deploying the application. Execute the commands below to finish creating the chart:
$ helm dep list $ helm dep update .
- Test that the syntax of the files is correct by running the helm lint command:
$ helm lint .
Step 2: Deploy The Example Application In Kubernetes
At this point, you have created your custom Helm chart. It’s now time to deploy the example Rails application within a Kubernetes cluster.
To deploy the example application using the current Helm chart, follow these steps:
- First, make sure that you are able to connect to your Kubernetes cluster by executing the command below:
$ kubectl cluster-info
- Deploy the Helm chart with the
helm installcommand. This will create two pods within the cluster, one for the Rails service and the other for the MariaDB service.
DB-PASSWORDis a placeholder, replace it with the password of the root user used during the database creation.
$ helm install --set mariadb.mariadbRootPassword=DB-PASSWORD my-rails-app .
Once the chart has been installed, you will see a lot of useful information about the deployment.