diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bb414ce..2a311f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,11 @@ jobs: - name: The compose file is valid run: docker compose config --quiet + # the several-nodes setup, with its example settings + - name: The several-nodes compose file is valid + working-directory: scale + run: cp .env.example .env && docker compose config --quiet && rm .env + # the containers run as uid 1000, the checkout belongs to the runner's user (README.md) - name: Give the containers the data folder run: sudo chown -R 1000:1000 data diff --git a/.gitignore b/.gitignore index 9e75e1d..9177259 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,9 @@ data/rte-spool/ data/rte-files/ data/ansibleForms.lock +# The secrets of the several-nodes setup (scale/.env.example is the template) +scale/.env + # Editor and OS files .DS_Store .idea/ diff --git a/README.md b/README.md index 53b80a3..892e15b 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,11 @@ Ansible settings, roles and collections under `data/ansible/` are mounted into t playbooks need more, build an image FROM `ansibleforms-rte-core` or `ansibleforms-rte-full` and point the `rte` service at it: the app's [examples/rte](https://github.com/ansibleforms/ansibleforms/tree/main/examples/rte) shows how. +## Several nodes + +[scale/](scale) runs AnsibleForms on several nodes: two controller nodes behind nginx, two workers (one waiting), a runner and a database. +Its [README](scale/README.md) says what the nodes share, how they behave and how to upgrade them. + ## Kubernetes For Kubernetes, use the Helm chart in [ansibleforms/helm-charts](https://github.com/ansibleforms/helm-charts) instead. diff --git a/scale/.env.example b/scale/.env.example new file mode 100644 index 0000000..821cdb9 --- /dev/null +++ b/scale/.env.example @@ -0,0 +1,26 @@ +# The variables of compose.yaml : copy this file to .env and replace every change-me. +# The app nodes and the workers read all of it (env_file: .env) ; the runner only RTE_TOKEN, which +# compose.yaml hands it, so it never holds the database's passwords. + +# the database the app nodes and the workers share +DB_HOST=db +DB_PORT=3306 +# a user with rights on the AnsibleForms schema only (the db container creates it), not root +DB_USER=ansibleforms +DB_PASSWORD=change-me +# the database server's own root, for its administration ; AnsibleForms never uses it +DB_ROOT_PASSWORD=change-me-too +# the same on every app node and worker : credentials stored by one must decrypt on all +ENCRYPTION_SECRET=change-me-32-characters-or-more +# the same on every app node : a token signed by one must be accepted by all +ACCESS_TOKEN_SECRET=change-me-a-long-random-string +# the runner's bearer token (at least 16 characters) : the runner reads it, and the config seed puts +# the same token in the AnsibleForms credential its runner signs in with (seed.yaml) +RTE_TOKEN=change-me-at-least-16-characters +# the config seed the worker applies at every start : the runner (seed.yaml) +CONFIG_SEED_PATH=/app/dist/seed/seed.yaml +# the app nodes serve plain http behind the load balancer +HTTPS=0 +# each container's own log, NOT on the shared volume : several processes rotating one file +# lose lines (the default is persistent/logs, which every container here shares) +LOG_PATH=/app/dist/logs diff --git a/scale/README.md b/scale/README.md new file mode 100644 index 0000000..0a586db --- /dev/null +++ b/scale/README.md @@ -0,0 +1,75 @@ +# AnsibleForms on several nodes + +One image, started in different roles (`AF_ROLE`): + +| Role | Runs | How many | +|---|---|---| +| unset | the controller's web interface and API **and** the worker, in one process: the default | 1 | +| `controller` | the web interface and the API (`app` before 7.8, still read) | as many as you like, behind a load balancer | +| `worker` | the background work: the database bootstrap and the config seed, the schedules, the nightly backup, the repository syncs, the cleanups | 1 at work; more wait and take over | +| `rte` | the playbooks (the `ansibleforms-rte-full` image) | as many as you like | + +[compose.yaml](compose.yaml) runs two controller nodes behind nginx, two workers, one runner and a database. +Copy [.env.example](.env.example) to `.env` and fill in the secrets first. + +The runner runs in `inbound` mode (`RTE_MODE=inbound`): the controller dials it at `http://rte:8000` with its +`RTE_TOKEN` and keeps one WebSocket open, held by one node at a time. The runner holds no database +credentials and reads none of the `.env`. Its runner `rte` and the credential it signs in with are +declared by the config seed, [seed.yaml](seed.yaml), which the worker applies at every start +(`CONFIG_SEED_PATH`). See [the runner modes](https://ansibleforms.com/rte/modes.html). + +## What the nodes share + +- **The database.** It holds everything the nodes must agree on: the jobs, the worker lock, the + designer lock, the list of nodes (Status page) and the change notices that keep every node's + caches current (a credential, a runner or a schedule edited on one node is in force on all of + them within 5 seconds). +- **The persistent volume** (`/app/dist/persistent`): forms, the config, repositories, uploads, + backups and the managed `.env`. Every controller node and the worker mount the same one, read-write. +- **`~/.ssh`**: the key and the known hosts used for git over ssh. The worker creates the key on + the first start. +- **`ACCESS_TOKEN_SECRET`**, the same on every controller node, or a token signed by one node is refused + by the next. A controller node refuses to start without it. +- **`ENCRYPTION_SECRET`**, the same on every controller node and worker, or a credential stored by one + cannot be read by another. The runner needs none: the controller sends it each job's secrets. +- **`RTE_TOKEN`**, the same on the runner and in its credential (the seed reads it from `.env`). + +## How it behaves + +- **One worker at a time.** The worker holds the database lock `ansibleforms_worker`. When it + stops, the database releases the lock and a waiting worker takes it within 10 seconds. When its + host dies without a goodbye (power, a crashed node), the database would keep the lock for hours: + a waiting worker ends that session once no worker has written a heartbeat for two minutes, and + takes over. A worker that loses the lock to another one stops, and its container restarts as + the one waiting. A worker whose start fails stops too, so it never holds the lock doing nothing. +- **The schema is the worker's.** On an empty database the worker creates it, and on an upgrade it + patches it; controller nodes wait until both are done, and say in their log what is still missing. +- **Every container names itself** `--`: nothing to set. The Status page + lists them all, runners included. +- **Jobs belong to a container.** A job running on a runner belongs to that runner; it carries on when + the controller node that started it goes. Any other job (an AWX job, the steps of a multistep form) + belongs to the controller node that started it. A container that restarts ends its own unfinished + jobs; one that disappears (a replaced pod) has them ended by the worker after two minutes + without a heartbeat. +- **SIGHUP** on any node re-applies the config seed: a controller node asks the worker to do it. +- **Status** shows every node, its role and version, which one is the worker, and warns when the + nodes run different versions. + +## Upgrading + +Upgrade the **worker first**, then the controller nodes. The worker patches the schema; a controller node of +the new version waits until the schema is patched (it logs what it is waiting for), so controller nodes +upgraded first stay unready until the worker follows - the old ones keep serving meanwhile. Runners +can follow at any time while they speak the same contract ([`server/src/rte/contract.js`](https://github.com/ansibleforms/ansibleforms/blob/main/server/src/rte/contract.js) in the app). + +## Limits + +- The **chat assistant** keeps its conversations in the memory of one controller node: use sticky + sessions (nginx `ip_hash` here), or a single controller node. +- **Logs** stay in each container (`LOG_PATH` off the shared volume, as in `.env.example`): + several processes rotating one file lose lines. The log viewer shows the log of the node that + answers; send the logs somewhere central for the whole picture (`docker logs`, a log collector). +- **Galera and group replication** do not share the worker lock between database nodes: point + every worker at the same database node. +- Settings changed on the settings page are written to the managed `.env` on the shared volume + and applied on every node; the ones that need a restart need it on every node. diff --git a/scale/compose.yaml b/scale/compose.yaml new file mode 100644 index 0000000..6a09b5d --- /dev/null +++ b/scale/compose.yaml @@ -0,0 +1,82 @@ +# AnsibleForms split over several containers : two controller nodes behind nginx, one worker (and a +# second one waiting), one RTE, one database. A starting point - see README.md. +# +# cp .env.example .env # and fill in the secrets +# docker compose up -d # then https://localhost:8443 +x-ansibleforms: &ansibleforms + image: ghcr.io/ansibleforms/ansibleforms:7 + restart: unless-stopped + depends_on: [db] + env_file: .env + volumes: + # one volume for every controller node and the worker : forms, repositories, uploads, backups + - persistent:/app/dist/persistent + # the ssh key and known_hosts for git over ssh, the same everywhere + - ssh:/home/node/.ssh + # the config seed the worker applies at every start (CONFIG_SEED_PATH in .env) : the RTE's + # runner, inbound, and the credential it signs in with + - ./seed.yaml:/app/dist/seed/seed.yaml:ro + +services: + db: + image: mariadb:11 + restart: unless-stopped + environment: + MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} + # the user AnsibleForms connects with : all rights on its own schema, none elsewhere + MARIADB_DATABASE: AnsibleForms + MARIADB_USER: ${DB_USER} + MARIADB_PASSWORD: ${DB_PASSWORD} + volumes: + - mysql:/var/lib/mysql + + controller1: + <<: *ansibleforms + environment: { AF_ROLE: controller } + controller2: + <<: *ansibleforms + environment: { AF_ROLE: controller } + + # the background work ; worker2 waits and takes over when worker1 stops + worker1: + <<: *ansibleforms + environment: { AF_ROLE: worker } + worker2: + <<: *ansibleforms + environment: { AF_ROLE: worker } + + # runs the playbooks. The controller dials it on http://rte:8000 with RTE_TOKEN (RTE_MODE=inbound) and + # keeps one WebSocket open, from the controller node that holds it : the RTE holds no database + # credentials, so it does not read the .env, and it is not published on the host. Its runner + # 'rte' is declared by the config seed (seed.yaml). ansible with its collections ; + # ansibleforms-rte-core is the smaller one to build your own on (ansibleforms/rte's examples) + rte: + image: ghcr.io/ansibleforms/ansibleforms-rte-full:7 + restart: unless-stopped + environment: + NODE_ENV: production + RTE_MODE: inbound + RTE_TOKEN: ${RTE_TOKEN} + # its files are the controller's own (the shared volume, mounted below) : nothing to send with + # each job ; without the volume, leave RTE_FILES out and the controller sends them (transfer) + RTE_FILES: local + PORT: "8000" + # only the controller nodes call it, inside the compose network + HTTPS: "0" + volumes: + - persistent:/app/dist/persistent + - ssh:/home/node/.ssh + + lb: + image: nginx:stable + restart: unless-stopped + depends_on: [controller1, controller2] + ports: ["8443:443"] + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro + - ./certs:/etc/nginx/certs:ro + +volumes: + persistent: + ssh: + mysql: diff --git a/scale/nginx.conf b/scale/nginx.conf new file mode 100644 index 0000000..a234359 --- /dev/null +++ b/scale/nginx.conf @@ -0,0 +1,20 @@ +# ip_hash keeps a browser on one app node : the chat assistant keeps its conversations in the +# memory of the node that started them. Everything else works from any node. +upstream ansibleforms { + ip_hash; + server controller1:8000; + server controller2:8000; +} +server { + listen 443 ssl; + ssl_certificate /etc/nginx/certs/cert.pem; + ssl_certificate_key /etc/nginx/certs/key.pem; + client_max_body_size 0; + location / { + proxy_pass http://ansibleforms; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_read_timeout 300s; + } +} diff --git a/scale/seed.yaml b/scale/seed.yaml new file mode 100644 index 0000000..768fce3 --- /dev/null +++ b/scale/seed.yaml @@ -0,0 +1,21 @@ +# Config seed, applied by the worker at every start (CONFIG_SEED_PATH in .env). It declares the +# rte container of compose.yaml as the default runner, so playbook forms run there, signing in +# with the credential below. The controller dials the RTE (RTE_MODE=inbound) over one WebSocket : the RTE +# holds no database credentials. Objects declared here are read-only in the interface ; see +# https://ansibleforms.com/seed/ and https://ansibleforms.com/rte/modes.html +version: 1 +credentials: + items: + - name: rte + credential_type: rte + description: The RTE_TOKEN of the rte container of compose.yaml, and its address + url: http://rte:8000 + password: ${RTE_TOKEN} +runners: + items: + - name: rte + type: rte + rte_mode: inbound + description: The rte container of compose.yaml + credential: rte + is_default: true