Kubernetes Cost Optimization with GitLab CI: Where the Budget Leaks
Five changes that cut your Kubernetes CI/CD bill: right-sizing resources, autoscaling runners to zero, spot nodes, caching and cost per project.
KUBERNETES · GITLAB · CI-CD · FINOPS · DEVOPS
Pipelines run, the cluster scales, deployments go out on their own. Then the cloud invoice arrives and someone asks why CI alone costs as much as half of production.
Most of that bill comes from CI that nobody measures: runners standing by 24/7, jobs asking for 8 GB of RAM “just in case”, two-gigabyte images and builds with no cache. We optimize Kubernetes costs in GitLab CI with five changes, and none of them slows the team down.
Where the budget leaks
In the CI/CD environments we’ve taken over, the cost almost always broke down the same way:
| Cost source | Typical symptom |
|---|---|
| Oversized requests | A job reserves 8 vCPU, uses 1.5, and the rest of the node sits idle |
| 24/7 runners | A fixed pool of machines costs money on nights and weekends when nobody commits |
| No cache | Every build pulls dependencies from scratch, and you pay for those minutes |
| Large images | Pulling the image takes longer than the build itself, across hundreds of jobs |
A typical case: 200 vCPU on a fixed pool of hosts, of which a few percent actually work at night. You pay for the peak and use the average.
1. Right-size resources
Most waste comes from requests set by eye, with a margin. Kubernetes reserves whatever the job asks for, regardless of what it actually uses. An oversized request is capacity you pay for and nobody uses.
In practice we measure real job usage (Prometheus, metrics-server) instead of guessing. We set the request near median usage and the limit with a sensible margin. We enforce it per project with ResourceQuota and LimitRange, which we covered in the post on hard resource limits per project in GitLab CI.
This is usually the fastest saving. The infrastructure stays as it is, and you free up the capacity that over-provisioned reservations had been blocking.
2. Autoscale runners to zero
Docker runners on fixed hosts cost the same whether anyone commits or not. GitLab on the Kubernetes executor creates job pods on demand, and the cluster autoscaler adds and removes nodes based on the queue. When there are no jobs, the pool scales down to zero or to a small minimum for working hours. When commits spike, the autoscaler adds nodes and releases them afterwards. You pay for jobs that ran, not for machines waiting overnight.
The cost is cold start: the first job after a quiet period waits tens of seconds for a node. For most teams that trade pays off.
3. Spot nodes for CI
CI is an ephemeral workload: a job either succeeds or gets retried. That makes it a good fit for spot / preemptible nodes, which can be several times cheaper than on-demand.
There’s one condition: the pipeline has to tolerate node preemption, which means idempotent jobs with retries enabled. We don’t run production on spot, but we do run builds and tests there. An on-demand pool stays as a buffer in case spot capacity runs out.
4. Caching and smaller images
Every build minute is a minute you pay for, so a shorter build also lowers the bill. Two changes help the most. Dependency caching means Maven, npm, pip or Docker layers don’t start from scratch on every build. Multi-stage builds and small base images shorten the pull and speed up job start.
In one of our migrations, multi-stage builds and layer cleanup cut image size by 87%, and build time dropped from around 6 minutes to ~30 seconds. We described it in the case study of migrating 60 applications to Kubernetes.
5. Cost per project
As long as the CI bill is a single number, you don’t know which team drives most of the cost or where cutting makes sense. A namespace and ResourceQuota per project give you a natural point to attribute usage. Prometheus with Grafana shows CPU, RAM and job time broken down by team, so you optimize where the cost is highest.
Where to start
Start by measuring cost per project and by right-sizing. It costs nothing and shows the scale of the waste right away. Then autoscaling and spot nodes, because that’s where the biggest savings are. Leave caching and smaller images for last. They will also speed up your team’s work.
We run CI/CD on Kubernetes in production and take over environments where the bill got out of hand. If your CI costs more than it should, see how we build and optimize pipelines or book a free consultation. We start with an audit and concrete numbers.