Ruby on Rails on CloudFlow
Learn how to run a Ruby on Rails app at the edge for low latency and high availability. You can use our repo as a template, or perform the steps yourself using the Kubernetes dashboard or kubectl commands.
Step by Step
Following are step-by-step instructions to deploy a Ruby on Rails application to the edge on CloudFlow. We'll Dockerize it, and deploy it on CloudFlow.
Prerequisites
- You need Docker installed so that you can build a docker image, and Ruby, Rails, and SQLite installed for testing locally.
Create the Ruby on Rails App
Create a new directory for your app.
mkdir my-rubyonrails-app
cd my-rubyonrails-app
Initialize the Ruby on Rails application with the following commands:
rails new hello_world
cd hello_world
Run the Ruby on Rails application locally with the following command:
# On Windows
ruby bin/rails server
# On Linux/Mac
bin/rails server
Now navigate to http://localhost:3000
in your browser to see the Ruby on Rails app running.
note
After deploying on CloudFlow and using your own domain name(s) you will need to edit the config.hosts
setting in my_rubyonrails_app/hello_world/config/environments/development.rb
to be your domain name(s). For example, if your domain name is example.com
(or including subdomains such as www.example.com
), you would change it to the following line:
# Allow hosts
config.hosts << /[a-z0-9-.]+\.example\.com/
Dockerize It
Let's build the container image that we'll deploy to CloudFlow. First make a Dockerfile
in your hello_world
directory with the following content.
# Use the latest Ruby image from Docker Hub (https://hub.docker.com/_/ruby)
FROM ruby:latest
WORKDIR /hello_world
COPY . /hello_world
# Run bundle install to install the Ruby dependencies.
RUN bundle install
# Install Yarn.
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn
# Run yarn install to install JavaScript dependencies.
RUN yarn install --check-files
# Export port 3000
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Build and tag it.
docker build . -t ghcr.io/YOUR_GITHUB_USERNAME/my-rubyonrails-app:prod
Launch it locally to test it.
docker run -p 3000:3000 ghcr.io/YOUR_GITHUB_USERNAME/my-rubyonrails-app:prod
curl http://localhost:3000
Push It
Push it to GitHub Packages. This makes it available to CloudFlow.
docker push ghcr.io/YOUR_GITHUB_USERNAME/my-rubyonrails-app:prod
Be sure to make it public. To see your packages and make this change, visit https://github.com/YOUR_GITHUB_USERNAME?tab=packages
Deploy It
Next, use the Create Project command in the CloudFlow Console in order to deploy your new container. Use the image name ghcr.io/YOUR_GITHUB_USERNAME/my-rubyonrails-app:prod
with port 3000.
See the pods running on CloudFlow's network with either the Kubernetes dashboard or kubectl get pods -o wide
. The -o wide
switch shows where your app is running according to the default AEE location optimization strategy. Your app will be optimally deployed according to traffic. In lieu of significant traffic, your deployment will be made to default locations.
Try kubectl logs POD
to see the log message reporting that the server is listening on port 3000:
=> Booting Puma
=> Rails 7.0.4 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.1.3-p185) ("Birdie's Version")
* Min threads: 5
* Max threads: 5
* Environment: development
* PID: 1
* Listening on http://0.0.0.0:3000
Finally, follow the instructions that configure DNS and TLS.
See What You've Built
See the sample app you've built by visiting the https://YOUR.DOMAIN.COM
, substituting YOUR.DOMAIN.COM
according to your DNS and HTTPS configuration.