feat: migrate Forgejo runner to StatefulSet with Docker-in-Docker

Replaces the Deployment-based runner with a StatefulSet using a proper
Docker-in-Docker sidecar for improved isolation and state management.

Key changes:
- StatefulSet deployment for stable pod identity and persistent storage
- Docker-in-Docker init container (privileged) providing isolated Docker daemon
- Two persistent volumes: runner-data (1Gi) for config, docker-data (20Gi) for image cache
- Uses hcloud-volumes storage class for durability
- Each replica gets its own Docker daemon and image cache

This addresses the workflow failure where job containers (node:24-bookworm)
couldn't access Docker because the CLI was missing. The DinD sidecar provides
a complete Docker environment for all job containers.

Fixes: DEV-335

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
CTO Agent 2026-07-18 16:41:20 +00:00
parent d74a72c130
commit 27397fe859
2 changed files with 74 additions and 27 deletions

View file

@ -50,11 +50,17 @@ kubectl apply -f apps/app-forgejo-runner.yaml
## Runner Configuration ## Runner Configuration
The runner is configured to: The runner is deployed as a StatefulSet with Docker-in-Docker (dind) sidecar for proper isolation and state management.
- Run 2 concurrent jobs
- Use Docker-in-Docker for workflow execution Configuration:
- Support ubuntu-latest and ubuntu-22.04 labels with Node.js 24 - **Deployment type**: StatefulSet (stable pod identity, persistent storage)
- Connect to Forgejo at http://forgejo.forgejo.svc.cluster.local:3000 - **Docker execution**: Docker-in-Docker sidecar (privileged init container)
- **Concurrent jobs**: 2 (configurable via config.yaml)
- **Labels**: ubuntu-latest:docker://node:24-bookworm, ubuntu-22.04:docker://node:24-bookworm
- **Forgejo URL**: https://forgejo.basicstack.de (external URL for proper webhook/API access)
- **Persistent volumes**:
- runner-data (1Gi): Runner registration and config
- docker-data (20Gi): Docker image cache
## Troubleshooting ## Troubleshooting

View file

@ -1,9 +1,10 @@
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: StatefulSet
metadata: metadata:
name: forgejo-runner name: forgejo-runner
namespace: forgejo namespace: forgejo
spec: spec:
serviceName: forgejo-runner
replicas: 1 replicas: 1
selector: selector:
matchLabels: matchLabels:
@ -13,24 +14,55 @@ spec:
labels: labels:
app: forgejo-runner app: forgejo-runner
spec: spec:
restartPolicy: Always
serviceAccountName: forgejo-runner serviceAccountName: forgejo-runner
volumes:
- name: docker-socket
emptyDir: {}
- name: config
configMap:
name: forgejo-runner-config
initContainers:
- name: docker
image: docker:28-dind
securityContext:
privileged: true
volumeMounts:
- name: docker-socket
mountPath: /var/run
- name: docker-data
mountPath: /var/lib/docker
startupProbe:
exec:
command: ["/usr/bin/test", "-S", "/var/run/docker.sock"]
initialDelaySeconds: 5
periodSeconds: 2
failureThreshold: 30
livenessProbe:
exec:
command: ["/usr/bin/test", "-S", "/var/run/docker.sock"]
periodSeconds: 10
restartPolicy: Always
containers: containers:
- name: runner - name: runner
image: code.forgejo.org/forgejo/runner:4.0.1 image: code.forgejo.org/forgejo/runner:4.0.1
imagePullPolicy: IfNotPresent imagePullPolicy: IfNotPresent
securityContext:
runAsUser: 0
runAsGroup: 0
command: command:
- sh - sh
- -c - -c
- | - |
# Wait for Forgejo to be ready (external URL) # Wait for Forgejo to be ready
while ! wget -q -O- https://forgejo.basicstack.de/api/healthz > /dev/null 2>&1; do while ! wget -q -O- https://forgejo.basicstack.de/api/healthz > /dev/null 2>&1; do
echo "Waiting for Forgejo to be ready..." echo "Waiting for Forgejo to be ready..."
sleep 5 sleep 5
done done
# Wait for Docker daemon to be ready
while ! docker version > /dev/null 2>&1; do
echo "Waiting for Docker daemon to be ready..."
sleep 2
done
# Re-register if the stored address is the internal cluster URL # Re-register if the stored address is the internal cluster URL
if [ -f /data/.runner ] && grep -q "svc.cluster.local" /data/.runner; then if [ -f /data/.runner ] && grep -q "svc.cluster.local" /data/.runner; then
echo "Detected internal cluster URL in .runner — forcing re-registration with external URL" echo "Detected internal cluster URL in .runner — forcing re-registration with external URL"
@ -43,7 +75,7 @@ spec:
--no-interactive \ --no-interactive \
--instance https://forgejo.basicstack.de \ --instance https://forgejo.basicstack.de \
--token "$RUNNER_TOKEN" \ --token "$RUNNER_TOKEN" \
--name k3s-runner-1 \ --name "k3s-dind-runner-${HOSTNAME##*-}" \
--labels ubuntu-latest:docker://node:24-bookworm,ubuntu-22.04:docker://node:24-bookworm --labels ubuntu-latest:docker://node:24-bookworm,ubuntu-22.04:docker://node:24-bookworm
fi fi
@ -58,26 +90,35 @@ spec:
- name: DOCKER_HOST - name: DOCKER_HOST
value: unix:///var/run/docker.sock value: unix:///var/run/docker.sock
volumeMounts: volumeMounts:
- name: docker-sock
mountPath: /var/run/docker.sock
- name: runner-data - name: runner-data
mountPath: /data mountPath: /data
- name: docker-socket
mountPath: /var/run
- name: config - name: config
mountPath: /etc/forgejo-runner mountPath: /etc/forgejo-runner
resources: resources:
requests: requests:
cpu: 100m cpu: 200m
memory: 256Mi memory: 512Mi
limits: limits:
cpu: "2" cpu: "4"
memory: 2Gi memory: 4Gi
volumes: volumeClaimTemplates:
- name: docker-sock - metadata:
hostPath: name: runner-data
path: /var/run/docker.sock spec:
type: Socket accessModes:
- name: runner-data - ReadWriteOnce
emptyDir: {} resources:
- name: config requests:
configMap: storage: 1Gi
name: forgejo-runner-config storageClassName: hcloud-volumes
- metadata:
name: docker-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: hcloud-volumes