source: Klonkt/deploy/MULTI-INSTANCE.md@ 2dd1dc4

main
Last change on this file since 2dd1dc4 was 2dd1dc4, checked in by Robin <roboburr@…>, 6 weeks ago

Scheid gebruikersdata van code voor self-hosters

Een instance bewaarde zijn database, uploads en .env binnen de checkout. Daardoor
bevatte de codemap levende gebruikersdata: opruimen bij een deploy kon uploads
raken, een back-up moest de data tussen de code vandaan vissen, en een tweede
site vroeg een tweede kopie van alles, inclusief node_modules, die apart
bijgewerkt moest worden.

Nu staat de code in /opt/klonkt en de data per instance in /var/lib/klonkt/<slug>.
De checkout is daarmee wegwerpbaar: weggooien en opnieuw klonen laat elke
instance intact. Een site toevoegen is een map plus een .env, zonder tweede
kopie van de code, en klonkt-update brengt ze in een keer allemaal naar de
nieuwe versie.

De systemd-template draait als de klonkt-gebruiker met ProtectSystem=strict en
ReadWritePaths op alleen de eigen datamap. Een instance kan dus niet in de code
schrijven en niet bij de data van een andere instance, ook niet als er in de app
iets misgaat.

Changed files:
scripts/install.sh

  • KLONKT_DATA_ROOT en KLONKT_SLUG toegevoegd, slug afgeleid van het domein
  • verse installatie zet data in /var/lib/klonkt/<slug> en start klonkt@<slug>
  • bestaande installaties met een .env in de checkout blijven ongemoeid, opnieuw draaien mag nooit een levende database verplaatsen
  • weigert bij een database in de checkout zonder .env, te dubbelzinnig
  • klonkt-update herstart voortaan elke instance, niet alleen klonkt.service

deploy/DEPLOY.md

  • sectie 9b: meerdere zelfstandige Klonkts naast elkaar, met verwijzing
  • onderscheid verduidelijkt met de bestaande multi-tenant sectie, die gaat over sites binnen een instance

New file:
deploy/klonkt@.service

  • systemd template-unit, een service per instance, gedeelde code

scripts/klonkt-migrate-data.sh

  • zet een bestaande installatie om, met --dry-run en een rollback-pad
  • weigert op code zonder src/config/paths.js, anders schrijft de app alsnog naast zijn eigen code

scripts/klonkt-add-instance.sh

  • nieuwe instance: datamap, .env met verse SESSION_SECRET en vrije poort, service en Caddy-blok

deploy/MULTI-INSTANCE.md

  • indeling, eigenaarschap en rechten, migratie, instances toevoegen, updaten, back-up en verwijderen

Nog te doen: dit is getest op de fleet en met een lokale rooktest, maar de
verse-installatiestap zelf is nog niet op een schone VPS gedraaid.

-robo
Co-Authored-By: Claude Opus 5 <noreply@…>

  • Property mode set to 100644
