Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

GitLab Runner on Kubernetes is refused when it tries to create secrets

Your error

secrets is forbidden: User "system:serviceaccount:gitlab-runner:default" cannot create resource "secrets" in API group ""1 more line it can show

Fix it fast

Most common cause, 1 of 3

The chart was installed without RBAC, so the runner runs as the default account

Add rbac.create: true to the values file the release was installed with and upgrade the release.

Fix
# values.yaml
rbac:
  create: true

helm upgrade --namespace gitlab-runner -f values.yaml gitlab-runner gitlab/gitlab-runner
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The error secrets is forbidden: User system:serviceaccount:gitlab-runner:default means the Kubernetes executor calls the API with a service account that RBAC grants nothing. Set rbac.create: true in the GitLab Runner Helm values and run helm upgrade, or bind a Role with create, delete, get and update on secrets. Applies to GitLab Runner 19.x on Kubernetes 1.3x.

  • Applies to GitLab Runner 19.6 (Helm chart, Kubernetes executor), Kubernetes 1.37, kubectl 1.37
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List every action the API server allows for the account named in the error. Put the namespace and account name from your own error into the command. A short list with no secrets line confirms an RBAC gap; then compare the namespace the jobs use with the one you checked.

Run this first
kubectl auth can-i --list --namespace=gitlab-runner --as=system:serviceaccount:gitlab-runner:default

Then find the line you see in this list and open its fix:

  1. 1The chart was installed without RBAC, so the runner runs as the default accountThe account in the error ends in :default and kubectl lists no Role or RoleBinding for the runner in its namespace
  2. 2Your own service account has a Role that leaves out secretscan-i answers yes for create pods but no for create secrets with the same account
  3. 3Jobs run in a namespace where the account has no bindingcan-i prints yes for the runner's own namespace and no for the namespace set in config.toml or by a job

What does the error look like?

  • secrets is forbidden: User "system:serviceaccount:gitlab-runner:default" cannot create resource "secrets" in API group ""
  • pods is forbidden: User "system:serviceaccount:gitlab-runner:default" cannot create resource "pods" in API group ""

Cause 1 of 3

The chart was installed without RBAC, so the runner runs as the default account

You seeThe account in the error ends in :default and kubectl lists no Role or RoleBinding for the runner in its namespace

Check

Seeing secrets is forbidden for system:serviceaccount:gitlab-runner:default tells you the runner pod uses the namespace's built-in account. Kubernetes RBAC hands no permissions to service accounts outside kube-system unless someone binds a role, so each create call from the executor is turned down.

Check
kubectl get pods -n gitlab-runner -o yaml | grep serviceAccountName
kubectl get role,rolebinding -n gitlab-runner

Fix

Add rbac.create: true to the values file the release was installed with and upgrade the release. The chart then writes RBAC rules for the runner. Left at its default, rbac.rules is empty and the chart grants every verb on core API resources, so fill rbac.rules if you want only the documented set.

Fix
# values.yaml
rbac:
  create: true

helm upgrade --namespace gitlab-runner -f values.yaml gitlab-runner gitlab/gitlab-runner

Verify

Read the runner pod's service account again, then repeat the can-i check with that name. It answers yes, and a new pipeline job starts its build pod.

Verify
kubectl auth can-i create secrets --namespace=gitlab-runner --as=system:serviceaccount:gitlab-runner:<runner-service-account>

Confirmed by docs.gitlab.com

Did this fix it?

Cause 2 of 3

Your own service account has a Role that leaves out secrets

You seecan-i answers yes for create pods but no for create secrets with the same account

Check

With rbac.create false and serviceAccount.name set, the runner is only as strong as the Role you wrote. GitLab's permission table for the executor asks for create, delete, get and update on secrets, get on serviceaccounts, and create and get on services, next to the pods and pods/exec verbs.

Check
kubectl auth can-i create pods --namespace=gitlab-runner --as=system:serviceaccount:gitlab-runner:<runner-service-account>
kubectl auth can-i create secrets --namespace=gitlab-runner --as=system:serviceaccount:gitlab-runner:<runner-service-account>

Fix

Add the missing rules to that Role and apply it again. The RoleBinding that already ties the Role to the account can stay as it is.

Fix
# extra rules in the existing Role
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create", "delete", "get", "update"]
- apiGroups: [""]
  resources: ["serviceaccounts"]
  verbs: ["get"]
- apiGroups: [""]
  resources: ["services"]
  verbs: ["create", "get"]

kubectl apply -f gitlab-runner-role.yaml

Verify

Both can-i checks print yes, and the job log moves past pod creation.

Verify
kubectl auth can-i create secrets --namespace=gitlab-runner --as=system:serviceaccount:gitlab-runner:<runner-service-account>

Confirmed by docs.gitlab.com

Did this fix it?

Cause 3 of 3

Jobs run in a namespace where the account has no binding

You seecan-i prints yes for the runner's own namespace and no for the namespace set in config.toml or by a job

Check

Role grants stop at their namespace. When the runner's config sends build pods to a separate namespace, the secrets is forbidden error appears there even though the same account works in its home namespace.

Check
kubectl auth can-i create secrets --namespace=ci-jobs --as=system:serviceaccount:gitlab-runner:<runner-service-account>

Fix

Create a RoleBinding inside the job namespace that points at the runner's account; it may reference a ClusterRole holding the runner verbs. If jobs pick their namespace through namespace overwrite, GitLab also requires rbac.clusterWideAccess: true in the chart values.

Fix
kubectl create rolebinding gitlab-runner-jobs --clusterrole=<runner-clusterrole> --serviceaccount=gitlab-runner:<runner-service-account> --namespace=ci-jobs

Verify

The can-i check against the job namespace now prints yes.

Verify
kubectl auth can-i create secrets --namespace=ci-jobs --as=system:serviceaccount:gitlab-runner:<runner-service-account>

Confirmed by kubernetes.io

Did this fix it?

How do I stop it coming back?

  • Keep rbac.create (or serviceAccount.name plus a Role) in the committed values file, so a reinstall cannot fall back to the default account.
  • Copy the Role from the GitLab permission table that matches your runner version, and recheck it when you turn on a feature flag such as FF_SUSPENDABLE_ENVIRONMENTS.
  • Run the kubectl auth can-i checks for secrets and pods in every job namespace as a post-install step of the chart.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.