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 DNS management, load balancers, 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 to deploy an example web app through Kamal using Ubicloud services.
We will demonstrate how to deploy an HTTPS-enabled Rails app using Kamal and Ubicloud from scratch.
Note: This content is heavily inspired by DHH’s demo at kamal-deploy.org. We will use similar steps when deploying to Ubicloud.
First things first, we need to install Rails and Kamal. Assuming you have Ruby installation in your local, run;
First, create a basic Rails app using PostgreSQL as the database and Tailwind CSS for the frontend:
After generating the app, open the project in your favourite text editor.
Now, copy the master key from ./config/master.key and paste it into the .env file:
Set the root route to serve the /posts endpoint by updating ./config/routes.rb:
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.
Configure the deploy.yml file to put your docker registry and the service names in place;
Create the following resources:
Virtual Machines (VMs): Create two VMs 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.
Use the same private subnet for VMs, load balancer, and managed Postgres. If you don’t already have a private subnet, create a new one the very first time.
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.
Load Balancer: Create a load balancer to distribute traffic and utilize SSL certificates. Pick the private subnet you chose for your VMs, use /up endpoint, ports 443 and HTTPS as the health check protocol. If you need more info, you can also visit our Load Balancer Guide.
After setting up the resources, collect the following information from the Ubicloud Console:
Configure servers and the load balancer in deploy.yml:
Add the PostgreSQL connection string to the .env file:
Update deploy.yml to pass the environment variables:
Modify database.yml to use the environment variable. The DATABASE_URL environment variable will already contain the user, password and the database name:
Configure Traefik to use the SSL certificates provided by Ubicloud. Update deploy.yml:
Use Kamal hooks to pull SSL certificates into the VM. Rename the pre-traefik-reboot.sample file at .kamal/hooks/ directory to pre-traefik-reboot and insert the following script:
Commit your changes:
Run Kamal setup:
Reboot Traefik to execute the pre-traefik-reboot hook:
Visit the load balancer DNS name 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.