Skip to content

Buildx cleanup


title: GitLab node buildx cleanup description: Incident record from 2026-08-18: buildx and buildkit builder sprawl filled disk and memory on the GitLab k3s node, and the cleanup. audience: internal type: explanation owner: platform-team last_reviewed: 2026-11-19


Date: 2026-08-18 Node: ubuntu-mashup-gitlab-haproxy (cloud k3s cluster, DigitalOcean SGP1) Trigger: Disk usage crept back up toward capacity again (this same node caused the 2026-05-14 APISIX/etcd outage documented separately), and RAM was reported at ~94%.


1. Symptom

  • df -h / showed /dev/vda1 at 256G/310G used (83%), only 55G free.
  • free -h and ps aux --sort=-%mem showed RAM under heavy pressure.
  • Initial du scans of /, /var, and /var/lib repeatedly timed out or hung — caused by du recursing into slow NFS-backed pod volume mounts (GitLab, Zitadel) and thousands of small files under Docker's overlay2 layers. Fixed by scanning one directory at a time with sudo timeout N du -sh <path> (note: timeout must come before sudo, not after, or the signal never reaches the actual du process).

2. Investigation Path

Ruled out in order: - /var/lib/rancher/k3s — only 30G total (29G agent/containerd, 482M server, 230M data). Not the cause. - /var/lib/longhorn — 23G. Normal. - /data (local GitLab/Zitadel storage this node also serves via NFS) — 42G. Normal, and much lower than the 65GB of orphaned registry blobs found in the May incident, suggesting GitLab's registry garbage collection policy (set up after that incident) is working. - /home, /opt, /root, /usr, /tmp, /var/log (5.3G), /var/cache — all small, non-issues.

Root cause found: ps aux --sort=-%mem surfaced multiple long-running buildkitd processes. ps -ef | grep buildkitd showed 15 separate processes, several running continuously for 46–53 days. docker ps -a | grep -i build identified them as docker buildx multi-arch builder containers, one per CI/CD project:

buildx_buildkit_multiarch-conciergeguestapp0
buildx_buildkit_multiarch-castis-website0
buildx_buildkit_multiarch-platformconsole0
buildx_buildkit_multiarch-spatioviewadmin0
buildx_buildkit_multiarch-playtellyinternal0
buildx_buildkit_multiarch-promoweb0
buildx_buildkit_multiarch-orgconsole0
buildx_buildkit_multiarch-ticketcms0
buildx_buildkit_multiarch-ticketadmin0
buildx_buildkit_multiarch-authapi0
buildx_buildkit_multiarch-playoutadmin0
buildx_buildkit_multiarch-coreapi0
buildx_buildkit_multiarch-documentations0
buildx_buildkit_multiarch-tellyboardadmin0
buildx_buildkit_multiarch-tellyid0

These are created by GitLab CI (docker buildx create --use --name multiarch-$PROJECT_NAME) to keep a warm build cache per project, but are never torn down — so one gets created per project and lives forever.

RAM impact: ps -eo pid,rss,comm | grep buildkitd | awk '{sum+=$2} END {print sum/1024" MB"}' → ~3.16GB total across all 15.

Disk impact — the real culprit. Each container's own writable layer was 0B (docker ps -a -s showed only ~242MB virtual size, i.e. the shared base image). The actual cache lives in a named Docker volume mounted at /var/lib/buildkit inside each container (buildx_buildkit_multiarch-<project>0_state). Measuring each volume directly:

for v in $(docker volume ls -q | grep buildx_buildkit); do
  du -sh "/var/lib/docker/volumes/$v/_data"
done

Totaled ~90GB across the 15 volumes (individual builders ranged from 1.3G up to 9.7G). This was the dominant chunk of the 256GB used on /.

Contributing factor: buildkitd's garbage collection is not time-based — it only runs opportunistically during an active build, and only evicts cache once a size threshold is crossed. A builder dedicated to a single project may never hit that threshold on its own, so cache just grows unbounded for as long as the builder exists (explaining multi-month, multi-GB accumulation with zero automatic cleanup).


3. Fix #1 — Pool Builders (15 → 3)

Rather than one builder per project forever, GitLab CI now assigns each project to one of a fixed pool of 3 shared builders (multiarch-pool-0, multiarch-pool-1, multiarch-pool-2), chosen deterministically by hashing the project name — so a given project always lands on the same pool member (preserving cache warmth) while the total builder count is capped.

Logic (in .gitlab-ci/.gitlab-ci-build.yml / .hotel-ci-build.yml):

