Even with Kamal, you still need certain infrastructure primitives, including virtual machines and managed databases. Ubicloud offers these primitives on the cloud. We believe the combination of Kamal and Ubicloud provide a compelling open source web app deployment platform.

In this guide, we’ll show steps for configuring an example Rails 8 application in a way that allows you to use Kamal 2 to deploy it to Ubicloud.

Step 1: Install Kamal

First, we need to install Rails and Kamal. Assuming you have Ruby installation in your local machine, run:

gem install -N rails kamal

Step 2: Create a Simple Rails App

Next, create a basic Rails app using PostgreSQL as the database and Tailwind CSS for the frontend, and generate a scaffold:

rails new blog -d postgresql -c tailwind
cd blog
rails generate scaffold post title:string content:text

Commit the generated application, so you can easily tell the changes you will be making from the defaults:

git add .
git commit -m "Initial commit: rails new and rails generate scaffold post"

Step 3: Configure Routes

Set the root route to serve the /posts endpoint by updating config/routes.rb:

root "posts#index"

Step 4: Set Up Docker Registry

Now, we need to set up a dockerhub repository to store and fetch our images. For that, you need to create an account at https://hub.docker.com. Once you have the account, please create a new repository named “blog” in the docker hub page and generate a new key at https://app.docker.com/settings/personal-access-tokens and paste it to the .env file as shown below.

KAMAL_REGISTRY_PASSWORD=DOCKER_REGISTRY_KEY

Update .kamal/secrets to copy the key from the environment:

DATABASE_URL=$DATABASE_URL

Configure the config/deploy.yml file to make it load from .env, and include your docker registry and the service names in place:

<% require "dotenv"; Dotenv.load(".env") %>

service: blog

image: YOUR_DOCKER_REGISTRY_NAME/blog

registry:
  username: YOUR_DOCKER_REGISTRY_NAME

Step 5: Set Up Ubicloud Resources

Create the following resources:

  • Virtual Machines (VMs): Create a VM from console.ubicloud.com. When creating the VM, you need to pay attention to three things:

    • For the location, pick Germany. Later, we’re going to provision a managed Postgres in the same region.
    • Make sure to include an IPv4 address, as Kamal 2 will have deployment issues in IPv6 only environments.
    • Change the user from ubi to root. We use root because Kamal installs Docker and connects to it on the VMs. Using root avoids the need to manage users and groups.
  • Managed PostgreSQL Database: Set up a managed PostgreSQL database.

Step 6: Gather Resource Information

After setting up the resources, collect the following information from the Ubicloud Console:

  • PostgreSQL Connection String: Shown by clicking on the database name on the PostgreSQL page
  • Public IP of the VM: Shown in the IP Address column on the Compute page

Step 7: Configure Kamal and Environment Variables

Next, copy the master key from config/master.key and paste it into the .env file:

RAILS_MASTER_KEY=YOUR_MASTER_KEY

Add the PostgreSQL connection string to the .env file:

DATABASE_URL="postgres://postgres:password@hostname/?channel_binding=require"

Configure server IP address to deploy to in config/deploy.yml:

servers:
 web:
   hosts:
     - IP

Rails 8’s Kamal 2 configuration defaults to an HTTPS-only application, using certificates from Let’s Encrypt, so you need to edit the config/deploy.yml file to set the hostname for the application:

proxy:
  ssl: true
  host: your-host.your-domain.com

You will need to create or update your DNS records so that the hostname you are using resolves to the IP address of the VM you created. How you do this depends on your DNS provider.

Update config/deploy.yml to pass the environment variables:

env:  
 secret:
   - RAILS_MASTER_KEY
   - DATABASE_URL

Modify config/database.yml to use the DATABASE_URL environment variable, which will already contain the user, password and the database name:

production:
 <<: *default
 url: <%= ENV["DATABASE_URL"] %>

Step 8: Deploy the Application

Commit your changes:

git add .
git commit -m "Changes to allow deploying to Ubicloud using Kamal"

Run Kamal setup, which will setup Docker on the VM you created, and then deploy your application to it:

kamal setup

Step 9: Enjoy Your HTTPS-Enabled Rails App

Visit the DNS name you configured and enjoy your newly deployed, secure Rails app!

By following these steps, you can seamlessly integrate Kamal with Ubicloud services to deploy your web applications efficiently and securely. If you have any questions or need further assistance, feel free to reach out to our support team.

Step 10: Deploying Changes After Initial Setup

After making and committing changes in your application, you can deploy them using:

kamal deploy