## Kamal
Deploy web apps anywhere.
From bare metal to cloud VMs.
https://kamal-deploy.org/
## Cheatsheet
Cette cheatsheet peut s'adapter à différents environnements. Elle est partagée dans le cadre d'une utilisation de Kamal avec un GitLab auto-hébergé avec son Docker Registry, et un Bitwarden auto-hébergé.
1. Installer [Bitwarden CLI](https://bitwarden.com/help/cli/) :
```bash
npm install -g @bitwarden/cli
```
2. Configurer le serveur Bitwarden si auto-hébergé :
```bash
bw config server https://bw.societe.fr
```
3. Stocker les secrets dans les champs personnalisés (par exemple `RAILS_MASTER_KEY`, `KAMAL_REGISTRY_PASSWORD` et `POSTGRES_PASSWORD`) d'un nouvel élément (par exemple `projet-production`) dans Bitwarden Password Manager
4. Configurer les secrets dans votre projet Ruby on Rails, par exemple pour l'environnement de production dans le fichier `.kamal/secrets.production` :
```bash
SECRETS=$(kamal secrets fetch --adapter bitwarden --account
[email protected] --from projet-production KAMAL_REGISTRY_PASSWORD POSTGRES_PASSWORD RAILS_MASTER_KEY)
KAMAL_REGISTRY_PASSWORD=$(kamal secrets extract KAMAL_REGISTRY_PASSWORD ${SECRETS})
POSTGRES_PASSWORD=$(kamal secrets extract POSTGRES_PASSWORD ${SECRETS})
RAILS_MASTER_KEY=$(kamal secrets extract RAILS_MASTER_KEY ${SECRETS})
```
5. Configurer le fichier `config/deploy.yml`, par exemple :
```yml
# Name of your application. Used to uniquely configure containers.
service: projet
# Name of the container image.
image: group/projet
# Enable SSL auto certification via Let's Encrypt (and allow for multiple apps on one server).
proxy:
 ssl: true
# Credentials for your image host.
registry:
 server: registry.gitlab.societe.fr
 username:
  - KAMAL_REGISTRY_PASSWORD
 password:
  - KAMAL_REGISTRY_PASSWORD
# Inject ENV variables into containers (secrets come from .kamal/secrets).
env:
 secret:
  - RAILS_MASTER_KEY
  - POSTGRES_PASSWORD
 clear:
aliases:
 console: app exec --interactive --reuse "bin/rails console"
 shell: app exec --interactive --reuse "bash"
 logs: app logs -f
 dbc: app exec --interactive --reuse "bin/rails dbconsole"
# Use a persistent storage volume for sqlite database files and local Active Storage files.
# Recommended to change this to a mounted volume path that is backed up off server.
volumes:
 - "project_storage:/rails/storage"
# Bridge fingerprinted assets, like JS and CSS, between versions to avoid
# hitting 404 on in-flight requests. Combines all files from new and old
# version inside the asset_path.
asset_path: /rails/public/assets
# If necessary, build image via remote server (useful for faster amd64 builds on arm64 computers)
# builder:
# Â arch: amd64
# Â remote: ssh://
[email protected]
# Use your SSH private key
ssh:
 keys: [ "~/.ssh/id_ed25519" ]
```
6. Préparer un serveur (par exemple sous Debian 12) et autorisez votre clé publique SSH à s'y connecter soit en `root`, soit avec un utilisateur dédié (pensez dans ce cas à consulter la documentation de Kamal)
7. Configurer le fichier `config/deploy.production.yml`, par exemple :
```yml
servers:
 web:
  - &host 192.168.0.1
env:
 clear:
  DB_HOST: *host
proxy:
 host: projet.fr
# Use accessory services (secrets come from .kamal/secrets).
accessories:
 db:
  image: postgres:16
  host: *host
  port: 5432
  env:
   clear:
    POSTGRES_USER: projet
    POSTGRES_DB: projet_production
   secret:
    - POSTGRES_PASSWORD
  directories:
   - data:/var/lib/postgresql/data
```
7. Lancer l'installation et le déploiement sur l'environnement de production :
```bash
kamal setup -d production
```
8. Lancer vos futurs déploiements avec :
```bash
kamal deploy -d production
```
Adaptez autant que vous voulez, par exemple pour un environnement de `staging`.
Vous pouvez également vous inspirer de ces articles :
## Articles
## Migrer de Dokku vers Kamal
[[Migration de Dokku vers Kamal]]
### How to Deploy and Scale your Rails application with Kamal
https://guillaumebriday.fr/how-to-deploy-and-scale-your-rails-application-with-kamal
### How to deal with PostgreSQL Backups using Kamal
https://www.botzenhart.io/articles/how-to-deal-with-postgresql-backups-using-kamal
### Automated Database Backups with Kamal
https://mileswoodroffe.com/articles/kamal-database-backups
### Easy Postgres backups
https://www.erikminkel.com/2024/06/24/easy-postgres-backups/
### Utiliser la registry de conteneur de Gitlab
https://blog.stephane-robert.info/post/gitlab-container-docker-registry/
### Migrating From Dokku to Kamal
https://glaucocustodio.github.io/
### How to deploy multi-environment(staging, production) application using Kamal(previously MRSK)
https://www.kartikey.dev/2023/04/09/how-to-deploy-multi-environment-staging-production-application-using-mrsk.html