Using Kamal with Ubicloud
Kamal is an open source tool to deploy web apps anywhere. It simplifies the process of deploying and managing your web app in production with Docker.
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:
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:
Commit the generated application, so you can easily tell the changes you will be making from the defaults:
Step 3: Configure Routes
Set the root route to serve the /posts
endpoint by updating config/routes.rb
:
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.
Update .kamal/secrets
to copy the key from the environment:
Configure the config/deploy.yml
file to make it load from .env
, and include your docker registry and the service names in place:
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
toroot
. We useroot
because Kamal installs Docker and connects to it on the VMs. Usingroot
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:
Add the PostgreSQL connection string to the .env
file:
Configure server IP address to deploy to in config/deploy.yml
:
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:
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:
Modify config/database.yml
to use the DATABASE_URL
environment variable, which will already contain the user, password and the database name:
Step 8: Deploy the Application
Commit your changes:
Run Kamal setup, which will setup Docker on the VM you created, and then deploy your application to it:
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: