Using Kamal 2.0 in Production

Deploy Rails (and other) applications with confidence

Based on the book by Sam Ruby

Duration: 30 minutes

Using Kamal 2.0 in Production

QR Code

About the Author

Sam Ruby

  • Primary author of "Agile Web Development with Rails" since 2008
  • Contributor to Ruby on Rails framework
  • Works at Fly.io (PaaS company)
  • Believes in both PaaS AND Kamal - they complement each other
  • Running production apps on both Fly.io and Kamal/Hetzner

Just imagine...

What if you could give every user of your software the experience of having a dedicated server machine assigned to them?

The Core Message

"Production is more than copying files and starting a process"

Production requires:

  • Database backups
  • Searchable logs
  • Application monitoring
  • Security measures
  • And much more...

What is Kamal?

Zero-downtime deployment tool for containerized applications

Features:

  • Rolling restarts
  • Asset bridging
  • Remote builds
  • Accessory service management

Key Points:

  • Built on Docker
  • Originally for Rails
  • Works with any containerized app

The Knowledge Gap

What Rails developers know:

  • Web framework concepts
  • Business domain expertise
  • Application development

What production requires:

  • Docker & containerization
  • Linux system administration
  • Networking & security
  • Infrastructure management

This book bridges that gap!

Book Structure Overview

Three Main Sections:

  1. First Deploy - Getting started
  2. Going to Production - Production readiness
  3. Conclusion - Recap and alternatives

Key Philosophy: Deploy locally first, then to production

Prerequisites & Preparation

Before You Deploy, Gather:

  • ✅ SSH Key (not passwords!)
  • ✅ Server (VPS or dedicated)
  • ✅ Domain name
  • ✅ Container registry account
  • ✅ Password manager
  • ✅ Optional: Separate builder

Deploy Locally First!

The Recipe - Configuration

Key Configuration Files:

1. config/deploy.yml

  • Service name
  • Image registry details
  • Server IPs
  • SSL/proxy settings
  • Volumes for persistence

2. .kamal/secrets

  • Registry passwords
  • Rails master key
  • API credentials

First Deploy Commands

# Initial setup (installs Docker, builds, deploys)
bin/kamal setup

# Subsequent deploys
bin/kamal deploy

What happens during setup:

  • Installs Docker on host
  • Builds & pushes image
  • Starts container
  • Runs migrations
  • Configures SSL
  • Sets up proxy

Security Best Practices

Three Attack Vectors to Protect:

  1. You - Use SSH keys & password managers
  2. Your Application - Keep dependencies updated
  3. Your Server - Firewall & system updates

Essential Ports:

  • 22 (SSH)
  • 80 (HTTP)
  • 443 (HTTPS)
  • Block everything else!

Litestream: Continuous SQLite Backups

dbs:
  - path: /data/production.sqlite3
    replicas:
      - type: s3
        endpoint: ${ENDPOINT_URL}
        bucket: ${BUCKET_NAME}
        path: depot
  • Real-time S3 replication
  • Point-in-time recovery
  • Minimal performance impact

Logging & Monitoring

Vector for Log Aggregation

Benefits:

  • Collects logs from multiple sources
  • Ships to various destinations
  • Highly performant
  • Easy configuration

Also covered:

  • Sentry for error tracking
  • Prometheus for metrics
  • Health checks

Performance with Thruster

HTTP/2 and Asset Caching

# Simple addition to config.ru
require "thruster"
run Thruster.run(Rails.application)

Benefits:

  • X-Sendfile support
  • HTTP/2 out of the box
  • Automatic asset caching
  • Minimal configuration

Staging Environments

Test before production!

Options covered:

  1. Separate staging server
  2. Multiple apps on one server (using Kamal proxy)
  3. Environment-specific configurations

Key insight: Kamal's proxy allows multiple apps per server

Continuous Deployment

GitHub Actions Integration

- name: Deploy with Kamal
  run: |
    bin/kamal deploy
  env:
    KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
  • Automated deployments on push
  • Secure secret management
  • Zero-downtime updates

The Scaling Journey

  1. Single user, single machine
  2. Multiple users, separate databases
  3. Multiple machines
  4. Macro services
  5. Backups & logging
  6. Admin automation

"You can't make problems disappear, but you can make them smaller and more manageable"

Exponential Growth

Year Events Cities Countries Continents
2022 8 6 1 1
2023 30 20 3 2
2024 90 45 5 3
2025 333 74 8 4

Architecture scales linearly - no complex orchestration needed!

Production Architecture in Action

Running on Multiple Platforms

  • 12 hosts globally
  • 333 events across 74 cities
  • 8 countries, 4 continents
  • Running on both Fly.io AND Kamal/Hetzner
  • Companion apps in JavaScript
  • Zero shared data between events

Proof that the approach works!

Key Takeaways

  1. Deploy locally first - Catch issues early
  2. Gather ingredients before cooking - Preparation is key
  3. Security is not optional - SSH keys, firewalls, updates
  4. Backups are essential - Automate with Litestream
  5. Monitor everything - Logs, errors, performance
  6. Start simple, scale later - Don't over-engineer

When to Use What?

Use Kamal when:

  • You want full control
  • Cost optimization is important
  • You enjoy managing infrastructure
  • You have specific compliance needs

Use PaaS when:

  • You want to focus only on code
  • You need global presence quickly
  • You have a small team
  • You value convenience over cost

Both are valid choices!

Navigator: Go-based Web Server

  • Multi-tenant Rails applications
  • Dynamic process management
  • Intelligent routing
  • Asset caching

Replaces nginx/Passenger

github.com/rubys/navigator

Resources

Free and open source

Your Next Steps

  1. Try deploying locally with Docker
  2. Set up a simple VPS
  3. Follow the book's recipes
  4. Join the community

Start simple, scale when needed!

Questions?

Common topics:

  • Kamal vs Kubernetes vs Microservices
  • Cost comparisons with PaaS
  • Database choices (SQLite vs PostgreSQL)
  • Shared Nothing vs Traditional architectures
  • Capacity planning and performance
  • Debugging deployments
  • Team scaling and organizational impact

Thank you!

Remember: Start simple, scale when needed