cd ../articles

DevOps System NexiCloud

NexiCloud, part 2: CI/CD, Ansible, monitoring and backups

A git push that tests, builds, ships to staging and rolls out to three servers in fifteen seconds. Plus ten Ansible playbooks, Prometheus alerts and backups that are actually restored.

  • Gitea Actions
  • Docker
  • Ansible
  • Prometheus
  • Grafana

Part 1 made NexiCloud survive load and failures. This part is about the people running it: a pipeline that turns a git push into a tested production release, configuration that lives in Git instead of in someone's head, monitoring that pages before customers notice, and backups that are actually restored.

Part 2 of 3. Part 1: architecture and high availability. Part 3: security audit.

What I inherited

The containers existed, but several were empty shells:

  • The "gitlab-ci" container had Docker and no runner at all.
  • The "ansible" container had an inventory, no playbook and no SSH keys (and a broken locale).
  • Prometheus monitored only itself.
  • Docker used the vfs storage driver: every image layer a full copy, slow and disk-hungry.

Git and CI: Gitea instead of GitLab

The brief mentioned GitLab. GitLab wants 4 GB of RAM or more just to idle; Gitea runs in 512 MB and its Actions use the same YAML syntax as GitHub Actions. On a PoC sharing one host with 17 other containers, that was an easy call.

PieceContainerRole
Gitea302Repository dev/corporate-site, Actions UI
act_runner312Executes jobs, builds images (Docker, overlay2)
registry:2313Private image registry: every version, tagged by commit
staging314Runs the candidate image before production

The pipeline: seven steps, fifteen seconds

nexicloud-devops.svg
From git push to production in 15 seconds
From git push to production in 15 seconds
Text version
git push main
   │
   ├─ 1 checkout        clone the exact commit
   ├─ 2 tests           sh tests/test.sh              ── fail here → nothing ships
   ├─ 3 build           docker build  :<sha7> + :latest
   ├─ 4 push            → registry 172.16.1.23:5000
   ├─ 5 staging         docker run on CT 314
   ├─ 6 acceptance      /health OK and page shows <sha7>
   └─ 7 production      rolling: web1 → web2 → web3, each checked
.gitea/workflows/ci.yml (excerpt)
on:
  push:
    branches: [main]

env:
  IMAGE: 172.16.1.23:5000/nexicloud/corporate-site
  SSH: ssh -i /root/.ssh/ci_deploy -o BatchMode=yes

jobs:
  pipeline:
    runs-on: host
    steps:
      - name: 2. Tests
        run: cd src && sh tests/test.sh

      - name: 3. Build
        run: |
          V=$(echo ${{ gitea.sha }} | cut -c1-7)
          cd src && docker build -q --build-arg VERSION=$V -t $IMAGE:$V -t $IMAGE:latest .

      - name: 5. Deploy to staging
        run: |
          $SSH deploy@172.16.1.24 "docker pull -q $IMAGE:$V && \
            (docker rm -f corporate || true) && \
            docker run -d --name corporate --restart unless-stopped -p 80:80 $IMAGE:$V"

      - name: 6. Acceptance on staging
        run: |
          curl -fsS http://172.16.1.24/health
          curl -fsS http://172.16.1.24/ | grep -q "$V"

      - name: 7. Production (rolling update)
        run: |
          cid=$(docker create $IMAGE:$V); docker cp $cid:/usr/share/nginx/html out; docker rm $cid
          for h in 172.16.1.11 172.16.1.15 172.16.1.16; do
            rsync -a --delete -e "$SSH" out/ deploy@$h:/var/www/html/
            curl -fsS http://$h/ | grep -q "$V" && echo "$h: $V in production"
          done

Three details I care about:

  • The version is the commit. The build stamps the short SHA into the page, so steps 6 and 7 can check that the server really serves this version, not just "something".
  • Production gets the files from the image that passed staging. Not a fresh build, the same artefact, extracted with docker cp. What was tested is what ships.
  • One server at a time. Each web server is updated and checked before the next; HAProxy keeps the others serving. If one fails its check, the job stops with two servers still on the old version.