File size: 6.2 KB
Line 
1# Running several Klonkts on one server
2
3One copy of the code, one directory of data per site. Adding a site is a
4directory and an `.env` file; updating is one command for all of them.
5
6```
7/opt/klonkt/ the code. Shared. Replaceable. No user data.
8/var/lib/klonkt/<slug>/ one instance: its .env, database, uploads.
9 .env
10 database.sqlite
11 media/
12 audio/
13```
14
15A *slug* is just a short name for an instance: `boiert`, `blog`, `label`. It is
16the directory name under `/var/lib/klonkt` and the systemd instance name
17(`klonkt@boiert`). It never appears in a URL.
18
19New installs get this layout on their own; the installer derives the slug from
20the domain (`boiert.eu` becomes `boiert`) unless you set `KLONKT_SLUG`. Servers
21installed before this existed keep their old layout untouched until you convert
22them, which is the section below.
23
24## Why the split
25
26When the database and uploads sit inside the checkout, the code directory holds
27live user data. That has three consequences you feel sooner or later:
28
29- **Updates get risky.** Anything that cleans up the checkout can reach your
30 uploads. Deleting and re-cloning the code becomes impossible.
31- **Backups get vague.** You either back up the code as well or you have to pick
32 the data out from between it.
33- **A second site needs a second copy of everything**, including `node_modules`,
34 and every copy has to be updated separately.
35
36After the split the checkout is disposable: throw it away, clone it again, and
37every instance still has its data. Backing up `/var/lib/klonkt/<slug>` backs up
38the whole instance, configuration included.
39
40## Who owns what
41
42Everything runs as the unprivileged `klonkt` system user, which is created by
43the installer and cannot log in.
44
45| Path | Owner | Mode | Written by |
46|---|---|---|---|
47| `/opt/klonkt` | `klonkt` | `0755` | `klonkt-update`, never the running app |
48| `/var/lib/klonkt/<slug>` | `klonkt` | `0750` | the app |
49| `/var/lib/klonkt/<slug>/.env` | `klonkt` | `0600` | you |
50
51Root is needed to install the systemd unit, create the directory and edit the
52web server config. The application itself never runs as root.
53
54The service unit narrows this further:
55
56```ini
57ProtectSystem=strict
58ReadWritePaths=/var/lib/klonkt/%i
59```
60
61The whole filesystem is read-only to the process except its own data directory.
62An instance cannot write into the code, and it cannot reach another instance's
63data, even if something inside the application goes wrong.
64
65## Migrating an existing install
66
67For a server that was installed the single site way, with everything under
68`/opt/klonkt`. Update the code first: the split needs a build where every media
69subdirectory follows `MEDIA_PATH`, and the script refuses to run on anything
70older.
71
72```bash
73klonkt-update
74sudo bash /opt/klonkt/scripts/klonkt-migrate-data.sh <slug> --dry-run
75sudo bash /opt/klonkt/scripts/klonkt-migrate-data.sh <slug>
76```
77
78Pick a slug that describes the site, for example `boiert` for boiert.eu. The
79script stops the service (so SQLite writes out its log), moves `storage/` and
80`.env` to `/var/lib/klonkt/<slug>/`, rewrites the three data paths, installs the
81systemd template, and switches from `klonkt.service` to `klonkt@<slug>`.
82
83Your web server config does not change. The instance keeps the same port,
84because the port comes from the same `.env`.
85
86**Rolling back.** The old `klonkt.service` is disabled but not deleted. To go
87back, move the data into `/opt/klonkt/storage`, restore the relative paths in
88`.env`, and run `systemctl enable --now klonkt`.
89
90## Adding an instance
91
92```bash
93sudo bash /opt/klonkt/scripts/klonkt-add-instance.sh blog blog.example.com
94```
95
96That creates `/var/lib/klonkt/blog`, writes an `.env` with a fresh random
97`SESSION_SECRET` and a free port, starts `klonkt@blog`, and adds a Caddy block
98for the domain. Pass a port as a third argument to choose it yourself, or
99`--no-caddy` if you run your own proxy.
100
101Point the DNS at the server first, otherwise the certificate cannot be issued.
102
103Doing it by hand comes down to the same four things:
104
105```bash
106sudo mkdir -p /var/lib/klonkt/blog
107sudo tee /var/lib/klonkt/blog/.env >/dev/null <<'ENV'
108NODE_ENV=production
109PORT=3001
110HOST=127.0.0.1
111SESSION_SECRET=<openssl rand -hex 32>
112PUBLIC_BASE_URL=https://blog.example.com
113DATABASE_PATH=/var/lib/klonkt/blog/database.sqlite
114MEDIA_PATH=/var/lib/klonkt/blog/media
115AUDIO_PATH=/var/lib/klonkt/blog/audio
116ENV
117sudo chown -R klonkt:klonkt /var/lib/klonkt/blog
118sudo chmod 750 /var/lib/klonkt/blog && sudo chmod 600 /var/lib/klonkt/blog/.env
119sudo systemctl enable --now klonkt@blog
120```
121
122Every instance needs its own `PORT` and its own `PUBLIC_BASE_URL`. Give each one
123its own `SESSION_SECRET` as well: sharing it would make sessions from one site
124valid on another.
125
126The three data paths are the only ones you need. Media subdirectories such as
127`media/avatars` and `media/post-images` follow `MEDIA_PATH` on their own.
128
129## Updating them all at once
130
131```bash
132sudo klonkt-update
133```
134
135That pulls the code once into `/opt/klonkt`, reinstalls dependencies only when
136`package-lock.json` changed, and restarts every instance it finds under
137`/var/lib/klonkt`. There is one copy of the code, so no instance can lag behind.
138
139Watch one instance while it comes back:
140
141```bash
142journalctl -u klonkt@blog -f
143```
144
145## Backups
146
147One directory per instance:
148
149```bash
150systemctl stop klonkt@blog
151tar czf blog-$(date +%F).tar.gz -C /var/lib/klonkt blog
152systemctl start klonkt@blog
153```
154
155Stopping first keeps the SQLite file consistent. To back up without downtime,
156use `sqlite3 database.sqlite ".backup snapshot.db"` and archive the snapshot
157together with `media/` and `audio/`.
158
159There is nothing to back up in `/opt/klonkt`: it is a checkout of a public
160repository and `klonkt-update` recreates it.
161
162## Removing an instance
163
164```bash
165sudo systemctl disable --now klonkt@blog
166sudo rm -rf /var/lib/klonkt/blog # this deletes the site's data
167```
168
169Then remove its block from the web server config and reload it.
170
171## Everyday commands
172
173| | |
174|---|---|
175| `systemctl status klonkt@<slug>` | is it running |
176| `journalctl -u klonkt@<slug> -f` | follow the log |
177| `systemctl restart klonkt@<slug>` | restart one instance |
178| `klonkt-update` | update the code, restart all instances |
179| `ls /var/lib/klonkt` | which instances exist |
Note: See TracBrowser for help on using the repository browser.