all systems operational +48 697 610 212
Technical · [2026-02-05]

GitLab CI/CD on Kubernetes: Hard Resource Limits per Project

How we migrated CI/CD from Docker runners to the Kubernetes executor with ResourceQuota and shell-operator. Result: 40–60% faster pipelines.

KUBERNETES · GITLAB · CI-CD · DEVOPS · RKE2

We had 200 vCPU and 320 GB of RAM across five hosts running GitLab Docker runners, and still not enough capacity. One poorly designed pipeline would take over a whole node, and the rest of the jobs waited in the queue or died with OOM.

What fixed it was moving to the Kubernetes executor with an automatic ResourceQuota for every project. On top of that, a shell-operator sets limits separately for each service in a job, because PostgreSQL needs different ones than Redis. Pipelines got 40–60% faster and the runners stopped crashing.

Starting point: Docker runner doesn’t isolate projects

Five hosts with GitLab Docker runners, 200 vCPU and 320 GB of RAM in total, and dozens of projects sharing the same pool. This is what the lack of isolation looked like:

SymptomFrequency
Pipeline saturates 100% CPU on a host8–9 times a week
Runner crash requiring manual intervention2–3 times a week
Jobs slowed down by lack of resourcesdaily

The Docker executor lets you set cgroup limits for a single container, but not for a project as a whole, and jobs land on a host without any check of how much capacity is left. Under heavy load it doesn’t isolate projects the way Kubernetes does with ResourceQuota and LimitRange.

Goal: hard limits per project

The requirements:

  1. Hard CPU and RAM limits at the project or project group level.
  2. Separate pools for teams: frontend 10 vCPU / 10 GiB RAM, backend 20 vCPU / 20 GiB RAM.
  3. Each team decides how to split its budget between the containers in a job.
  4. Namespaces and quotas are created automatically, with no manual work.

Step 1: Kubernetes executor with a namespace per project

We set up an RKE2 cluster and installed GitLab Runner with the Kubernetes executor on it. This is the line in the runner configuration that matters:

[[runners]]
  [runners.kubernetes]
    namespace_overwrite_allowed = "ci-.*"

It lets you override the namespace in each job with the KUBERNETES_NAMESPACE_OVERWRITE variable. In the group or project template we set:

variables:
  KUBERNETES_NAMESPACE_OVERWRITE: "ci-${CI_PROJECT_NAMESPACE}-${CI_PROJECT_NAME}"

Projects then map to namespaces like this:

GitLab projectK8s namespace
backend-projects/app1ci-backend-projects-app1
frontend-projects/app2ci-frontend-projects-app2

Every project gets its own namespace, so it can have its own ResourceQuota.

Step 2: automatic ResourceQuota per group

Quotas were created automatically by nsinjector (today Kyverno would make more sense, the principle is the same). Configuration for the frontend and backend groups:

- name: ci-quota-injector-front
  namespaces:
    - ci-frontend-projects-*
  excludedNamespaces:
    - ci-backend-projects-*
  resources:
    - |
      apiVersion: v1
      kind: ResourceQuota
      metadata:
        name: ci-resourcequota
      spec:
        hard:
          requests.cpu: "10"
          requests.memory: 10Gi
          limits.cpu: "10"
          limits.memory: 10Gi

- name: ci-quota-injector-back
  namespaces:
    - ci-backend-projects-*
  excludedNamespaces:
    - ci-frontend-projects-*
  resources:
    - |
      apiVersion: v1
      kind: ResourceQuota
      metadata:
        name: ci-resourcequota
      spec:
        hard:
          requests.cpu: "20"
          requests.memory: 20Gi
          limits.cpu: "20"
          limits.memory: 20Gi

The backend-projects/app1 project ends up with a limit of 20 CPU / 20 GiB, and frontend-projects/app2 with 10 CPU / 10 GiB. Kubernetes enforces the sum of requests and limits in the namespace and won’t let it be exceeded.

If a specific project needs its own limits, add a separate namespace pattern and a separate ResourceQuota for it.

Limits for individual services in a job

GitLab Runner lets you set limits for services, but the same for all of them:

[runners.kubernetes]
  service_cpu_limit = "500m"
  service_cpu_limit_overwrite_max_allowed = "10000m"
  service_cpu_request = "128m"
  service_cpu_request_overwrite_max_allowed = "10000m"
  service_memory_limit = "512Mi"
  service_memory_limit_overwrite_max_allowed = "10Gi"
  service_memory_request = "128Mi"
  service_memory_request_overwrite_max_allowed = "10Gi"

You can override them per job in .gitlab-ci.yml, but then they still apply to all services equally. That’s not enough when PostgreSQL needs 2 GiB of RAM and Redis needs 256 MiB.

Shell-operator sets limits per service

We used shell-operator from Flant, a tool that runs hooks in response to Kubernetes events. It works like this:

  1. The runner labels Pods with shell-operator: "true".
  2. Shell-operator watches for Pods with that label.
  3. When a Pod is created, the hook reads its spec from BINDING_CONTEXT_PATH and finds the service containers.
  4. The hook builds a patch from the variables in .gitlab-ci.yml or from default values and runs kubectl patch pod.

Hook

#!/usr/bin/env bash

if [[ $1 == "--config" ]] ; then
  cat <<EOF
configVersion: v1
kubernetes:
- apiVersion: v1
  kind: Pod
  executeHookOnEvent:
  - Added
  labelSelector:
    matchLabels:
      shell-operator: "true"
  allowFailure: true
