The CI surface lives on Buildkite — the hosted control plane is excellent and the team likes the pipeline model — but every job currently runs on Buildkite-hosted infrastructure, which means production credentials, registry tokens, and customer database snapshots stream through a runner you don’t own. Compliance now wants build execution inside your VPC. The good news: the Buildkite agent is open source, runs anywhere, and registers back to the hosted control plane over an outbound HTTPS connection. The bad news: the official tarball install lands a single agent on a single VM running as root, with no isolation between jobs, no queue tagging, no Docker-in-Docker sandboxing, and no rotation of the agent token. A serious self-hosted Buildkite agent pool is a different deployment.
What’s happening
A production Buildkite agent pool runs the agent as a non-root user (buildkite-agent), each job in a fresh Docker container via the docker-compose plugin or job hook, with the host’s Docker socket bind-mounted read-only-where-possible and per-job working directories deleted after the run. The agent registers with a cluster + queue token from your Buildkite org — never the legacy org-wide agent token — so revoking one cluster doesn’t revoke everything. Agents must be tagged (queue=default, queue=docker, os=linux, arch=amd64) so pipelines steer to the right pool. The agent token sits in /etc/buildkite-agent/buildkite-agent.cfg with mode 600, owned by buildkite-agent — never world-readable. SSH keys for git clone are per-agent and pulled from your secrets manager at boot, not baked into the AMI. Outbound traffic is controlled — the agent only needs agent.buildkite.com:443 and your private registry; block egress to public Docker Hub unless explicitly required, and route image pulls through a registry mirror you control. Job-level concurrency is the most common foot-gun: --spawn=4 runs four jobs per agent VM, sharing one Docker daemon, sharing one filesystem — set it to 1 unless you’ve verified job isolation works.
How to verify
Agent host should expose only :22:
ss -lntp | grep -E ':(22|80|443)\b'
# 0.0.0.0:22 sshd
# (no ingress for the agent — it dials out)
The agent should be in connected state in the Buildkite UI and journalctl should show a clean register:
journalctl -u buildkite-agent -n 50 | grep -iE 'connected|registered|queue'
# Connected to Buildkite Agent API
# Tags: queue=default, os=linux, arch=amd64, cluster=production
The agent must run as buildkite-agent, not root:
systemctl cat buildkite-agent | grep -E '^User=|^Group='
# User=buildkite-agent
# Group=buildkite-agent
ps -ef | grep buildkite-agent | grep -v grep | awk '{print $1}'
# buildkite-agent
A trial job must be able to call docker run:
sudo -u buildkite-agent docker run --rm alpine:3.20 echo ok
# ok
If the agent is registered but jobs never dispatch, the queue tag on the pipeline step doesn’t match the agent’s queue= tag — fix one or the other.
The fix
Save the script as /root/setup-buildkite-agent-alma9.sh on each agent host. It installs the Buildkite agent on a fresh AlmaLinux 9 VM, registers it under a cluster + queue token, and pre-stages Docker for sandboxed jobs.
#!/usr/bin/env bash
set -euo pipefail
### AlmaLinux 9 Buildkite agent pool member
### Open-source agent only; control plane stays at agent.buildkite.com
### Each job sandboxed via Docker (recommend the docker-compose plugin in pipelines)
### Agent runs as buildkite-agent (non-root); spawns one job at a time
### Agent token from /root/.buildkite-token (cluster-scoped, never legacy org token)
### Outbound: agent.buildkite.com:443, your registry, your secrets manager
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
: "${BUILDKITE_QUEUE:?Set BUILDKITE_QUEUE=default}"
: "${BUILDKITE_CLUSTER_NAME:?Set BUILDKITE_CLUSTER_NAME=production}"
BUILDKITE_AGENT_VERSION="${BUILDKITE_AGENT_VERSION:-3.84.0}"
BUILDKITE_SPAWN="${BUILDKITE_SPAWN:-1}"
BUILDKITE_TOKEN_FILE="${BUILDKITE_TOKEN_FILE:-/root/.buildkite-token}"
if [[ ! -s "${BUILDKITE_TOKEN_FILE}" ]]; then
echo "Place the cluster-scoped agent token at ${BUILDKITE_TOKEN_FILE} before running."
echo "Buildkite UI: Agents -> Clusters -> ${BUILDKITE_CLUSTER_NAME} -> Tokens -> New."
exit 1
fi
BUILDKITE_AGENT_TOKEN="$(<"${BUILDKITE_TOKEN_FILE}")"
echo
echo "========================================"
echo "Buildkite agent AlmaLinux 9 Setup"
echo "Queue: ${BUILDKITE_QUEUE} Cluster: ${BUILDKITE_CLUSTER_NAME}"
echo "========================================"
echo
echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y install vim curl wget chrony fail2ban dnf-automatic firewalld \
policycoreutils-python-utils lsof tcpdump bind-utils tar gzip \
iproute net-tools openssl jq git
systemctl enable --now chronyd
echo "==> SELinux enforcing"
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
setenforce 1 || true
cat > /etc/sysctl.d/99-buildkite.conf <<'SYSCTL'
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
kernel.dmesg_restrict = 1
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 8192
SYSCTL
sysctl --system
echo "==> Firewalld — agent dials out only"
systemctl enable --now firewalld
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload
echo "==> Hardening SSH"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart sshd
echo "==> fail2ban + automatic security updates"
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban
cat > /etc/dnf/automatic.conf <<'DNFAUTO'
[commands]
upgrade_type = security
random_sleep = 900
download_updates = yes
apply_updates = yes
[emitters]
emit_via = stdio
DNFAUTO
systemctl enable --now dnf-automatic.timer
echo "==> Installing Docker CE"
dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker
echo "==> Adding Buildkite repo"
curl -fsSL https://keys.openpgp.org/vks/v1/by-fingerprint/32A37959C2FA5C3C99EFBC32A79206696452D198 \
| gpg --dearmor -o /etc/pki/rpm-gpg/RPM-GPG-KEY-buildkite
cat > /etc/yum.repos.d/buildkite-agent.repo <<'REPO'
[buildkite-agent]
name=Buildkite agent
baseurl=https://yum.buildkite.com/buildkite-agent/stable/x86_64/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-buildkite
REPO
dnf -y install buildkite-agent-${BUILDKITE_AGENT_VERSION}-1.x86_64 || dnf -y install buildkite-agent
echo "==> Configuring buildkite-agent.cfg"
HOSTNAME_SHORT="$(hostname -s)"
cat > /etc/buildkite-agent/buildkite-agent.cfg <<CFG
token="${BUILDKITE_AGENT_TOKEN}"
name="${HOSTNAME_SHORT}-%spawn"
tags="queue=${BUILDKITE_QUEUE},os=linux,arch=amd64,cluster=${BUILDKITE_CLUSTER_NAME}"
tags-from-host=true
build-path="/var/lib/buildkite-agent/builds"
hooks-path="/etc/buildkite-agent/hooks"
plugins-path="/etc/buildkite-agent/plugins"
spawn=${BUILDKITE_SPAWN}
spawn-with-priority=true
git-clean-flags="-ffxdq"
git-mirrors-path="/var/lib/buildkite-agent/git-mirrors"
shell="/bin/bash -e -c"
no-pty=false
disconnect-after-job=false
disconnect-after-idle-timeout=0
cancel-grace-period=120
job-log-path="/var/log/buildkite-agent"
verbose=false
debug=false
no-color=false
experiment=""
priority=5
CFG
chown buildkite-agent:buildkite-agent /etc/buildkite-agent/buildkite-agent.cfg
chmod 600 /etc/buildkite-agent/buildkite-agent.cfg
usermod -aG docker buildkite-agent
mkdir -p /var/lib/buildkite-agent/builds /var/lib/buildkite-agent/git-mirrors /var/log/buildkite-agent
chown -R buildkite-agent:buildkite-agent /var/lib/buildkite-agent /var/log/buildkite-agent
# Pre-job hook: lock down the env; clean the workspace
mkdir -p /etc/buildkite-agent/hooks
cat > /etc/buildkite-agent/hooks/environment <<'HOOK'
#!/bin/bash
set -euo pipefail
# Hard caps — pipelines that want more must request explicitly
export BUILDKITE_TIMEOUT="${BUILDKITE_TIMEOUT:-60}"
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Prefer the local registry mirror if reachable
if curl -fs --max-time 2 http://registry-mirror.internal/v2/ > /dev/null; then
export DOCKER_REGISTRY_MIRROR=http://registry-mirror.internal
fi
# Mask any credential that leaked into env
unset AWS_SECRET_ACCESS_KEY GCP_SERVICE_ACCOUNT BUILDKITE_AGENT_TOKEN || true
HOOK
cat > /etc/buildkite-agent/hooks/pre-exit <<'HOOK'
#!/bin/bash
# Always clean up child Docker resources from this job
docker container prune -f --filter "label=com.buildkite.job-id" || true
docker volume prune -f || true
HOOK
chmod +x /etc/buildkite-agent/hooks/environment /etc/buildkite-agent/hooks/pre-exit
chown -R buildkite-agent:buildkite-agent /etc/buildkite-agent/hooks
# systemd: one unit, one agent process, spawn parameter inside .cfg
cat > /etc/systemd/system/buildkite-agent.service.d/override.conf <<'OVR'
[Service]
LimitNOFILE=200000
TimeoutStopSec=180
KillMode=process
OVR
mkdir -p /etc/systemd/system/buildkite-agent.service.d
cat > /etc/systemd/system/buildkite-agent.service.d/override.conf <<'OVR'
[Service]
LimitNOFILE=200000
TimeoutStopSec=180
KillMode=process
OVR
systemctl daemon-reload
systemctl enable --now buildkite-agent
echo "==> Waiting for agent to register"
for i in {1..60}; do
if journalctl -u buildkite-agent --no-pager -n 100 2>/dev/null | grep -qi 'connected\|registered'; then
break
fi
sleep 2
done
echo
echo "========================================"
echo "DONE — agent"
echo "========================================"
echo
echo "Service status:"
systemctl is-active buildkite-agent
echo
echo "Identity:"
ps -ef | grep -E '[/]buildkite-agent' | awk '{print $1, $NF}' | head -3
echo
echo "Tags:"
grep '^tags=' /etc/buildkite-agent/buildkite-agent.cfg
echo
echo "Registration logs:"
journalctl -u buildkite-agent --no-pager -n 20 | tail -20
echo
echo "Confirm in Buildkite UI: Agents -> ${BUILDKITE_CLUSTER_NAME} -> Agents — ${HOSTNAME_SHORT} should be online."
echo
echo "Token file at ${BUILDKITE_TOKEN_FILE} can now be moved to your secrets manager and removed from this host:"
echo " shred -u ${BUILDKITE_TOKEN_FILE}"
Make it executable, drop the cluster-scoped agent token in /root/.buildkite-token, and run:
chmod 700 /root/setup-buildkite-agent-alma9.sh
# Create the cluster-scoped token in the Buildkite UI:
# Agents -> Clusters -> production -> Tokens -> New token (with description "alma9 pool")
# Paste the token here:
install -m 600 /dev/stdin /root/.buildkite-token <<< "your-cluster-scoped-token-here"
BUILDKITE_QUEUE=default \
BUILDKITE_CLUSTER_NAME=production \
BUILDKITE_SPAWN=1 \
/root/setup-buildkite-agent-alma9.sh
# After register, remove the token from disk (it's now in the agent config):
shred -u /root/.buildkite-token
Once the agent appears in the Buildkite UI, push a pipeline step that targets queue: default and runs docker run --rm alpine:3.20 echo ok. The build should land on this agent within seconds. If you scale the pool by spinning up additional VMs, each registers under the same cluster token and shows up tagged with its hostname — Buildkite’s hosted dispatcher handles queue routing. For autoscaling, run a queue-poller that watches the Builds Waiting metric on the cluster API and runs your cloud provider’s create-instance command; the agent installer above is idempotent enough to bake into a golden image or run from cloud-init. Move /etc/buildkite-agent/buildkite-agent.cfg to your image build pipeline so new VMs come up pre-registered; never commit the token to git.
For an agent pool you don’t have to operate, scale, or patch yourself, our deployment management team runs queue-aware Buildkite pools on managed cloud compute with autoscaling, secrets rotation, and Sev-1 escalation in place — start a scoping call.