Rollback is a tag away: the registry keeps every version (curl http://172.16.1.23:5000/v2/nexicloud/corporate-site/tags/list).

Ansible: one source of truth

Before Ansible, configuration files had been copied by hand into a folder of the repo, and nobody knew which copy matched which server. I replaced them with templates and playbooks, run from CT 303, that manage all 17 other containers.

ansible/
├── inventory.ini          groups: dns, corporate_web, boutique, dev, infra…
├── group_vars/all.yml     network flow matrix (who may reach what)
├── host_vars/<ct>.yml     per-container firewall rules and settings
├── templates/             haproxy.cfg.j2, default.vcl.j2, named.conf.*.j2,
│                          nftables-nexicloud.nft.j2, prometheus.yml.j2…
└── playbooks/             10 playbooks, below
PlaybookWhat it does
base.ymlPackages, hardened SSH, node_exporter, fail2ban
firewall.ymlnftables per container from the flow matrix, security updates
dns.ymlbind9 primary and secondary, resolv.conf everywhere
web.ymlweb1–3, serial: 1 so only one is touched at a time
frontal.ymlHAProxy, Varnish, anti-DDoS fail2ban jail
boutique.ymlShop vhost, PHP-FPM restrictions
staging.ymlDocker host for staging, CI runner
monitoring.ymlPrometheus targets from the inventory, alerts, Grafana
backup.ymlBackup script, cron, restricted keys
audit.ymllynis on sensitive containers, nmap/siege tooling

Firewall rules are data, not code. A container's open ports are a list in its host_vars; the template turns them into nftables:

host_vars/db-master.yml
fw_rules:
  # MariaDB: only shop-php, phpMyAdmin, the replica and the admin container
  - { port: 3306, proto: tcp, from: [172.16.1.18, 172.16.1.19, 172.16.1.21, 172.16.1.13] }
cd /opt/nexicloud/ansible
ansible all -m ping
ansible-playbook playbooks/web.yml --check --diff   # show what would change, change nothing

--check --diff is the habit Ansible taught me: before touching production, read the diff.

Monitoring: Prometheus and Grafana

Every container runs node_exporter; HAProxy exposes its own metrics on :8405. Prometheus on CT 315 scrapes 20 targets every 15 seconds, and its target list is generated from the Ansible inventory, so a new container is monitored the moment it exists. Grafana shows the "Node Exporter Full" dashboard plus HAProxy panels.

AlertFires when
InstanceDownany target stops answering (up == 0)
BackendWebDownHAProxy marks a web server down
HighCPUa host above 85% CPU over 2 minutes
DiskAlmostFullless than 15% free on /
HTTPErrorRatemore than one 5xx per second at the front
RateLimitTriggeredthe anti-DDoS rule is rejecting clients
BackupTooOldno successful backup in 26 hours

The last one is the one I would keep in any job: backups fail silently, so the absence of a success has to be an alert.

Backups that are restored

CT 316 runs a backup every night at 01:30:

nexi-backup.sh (excerpt)
set -euo pipefail
DST=/srv/backup/$(date +%F_%H%M); LAST=/srv/backup/latest

# 1. shop database, dumped from the REPLICA: zero load on the master
mariadb-dump -h 172.16.1.21 --single-transaction --routines --triggers --events \
  --databases wordpress | zstd -q -T0 > "$DST/wordpress.sql.zst"
zstd -tq "$DST/wordpress.sql.zst"                       # the archive must be readable

# 2. WordPress files, 3. Gitea repos + DB, 4. DNS zones
# unchanged files are hard links to yesterday's copy
rsync -a --delete --link-dest="$LAST/wordpress/" root@172.16.1.18:/var/www/html/ "$DST/wordpress/"

( cd "$DST" && sha256sum *.zst > SHA256SUMS )
ln -sfn "$DST" "$LAST"
find /srv/backup -maxdepth 1 -name '20*' -mtime +7 -exec rm -rf {} +   # keep 7 days

# tell Prometheus it worked
echo "nexicloud_backup_last_success_timestamp $(date +%s)" \
  > /var/lib/prometheus/node-exporter/backup.prom
  • --single-transaction on the replica: a consistent dump without locking anything.
  • --link-dest: each night is a full, browsable copy, but unchanged files cost nothing. Measured: 188 MB the first night, +5 MB the second.
  • Checksums and the textfile metric close the loop: integrity is checked, and success is visible in monitoring.

On top of that, the Proxmox host takes a vzdump snapshot of all 18 containers every Sunday at 02:30, without stopping them (232 MB in 10 seconds for the DNS container). And test-restauration.sh restores the latest dump into a scratch database and compares it with production: a backup you have not restored is a hope, not a backup.

Acceptance testing in one command

scripts/tests-recette.sh, run on the host, performs 46 checks: every container up, DNS answers from both servers, round-robin across the three web servers, cache hits, HTTPS redirect on the shop, replication running, firewall refusing what it should, latest backup younger than a day… It ends with "all tests OK" or with the list of what broke. I run it before every demo, and it is the first thing I would hand to whoever takes over.

What I would do next

  • Run the Ansible playbooks from the pipeline too, so infrastructure changes get the same review → check → apply flow as the website.
  • Encrypt the backups (age) and push a copy off-site, for a real 3-2-1.
  • Ship logs as well as metrics (Loki), so an alert links straight to what happened.

Next: part 3, auditing and hardening the platform.