POOL_SIZE=3
POOL_INDEX=$(( 0x$(echo -n "$PROJECT_NAME" | md5sum | cut -c1-6) % POOL_SIZE ))
BUILDER_NAME="multiarch-pool-${POOL_INDEX}"
- MD5 hash of the (lowercased) project name, first 6 hex chars, interpreted as hex and taken mod 3. - Deterministic (same project → same pool every run), no shared state/counter needed, portable across bash/dash shell arithmetic. - Verified real-world distribution across all 15 projects: 6 / 4 / 5 split — reasonably balanced.

Bonus side effect: with 5-ish projects sharing one builder's cache budget instead of 1, buildkit's built-in GC threshold is reached far sooner, so automatic eviction now actually functions — this was effectively broken before under the one-builder-per-project setup.

Rollout — files updated (identical block, patched programmatically across all copies): - AuthAPI, CoreAPI, Promoweb, SpatioViewAdmin, castis-website, PlayTellyInternal (both .gitlab-ci-build.yml and .hotel-ci-build.yml), PlatformConsole, TellyboardAdmin, documentations, tellyid — .gitlab-ci-build.yml - OrgConsole, PlayoutAdmin — both .gitlab-ci-build.yml and .hotel-ci-build.yml - conciergeguestapp — .hotel-ci-build.yml

All committed and pushed; confirmed live via docker ps showing buildx_buildkit_multiarch-pool-00 and buildx_buildkit_multiarch-pool-20 created by actual pipeline runs.

Known follow-up: ticketcms and ticketadmin builder containers existed on the host, but ticketcms's current CI uses plain docker build (no buildx) — likely a leftover from a prior setup. Not yet confirmed whether ticketadmin's CI still uses buildx directly; worth checking before assuming it's fully migrated.

Considered but not adopted (for now): fully ephemeral builders (docker buildx create → build → docker buildx rm every job), relying entirely on the registry-based cache (--cache-from/--cache-to type=registry,mode=max) that CI already pushes to. This would mean zero persistent disk/RAM footprint at all, at the cost of a slower per-job cache pull from the registry instead of a warm local cache. Decision: keep the pool-of-3 middle ground for now since it preserves most of the speed benefit; ephemeral remains an option if disk/RAM pressure returns.


4. Fix #2 — Reclaim Existing Cache

Old per-project builders and volumes removed once the pooled builders were confirmed working:

docker ps -a --filter "name=buildx_buildkit_multiarch" --format "{{.Names}}" | grep -v "pool" | xargs -r docker rm -f
docker volume ls -q | grep "buildx_buildkit_multiarch" | grep -v "pool" | xargs -r docker volume rm

Result: /dev/vda1 usage dropped from 256G (83%) → 155G (50%), freeing roughly 100GB.


5. Fix #3 — Automatic Weekly Cache Pruning (prevents recurrence)

Script at /usr/local/bin/buildkit-cache-cleanup.sh:

#!/bin/bash
# Prunes buildkit cache on the pooled multiarch builders, keeping anything used in the last 7 days.
echo "[$(date)] Starting buildkit cache cleanup"
for i in 0 1 2; do
  BUILDER="multiarch-pool-${i}"
  CONTAINER="buildx_buildkit_multiarch-pool-${i}0"
  if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}\$"; then
    echo "[$(date)] Pruning $BUILDER ($CONTAINER)..."
    docker exec "$CONTAINER" buildctl prune --keep-duration 168h 2>&1
  else
    echo "[$(date)] $CONTAINER not running, skipping"
  fi
done
echo "[$(date)] Disk usage after cleanup:"
df -h /
echo "[$(date)] Cleanup finished"

Scheduled weekly via sudo crontab -e:

0 3 * * 0 /usr/local/bin/buildkit-cache-cleanup.sh >> /var/log/buildkit-cache-cleanup.log 2>&1

Log rotation added so the cleanup log itself doesn't grow unbounded (/etc/logrotate.d/buildkit-cache-cleanup, weekly rotation, 4 copies kept, compressed).

Manually test-ran once before scheduling — confirmed all 3 pool builders reachable, 0B pruned (expected, since their cache was freshly created that same day).


6. Outstanding / Not Yet Done

  • Confirm ticketadmin CI status — verify whether it still creates a standalone buildx builder outside the pool, or has been migrated like ticketcms appears to have been.
  • Disk usage alert (>75%) — still listed as pending from the 2026-05-14 incident's prevention checklist; this recurrence is the second time disk pressure crept up undetected. Worth prioritizing now.
  • Optional: explicit per-builder GC cap — currently relying on buildkit's default GC threshold plus the new weekly cron; could additionally pass a --buildkitd-flags GC config at builder-creation time to hard-cap each pool builder (e.g. ~5-8GB) for a guaranteed ceiling rather than an estimate.
  • Registry GC re-check — the May incident's cleanup policy (7-day cadence, keep 10 tags, 90-day expiry) appears to be holding (/data measured at 42G vs. the ~74G total from the incident's breakdown), but worth a periodic spot-check rather than assuming indefinitely.