You are on page 1of 5

Building Rails API using Docker and Kubernetes

Deploying Rails API once meant installing all the dependencies of the apps on Mac like Ruby, databases,
stuff like Redis, and more. This worked mostly, but it also meant working on a platform that can often be
very different from the platform to which you deploy applications. Therefore you may have run into
problems a few times.

You can solve this problem with containers. They allow you to build in an environment that's almost
identical to production. Then because of the simplicity with which I can set up throw away
environments. Containers make packing all the libraries and components that an app requires to run
very straightforward, ensuring that the app still runs in exactly the same consistent way regardless of
where it runs. Besides, they can also use containers to operate external dependencies. In this article let
us talk about the deploying of rails api using docker and kubernetes. ​ruby on rails online course​ helps 
you to learn more effectively.

Docker file
Initial move is to containerize the program. It is very important how you write your Docker file, since a
un optimized Docker file can lead to very large images that often have to be rebuilt from scratch,
slowing things down. Alternatively, because Docker constructs images as layers where each layer is
generated with a particular instruction in the Docker file-a good Docker file may exploit layer caching as
well as significantly reduce the final image size.

This is for Mac users, but on a Windows platform you can follow it for a Linux distribution, or find out
the equivalent. First build your project directory within the terminal. I 'm going to do a project called
"inventory manager," but you can name whatever you want for your project.

Mkdir Inventory manager & & cd inventory manager

Then build some files inside the project's root directory.

​ ocker file docker-compose.yml Gem file Gemfile.lock


Contact​ D

Setup Docker file


The Docker file is all you need to set up the framework for our Docker containers.

FROM ruby:2.3.0
RUN apt-get update -qq && apt-get install -y build-essential libmysqlclient-dev
RUN mkdir /inventory_manager
WORKDIR /inventory_manager
ADD Gemfile /inventory_manager/Gemfile
ADD Gemfile.lock /inventory_manager/Gemfile.lock
RUN bundle install
ADD . /inventory_manager

On the jar, you then run the following commands for apt package manager. During Design you will use
the MySQL client library. You will then add the local Gemfile and Gemfile.lock to the file system of our
container, and then install the dependencies. Finally, you 're adding all project directory content to the
folder.

You need to set up our docker-compose.yml file to manage the communication between our ​docker
Rails production system ​and My SQL container.

Db: image: mysql: latest ports: — "3306:3306" environment: mysql ROOT PASSWORD: build:.

Command: puma ports: — '9292:9292' links: — db volumes: —.:/inventory manage

This configuration creates a My SQL container and also a web container that constructs a Docker file and
runs a Puma web server on port 9292. You then link the Rails app container to the My SQL container to
allow them to communicate with each other. Now let us create a Kubernetes cluster for rails
deployment.

Setting up a Rails Kubernetes cluster deployment with K3s


There are different ways to set up a ​rails Kubernetes cluster​ that you can use for deployment, but my
preference is using Rancher's K3s Kubernetes distribution because it is accredited (just a few deviations
from the upstream Kubernetes that need to be considered) and extremely lightweight! In a single little
binary it is a complete Kubernetes distro. Thanks to the low CPU / RAM use of K3s, you can use a single
node cluster with only 2 cores and 4 GB of ram for growth, which is very cheap. ​ruby on rails training​ for 
more techniques.

Using the official script, installing K3s is super easy:

curl -sfL https:/get.k3s.io INSTALL K3S EXEC ="— no-deploy = traefik "sh-

Note that in the above command you make sure the Traefik Ingress Controller is not automatically
enabled. Because otherwise it is enabled by default. Then you may prefer Nginx as an Ingress Controller.
If you're satisfied with Traefik you can delete the parameter. If K3s are up and running, you must copy
the kubeconfig file to your dev machine so that you can control the cluster using kubectl:

ssh < server name or IP > "sudo cat /

etc /

rancher /

k3s /
k3s.yaml" > ~

/.kube / config-<cluster name >

sed -i -e "s

/ localhost

/<server IP>

/g" ~/.kube / config-<cluster name > sed -i -e "s / default/<cluster name>/g" ~/.

kube / config-<cluster name >

Done setting the KUBECONFIG name. To check, run:

kubectl get nodes

You'll need to manage persistent volumes for things like MySQL/Postgres/Redis/etc, so you'll need to
install some storage management software, or at least a driver for block storage through your cloud
provider, if accessible. I made a comparison of several Kubernetes storage solutions so see this post and
this one for more details that will help you pick. I am currently using a CSI driver for the volumes of
Hetzner Cloud as well as Linstor if your provider does not provide block storage.

Build the image from the Docker


Now it's time to create the image from Docker.

Docker-compose create

You'll need to run this command to rebuild your Docker image anytime you make a Gemfile or Docker
file shift. You will see that an image is generated by typing.

docker image

Let us now deploy Kubernetes on Ruby gems platform.

Kubernetes on Rails API gem


You should use Ruby on rails API GEMS for following reasons

● Clean API for dynamic Kubernetes API


● Groups / Resources Fast API requests using HTTP link
● Quick API discovery and resource listing using pipeline
● HTTP requests Typed errors with useful debugging details
Installation
Add this line to Gem file

gem 'k8s-client'

Then run the following code.

$ bundle

You may also install it through the following code.

$ gem install k8s-client.

Individual resources are returned as ​K8s::Resource instances​ (resource.metadata.name) which provide


access to attributes. The resource instances are returned by methods like
client.api('v1').resource('nodes').get('foo), 'and passed as arguments to
client.api('v1').resource('nodes').create resource(res).

Resources may also be loaded from the disk using K8s::Resource.from files(path), and transferred to the
top-level methods such as client.create resource(res), which search for the appropriate API or
Application client from the Rails API Version and type resource.

Now that you have deployed Kubernetes on Rails API gem. Let us create a ruby application.

Build the Rails API application structure


You should construct the application structure by running the Rails API commands in the Docker image.
In ourdocker-compose.yml you may call the internet as the container against which to execute our
commands.

Docker-compose new-api platform tracks.

Setting up the database

Now we need to configure ourdatabase.yml file so that we can start a migration process.

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: inventory_manager_dev
pool: 5
username: root
password: mypassword
host: db
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: inventory_manager_test
pool: 5
username: root
password: mypassword
host: db

The host is 'db' which is the same name that we set in our db. The value of this host most suits what you
have specified in the docker-compose.yml file to be your database container.

Let's check to see if our web applications are running.Now that the system framework has been
installed, let's run the web server to see if the Rails status page can be updated.

Make sure your docker address is identified by running the docker-machine api. By default
docker-machine ip. Then you may find ruby rails API deployed.

Conclusion:
I hope you reach a conclusion about building Rails API on docker and Kubernetes. You can learn more
through ​Kubernetes online training​.

You might also like