EOF
else
  type=$(jq -r '.[0].type' $BINDING_CONTEXT_PATH)
  if [[ $type == "Event" ]] ; then
    echo $BINDING_CONTEXT_PATH
    # Default limits per service type
    source ./limits.conf

    # Pod name and namespace
    podName=$(jq -r '.[0].object.metadata.name' $BINDING_CONTEXT_PATH)
    podNamespace=$(jq -r '.[0].object.metadata.namespace' $BINDING_CONTEXT_PATH)

    # All containers in the Pod
    podContainers=$(jq -r '.[0].object.spec.containers[].name' $BINDING_CONTEXT_PATH)

    # List of containers that are services (names containing "svc")
    svc_containers=$(echo $podContainers | awk -F" " '{for(i=1;i<=NF;i++){if ($i ~ /svc/){print $i}}}')

    if [[ ! -z $svc_containers ]] ; then
      for i in $svc_containers; do
        # Service container image
        SVC_IMAGE_NAME=$(jq --arg svc_container_name "$i" -r '.[0].object.spec.containers[] | select(.name==$svc_container_name).image' $BINDING_CONTEXT_PATH)

        # Try to read values from env set in .gitlab-ci
        CPU_LIMIT=$(jq --arg svc_container_name "$i" -r '.[0].object.spec.containers[] | select(.name==$svc_container_name).env | .[] | select(.name=="KUBERNETES_SVC_CPU_LIMIT").value' $BINDING_CONTEXT_PATH)

        # If no env in job, default values from limits.conf
        if [[ -z $CPU_LIMIT ]] ; then
          case "$SVC_IMAGE_NAME" in
            *postgresql*)
              CPU_LIMIT=$KUBERNETES_PSQL_CPU_LIMIT
              CPU_REQUEST=$KUBERNETES_PSQL_CPU_REQUEST
              MEM_LIMIT=$KUBERNETES_PSQL_MEMORY_LIMIT
              MEM_REQUEST=$KUBERNETES_PSQL_MEMORY_REQUEST
            ;;
            # Here you can add redis, elasticsearch, mysql etc.
          esac
        elif [[ ! -z $CPU_LIMIT ]] ; then
          CPU_REQUEST=$(jq --arg svc_container_name "$i" -r '.[0].object.spec.containers[] | select(.name==$svc_container_name).env | .[] | select(.name=="KUBERNETES_SVC_CPU_REQUEST").value' $BINDING_CONTEXT_PATH)
          MEM_LIMIT=$(jq --arg svc_container_name "$i" -r '.[0].object.spec.containers[] | select(.name==$svc_container_name).env | .[] | select(.name=="KUBERNETES_SVC_MEM_LIMIT").value' $BINDING_CONTEXT_PATH)
          MEM_REQUEST=$(jq --arg svc_container_name "$i" -r '.[0].object.spec.containers[] | select(.name==$svc_container_name).env | .[] | select(.name=="KUBERNETES_SVC_MEM_REQUEST").value' $BINDING_CONTEXT_PATH)
        else
          echo "Nothing to do! Exiting.."
          exit 0;
        fi

        # Substitute values into patch template
        sed -e "s/SVC_CONTAINER_NAME/$i/" \
            -e "s/CPU_LIMIT/$CPU_LIMIT/" \
            -e "s/CPU_REQUEST/$CPU_REQUEST/" \
            -e "s/MEM_LIMIT/$MEM_LIMIT/" \
            -e "s/MEM_REQUEST/$MEM_REQUEST/" svc-env.yaml > $i.yaml

        # Patch container resources in the Pod
        kubectl patch pod $podName -n $podNamespace --patch-file $i.yaml
      done
    else
      echo "Nothing to do! Exiting.."
      exit 0;
    fi
  fi
fi

Template svc-env.yaml

spec:
  containers:
    - name: "SVC_CONTAINER_NAME"
      resources:
        limits:
          cpu: "CPU_LIMIT"
          memory: "MEM_LIMIT"
        requests:
          cpu: "CPU_REQUEST"
          memory: "MEM_REQUEST"

Usage in .gitlab-ci.yml

services:
  - name: postgres:15
    alias: psql-svc
    variables:
      KUBERNETES_SVC_CPU_LIMIT: "1000m"
      KUBERNETES_SVC_CPU_REQUEST: "500m"
      KUBERNETES_SVC_MEM_LIMIT: "2Gi"
      KUBERNETES_SVC_MEM_REQUEST: "1Gi"

If the job doesn’t set these variables, the hook uses the defaults from limits.conf.

Results

MetricBeforeAfter
Average pipeline time100% (baseline)40–60% faster
Runner crashes per month~120
Manual interventions per week2–30
Time predictabilityrandomstable

Every project has a guaranteed resource pool and doesn’t compete with the others. One pipeline can no longer take over a whole node, and teams know how much they have and how to split it. A job that exceeds its CPU limit slows down (throttling), and a job that exceeds its memory limit is OOMKilled in its own pod. The runner and the other jobs keep running.

There is a cost. Pipelines that used to take over a whole node now run longer. Their duration is predictable, though, and they don’t slow anyone else down.

When this makes sense

This setup fits when many teams or projects share one CI/CD infrastructure and one project slows down the others. It also helps when you need auditable limits per team or when your pipelines run heavy services such as databases, search engines or caches.

You don’t need it if you have few projects and plenty of resources, all your pipelines are similar and lightweight, or nobody on your team knows Kubernetes.

Are your pipelines fighting over shared runners on a first come, first served basis? Tell us about your setup and we’ll talk about how to put hard limits in place for you.

Want the same in your setup?

Get in touch. The first consultation is free.

Free consultation →