Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Kamal Support for Self deployment #1163

Open
yinho999 opened this issue Jan 9, 2025 · 1 comment
Open

Adding Kamal Support for Self deployment #1163

yinho999 opened this issue Jan 9, 2025 · 1 comment
Labels
enhancement New feature or request generator

Comments

@yinho999
Copy link
Contributor

yinho999 commented Jan 9, 2025

Description

The current self-host deployment requires additional setup on the VPS like nginx and more. We can remove this unnecessary barrier via using ROR ecosystem Kamal

Usage

  • cargo loco generate --kind kamal
  • kamal setup
@yinho999 yinho999 added the enhancement New feature or request label Jan 9, 2025
@yinho999
Copy link
Contributor Author

yinho999 commented Jan 11, 2025

Current process to get Kamal working in a brand new sqlite project

  1. loco new
  2. Adding 0.0.0.0 to deployment.yaml - server -> binding
  3. cargo loco generate deployment to generate dockerfile
  4. Add sea orm installation after FROM debian:bookworm-slim since we need sea orm to run the application
# Install required system dependencies
RUN apt-get update && apt-get install -y \
    pkg-config \
    libpq-dev \
    libssl-dev \
    curl \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Install sea-orm-cli
RUN cargo install sea-orm-cli
  1. Add COPY --from=builder /usr/src/assets/views /usr/app/assets/views into the debian step since we also need views
  2. Update dockerfile from FROM rust:1.74-slim as builder to FROM rust:1.85-slim as builder
  3. Update dockerfile from ENTRYPOINT ["/usr/app/project_name"] to ENTRYPOINT ["/usr/app/project_name", "start"]
  4. kamal init to init all the files
  5. export KAMAL_REGISTRY_PASSWORD="docker person access token" in the terminal. Personal access token
  6. config/deploy.yml stuff that needed to be changed
service: project_container_name
image: docker_username/repository_name

servers:
  web:
    - your_web_server

proxy:
  ssl: true
  host: your_url_on_cloudflare
  app_port: 5150
  healthcheck:  
    interval: 3  
    path: /_health  
    timeout: 3

registry:
    username: docker_username

builder:
   dockerfile: dockerfile 
  # Pass in additional build args needed for your Dockerfile.
  # args: 
  #  make sure to remove the file read function 
  #   RUBY_VERSION: 

Eventually we will have something like this

# config/deploy.yml
# Name of your application. Used to uniquely configure containers.
service: project_container_name

# Name of the container image.
image: docker_username/docker_repository

# Deploy to these servers.
servers:
  web:
    - XXX.XXX.XXX.XXX # VPS ip address
  # job:
  #   hosts:
  #     - 192.168.0.1
  #   cmd: bin/jobs

# Enable SSL auto certification via Let's Encrypt and allow for multiple apps on a single web server.
# Remove this section when using multiple web servers and ensure you terminate SSL at your load balancer.
#
# Note: If using Cloudflare, set encryption mode in SSL/TLS setting to "Full" to enable CF-to-app encryption.
proxy:
  ssl: true
  host: your_domain.com
  # Proxy connects to your container on port 80 by default.
  app_port: 5150
  healthcheck:
    interval: 3
    path: /_health
    timeout: 3

# Credentials for your image host.
registry:
  # Specify the registry server, if you're not using Docker Hub
  # server: registry.digitalocean.com / ghcr.io / ...
  username: docker_username

  # Always use an access token rather than real password (pulled from .kamal/secrets).
  password:
    - KAMAL_REGISTRY_PASSWORD

# Configure builder setup.
builder:
  arch: amd64
  # Pass in additional build args needed for your Dockerfile.
  # args:
  dockerfile: dockerfile
# ------- rest of the comments
# config/development.yaml 

# ------ rest of the config

# Web server configuration
server:
  # Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
  port: 5150
  # The UI hostname or IP address that mailers will point to.
  host: http://localhost
  # Binding to 0.0.0.0 will allow the server to listen on all available interfaces.
  binding: 0.0.0.0

# ------ rest of the config
# dockerfile
FROM rust:1.84-slim as builder

WORKDIR /usr/src/

COPY . .

RUN cargo build --release

FROM debian:bookworm-slim
# Install required system dependencies
RUN apt-get update && apt-get install -y \
    pkg-config \
    libpq-dev \
    libssl-dev \
    curl \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Install sea-orm-cli
RUN cargo install sea-orm-cli
WORKDIR /usr/app

COPY --from=builder /usr/src/assets/static /usr/app/assets/static
COPY --from=builder /usr/src/assets/views /usr/app/assets/views

COPY --from=builder /usr/src/assets/static/404.html /usr/app/assets/static/404.html
COPY --from=builder /usr/src/config /usr/app/config
COPY --from=builder /usr/src/target/release/project_name /usr/app/project_name

ENTRYPOINT ["/usr/app/project_name","start"]
  1. git init && git add . && git commit -m "Init repo" - to init the project and commit
  2. kamal setup

Notes:

  • use gem install kamal to install kamal instead of using alias.
  • We need to setup ssh key for ssh connection between VPS and your machine
  • using cloudflare domain. Please point your domain to the vps server
  • using cloudflare domain. Please configure "SSL/TLS" to full

I will work on a project with postgres + redis and see how does the setup for those project works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request generator
Projects
None yet
Development

No branches or pull requests

2 participants