02 - The GitLab Runner
The goal here is a runner registered to a project, running on the kcp host, with host set to the tenant apiserver's loopback URL. Finish 01 first so you have a tenant cluster, its apiserver URL, the credential files in /etc/gitlab-runner/tenant-ci/, and the group name gitlab-runner.
If your cluster comes from k8s.socialweb.computer, you can skip those prerequisites. It uses a non-expiring kubeconfig and a shorter config (see gateway.md and 04).
Contents
- GitLab Setup
- Runner Installation
- Registering the Runner
- Tenant Cluster Configuration
- k8s Gateway Configuration
- Configuration Pitfalls
- Starting the Runner
- Credential Verification
- Failure Modes
GitLab Setup
If you already have a GitLab instance, skip ahead to installing and registering the runner. Otherwise, start one on the kcp host:
services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
hostname: 'gitlab'
shm_size: '256m'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab:8080'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
nginx['listen_port'] = 80
nginx['listen_https'] = false
ports:
- '8080:80'
- '2222:22'
volumes:
- /srv/gitlab/config:/etc/gitlab
- /srv/gitlab/logs:/var/log/gitlab
- /srv/gitlab/data:/var/opt/gitlab
Keep two things in mind with this file:
- Use a short
hostnamerather than an FQDN. A long container hostname breaks Docker's internal DNS, and it fails with an unreachable OIDC provider error instead of a DNS error. GITLAB_OMNIBUS_CONFIGonly applies on first boot, so changing it later has no effect. Edit/srv/gitlab/config/gitlab.rband rundocker exec gitlab gitlab-ctl reconfigureinstead. Updating the environment variable later is the easiest way to break a working GitLab instance with a change that looks valid.
Wait for GitLab to come up instead of using a fixed sleep:
until curl -fsS http://127.0.0.1:8080/-/health >/dev/null; do sleep 5; done
Runner Installation
Install the runner on the same host:
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash
sudo dnf install -y gitlab-runner
Run it on the host instead of inside the cluster because the apiserver binds to loopback.
Registering the Runner
Create the runner under the project's Settings > CI/CD > Runners and copy the authentication token (the legacy registration-token flow is deprecated):
gitlab-runner register \
--non-interactive \
--config /etc/gitlab-runner/config.toml \
--url https://gitlab.example.com/ \
--token <runner authentication token> \
--executor kubernetes
register writes a config with the token and nothing else useful. Replace the rest with the file below.
Tenant Cluster Configuration
Fill in the <<...>> placeholders. The table below explains the other settings.
concurrent = 8
check_interval = 3
[[runners]]
name = "socialweb-tenant-ci"
url = "https://gitlab.example.com/"
token = "<<runner authentication token>>"
executor = "kubernetes"
# Runs AFTER the clone, before the job's script. A STRING, not an array.
pre_build_script = "true"
# Kilobytes. Default is 4096, which is 4 MiB and is NOT enough for a Go test log.
output_limit = 51200
[runners.kubernetes]
host = "<<https://127.0.0.1:LEASED_PORT>>"
cert_file = "/etc/gitlab-runner/tenant-ci/tls.crt"
key_file = "/etc/gitlab-runner/tenant-ci/tls.key"
ca_file = "/etc/gitlab-runner/tenant-ci/ca.crt"
namespace = "gitlab-runner"
image_pull_policy = "if-not-present"
# Requests are what the scheduler places against. Set them EXPLICITLY.
cpu_request = "100m"
memory_request = "128Mi"
cpu_limit = "10"
memory_limit = "20Gi"
# Helper and init containers too, or their limits become their requests.
helper_cpu_request = "100m"
helper_memory_request = "128Mi"
helper_cpu_limit = "1"
helper_memory_limit = "1Gi"
poll_timeout = 1800
cleanup_grace_period_seconds = 30
[runners.kubernetes.affinity]
[runners.kubernetes.affinity.pod_anti_affinity]
[[runners.kubernetes.affinity.pod_anti_affinity.required_during_scheduling_ignored_during_execution]]
topology_key = "kubernetes.io/hostname"
[runners.kubernetes.affinity.pod_anti_affinity.required_during_scheduling_ignored_during_execution.label_selector]
[[runners.kubernetes.affinity.pod_anti_affinity.required_during_scheduling_ignored_during_execution.label_selector.match_expressions]]
key = "project.runner.gitlab.com/id"
operator = "Exists"
| Key | Why |
|---|---|
concurrent | Jobs in flight at once (one per node with anti-affinity on). Setting this to 1 wedges the runner if a stale job from a cancelled pipeline holds the only slot. |
host | The apiserver's loopback URL from 01. Do not use the kcp mount URL because it's a reverse proxy and doesn't support pods/exec. |
cert_file / key_file / ca_file | Tenant client certificate credentials instead of a bearer token. |
namespace | Target namespace for job Pods. Must already exist. |
image_pull_policy | if-not-present. Node images come pre-seeded with some images, and always-pull throws that away. |
cpu_request / memory_request | Used by the scheduler for placement. If these are too large, the Pod never schedules. |
cpu_limit / memory_limit | Enforcement only. The scheduler ignores limits, so a limit larger than node size is fine. |
helper_*_request | Always set these explicitly. See Configuration Pitfalls below. |
poll_timeout | Seconds to wait for the Pod to reach Running, including node boot and large image pulls. |
output_limit | Trace size in kilobytes. Default is 4096 (4 MiB). |
| anti-affinity | Keeps jobs at one per node. See 03. |
k8s Gateway Configuration
The config above is for the tenant-cluster setup from 01. A cluster from k8s.socialweb.computer needs no tenant credentials. It uses a non-expiring kubeconfig instead, so omit host, cert_file, key_file, and ca_file and pass KUBECONFIG in the runner's environment (gateway.md, 04).
Here is the complete config for that setup:
concurrent = 2
check_interval = 3
[[runners]]
name = "k8s-gateway"
url = "<<https://gitlab.example.com/>>"
token = "<<runner authentication token>>"
executor = "kubernetes"
run_untagged = false
tag_list = ["k8s-gateway"]
# Runs AFTER the clone, before the job's script. A STRING, not an array.
pre_build_script = "true"
# Kilobytes. Default is 4096, which is 4 MiB and is NOT enough for a Go test log.
output_limit = 51200
[runners.kubernetes]
# No host, no cert_file, no key_file, no ca_file: the kubeconfig carries the connection.
namespace = "gitlab-runner"
image_pull_policy = "if-not-present"
service_account = "gitlab-runner"
# The job pod's requests. They must fit ONE rented node: the cluster's vCPUs and memory.
cpu_request = "3"
memory_request = "6Gi"
cpu_limit = "4"
memory_limit = "6Gi"
# Each service and the helper get their own requests; they add into the pod's. A limit with no request IS the request.
service_cpu_request = "200m"
service_memory_request = "256Mi"
service_cpu_limit = "1"
service_memory_limit = "256Mi"
helper_cpu_request = "100m"
helper_memory_request = "128Mi"
helper_cpu_limit = "1"
helper_memory_limit = "128Mi"
# Seconds the runner waits to connect to the build container: node boot, join and image pull included.
poll_timeout = 3600
cleanup_grace_period_seconds = 30
[runners.kubernetes.node_selector]
"kubernetes.io/os" = "linux"
[runners.kubernetes.affinity]
[runners.kubernetes.affinity.pod_anti_affinity]
[[runners.kubernetes.affinity.pod_anti_affinity.required_during_scheduling_ignored_during_execution]]
topology_key = "kubernetes.io/hostname"
[runners.kubernetes.affinity.pod_anti_affinity.required_during_scheduling_ignored_during_execution.label_selector]
[[runners.kubernetes.affinity.pod_anti_affinity.required_during_scheduling_ignored_during_execution.label_selector.match_expressions]]
key = "project.runner.gitlab.com/id"
operator = "Exists"
Start the runner with the kubeconfig in its environment:
KUBECONFIG=$PWD/my-cluster.kubeconfig.yaml gitlab-runner run --config ./config.toml --working-directory "$PWD"
| Key | Value here | Why |
|---|---|---|
concurrent | 2 | Jobs in flight at once. Each concurrent job that can't share a node rents a node sized for its Pod (at least the cluster's base size), up to the cluster's max-node ceiling (set at creation or runtime, defaulting to 10 on the gateway). A [[runners]] limit can cap it lower. |
check_interval | 3 | Seconds between polls to GitLab for work. |
run_untagged / tag_list | false / ["k8s-gateway"] | Untagged jobs can match any untagged runner, including one that can't reach this cluster. Tag the runner and give its projects the same tag. |
pre_build_script | "true" | Must be a string, not an array. Runs after the clone and before the job's script, which is useful for tool guards. true is a no-op. |
output_limit | 51200 | Kilobytes of job trace. The runner's default of 4096 (4 MiB) is too small for a Go test log. |
host, cert_file, key_file, ca_file | not set | Leaving host empty tells the executor to use the kubeconfig. Any non-empty host makes it ignore the kubeconfig and use the out-of-cluster certificate path instead. |
namespace | gitlab-runner | Namespace where job Pods run. Create it first: KUBECONFIG=./my-cluster.kubeconfig.yaml kubectl create namespace gitlab-runner. |
image_pull_policy | if-not-present | Node images come with some pre-seeded images, and always-pull discards them. |
service_account | gitlab-runner | Service account for job Pods. Create it in the namespace first: kubectl -n gitlab-runner create serviceaccount gitlab-runner. Jobs that deploy need RBAC granted to it (03). Jobs that only run commands work with the default. |
cpu_request / memory_request | 3 / 6Gi | Build container requests used for scheduling. Together with helper, service, and node system pod requests, they must fit on a single rented node (sized for the job, and at least the cluster's base size). If a request fits no node, the Pod stays pending while the cluster keeps retrying. |
cpu_limit / memory_limit | 4 / 6Gi | Container enforcement limits only. The scheduler ignores them. A CPU limit matching node size is fine because it only throttles. Memory limits aren't placement hints either, but combined memory limits across all containers have to stay within what the node can provide or the OOM killer will terminate containers. Setting the memory limit equal to the request avoids that. |
service_*_request / service_*_limit | see file | Each GitLab services: container has its own requests, which add to the Pod's total request. A limit with no request becomes the request, so set both. |
helper_*_request / helper_*_limit | see file | Clone/artifact helper container resources. An unset request defaults to the limit here too. Init containers count as their maximum, not their sum (the runner's init-permissions container may also use helper resources). |
node_selector | kubernetes.io/os = linux | Restricts Pods to Linux nodes. Every rented node runs Linux. A selector that matches nothing leaves Pods pending with no error. |
node_tolerations | not set | Rented nodes have no taints once ready. The CCM removes the kubelet's registration taint after initialization, and job Pods schedule after that. Only add tolerations for taints you apply yourself. |
pod_labels / pod_annotations | not set | Add these only when your own policy needs them. The anti-affinity rule below needs no custom label because it matches project.runner.gitlab.com/id, which is already on every job Pod. |
poll_timeout | 3600 | Seconds the runner waits to connect to the build container (default 180). The first job has to wait for the market to win an RFP, boot a node, join the cluster, and pull the image. |
cleanup_grace_period_seconds | 30 | How long a finished job's Pod may take to terminate (ignored when terminationGracePeriodSeconds is set). |
affinity | required pod anti-affinity | Forces one job per node so a second concurrent job rents a second node instead of sharing the first. Setting topology_key = kubernetes.io/hostname makes each hostname a domain, and the selector matches every job Pod in the namespace via project.runner.gitlab.com/id (the project's ID). This also keeps jobs from other projects in the same namespace off the same node. To scope it to one runner config instead, select manager.runner.gitlab.com/name. Without anti-affinity, multiple jobs land on one node, concurrency stops renting nodes, and memory pressure kills jobs. |
[runners.kubernetes.autoscaler] | not set | Pause pods. See pause pods. |
Node Sizing and Pod Requests
A cluster's cpus and mem set the minimum size for its nodes, which is also the size rented when no Pod request asks for more. A node claimed for pending Pods is sized from those Pods' effective requests plus a system reserve of 500m CPU and 1.5GiB (for per-node system pods, the 100Mi eviction reserve, and base OS and kubelet overhead), rounded up to whole vCPUs and GiB and never below the floor. The podsPerNode count and max-node ceiling stay the same. Re-running the create call with an existing cluster name updates the floor and ceiling for nodes claimed afterward, not for running nodes.
In the web form, set the floor with the vCPUs and memory fields. From the CLI, pass cpus (an integer) and mem (a string) to the create call:
xrpc procedure createCluster name="$CL" cpus:=4 mem="8G"
Leaving a field empty keeps the default of 2 vCPUs and 4 GiB (gateway.md). Any bidder that can't meet the requested size declines, and the cluster keeps trying.
One GitLab job runs in one Pod on one node, and placement depends on requests, not limits. A Pod's effective request is the build container's cpu_request and memory_request, plus each service container's service_*_request and each helper container's helper_*_request (init containers count as their maximum, not their sum). This total has to fit on the node alongside system pod requests: node-local-dns (25m/32Mi) on every node, and about 225m/172Mi in total on the single node that also hosts the single-replica coredns and metrics-server.
The kubelet sets no kube-reserved or system-reserved. Allocatable CPU is the whole node, and allocatable memory is capacity minus the default 100Mi eviction reserve. Nothing is reserved for kubelet, the container runtime, or the OS, and the scheduler ignores their actual usage. Leave room in your requests, because if your requests leave no headroom, the node fails under its own system load instead of triggering a larger node.
The concurrent setting limits how many job Pods can exist at once. With pod anti-affinity enabled, each job that can't share a node rents another node sized for its Pod (at least the cluster's base size), up to the cluster's max-node ceiling. That ceiling is set at creation or runtime (the gateway default is 10), and a [[runners]] limit can cap it lower.
For a 4 vCPU / 8G cluster, the config values above break down like this:
| Component | CPU | Memory |
|---|---|---|
| Job (build container) | 3 CPU | 6GiB |
| Services | 200m | 256Mi |
| Helper | 100m | 128Mi |
| Pod total | about 3.3 CPU | about 6.4GiB |
| With system pods (node hosting coredns and metrics-server) | about 3.5 CPU | about 6.6GiB |
| Remaining for base system | roughly 0.5 CPU | roughly 1.3GiB |
For a 2 vCPU / 4G cluster, lower cpu_request to 1 and memory_request to 2Gi. These numbers come from manifest requests and kernel accounting, not from measuring a live node. Inspect a real node with kubectl describe node once a job has run and adjust as needed.
Configuration Pitfalls
pre_build_script Type and Timing
pre_build_script is a string, not an array. Passing an array fails at startup, and the error only shows up in the runner's own output:
FATAL: Service run failed error=decoding configuration file: toml: line 9
(last key "runners.pre_build_script"): incompatible types: TOML value has type []any;
destination has type string
The runner exits and stops taking jobs, while GitLab still shows it as online, so people end up waiting on a job that never starts. Alert on FATAL in the runner's logs instead of relying on GitLab's status view.
Also keep the hook order straight: pre_build_script runs after the clone, and pre_get_sources_script runs before it. Put clone guards in pre_get_sources_script and anything operating on the checked-out tree in pre_build_script.
Limits Without Requests
Kubernetes defaults an unset container request to its limit. Setting helper_cpu_limit = 1 alone makes every job Pod request about 2.1 CPU (the build container's request plus the helper and init containers, each promoted from its limit). A two-vCPU node replies:
Unschedulable: "0/1 nodes are available: 1 Insufficient cpu."
This looks like a slow cluster, when the Pod actually can't fit on the node.
Log Size Limits
The 4 MiB log cap belongs to the runner, not GitLab:
Job's log exceeded limit of 4194304 bytes. Job execution will continue but no more output will be collected.
That message comes from the gitlab-runner binary. GitLab's own ceiling is ci_jobs_trace_size_limit, a plan limit (project.actual_limits) that isn't in /application/settings. Check both before assuming which limit you hit.
Per-Image Tool Availability
Tools depend on the job image. A pre_build_script that assumes corepack, pnpm, or npm fails with exit code 127 on images without them. Check that the tool exists first, and end with true so the guard never fails the job it protects:
pre_build_script = "if ! command -v pnpm >/dev/null 2>&1 && command -v npm >/dev/null 2>&1; then npm install -g pnpm@10.11.0; fi; true"
Starting the Runner
Run the runner directly with:
gitlab-runner run --config /etc/gitlab-runner/config.toml --working-directory /var/lib/gitlab-runner
Or run it as a service under a user that can read /etc/gitlab-runner/tenant-ci/.
Credential Verification
Check the client certificate subject and RBAC permissions before troubleshooting the runner:
openssl x509 -in /etc/gitlab-runner/tenant-ci/tls.crt -noout -subject
kubectl --kubeconfig <tenant-admin> auth can-i create pods \
--namespace gitlab-runner --as=gitlab-runner --as-group=gitlab-runner
Failure Modes
These are ordered roughly by when you'll run into them:
| Symptom | Cause |
|---|---|
| Runner exits at startup | config.toml failed to decode. Check the runner's output for the offending key. |
Job stays pending, no node appears | The runner reached a different apiserver, or the Pod landed in a namespace the observer doesn't watch. Compare kubectl get pods -A against the cluster's own port. |
x509: certificate signed by unknown authority | Wrong ca_file, or a host port belonging to another cluster (ports are leased per cluster and never shared). |
Unauthorized at job start | The certificate belongs to another cluster's ClientCA, or its Organizations don't match the RBAC group. |
Forbidden creating pods | The ClusterRoleBinding's subject doesn't match the certificate's Organizations. |
| Job hangs at "Preparing the environment" | pods/exec is unreachable. This happens when the runner points at the kcp mount URL instead of loopback. |
| Job passes, then a retry fails | The credential expired. Check its VALID_FOR. |
Insufficient cpu, no node appears | Pod requests don't fit a node. See Configuration Pitfalls above. |
Next: 03 - Jobs.