Skip to content
Latchkey

kubectl "cannot get/create namespaces" - Fix Cluster-Scope RBAC

A namespaced RoleBinding cannot grant access to cluster-scoped resources. If CI needs to manage namespaces, nodes, CRDs, or ClusterRoles, it requires a ClusterRole bound with a ClusterRoleBinding.

What this error means

kubectl reports <resource> is forbidden: User "<id>" cannot <verb> resource "<res>" at the cluster scope. The identity may have namespaced access but is denied on cluster-scoped objects.

kubectl output
Error from server (Forbidden): namespaces is forbidden: User
"system:serviceaccount:ci:deployer" cannot create resource "namespaces" in API
group "" at the cluster scope

Common causes

Only a namespaced Role is bound

A Role/RoleBinding grants access within one namespace; it cannot authorize cluster-scoped resources, which live outside any namespace.

Trying to manage cluster resources from CI

Creating namespaces, installing CRDs, or binding cluster roles all require cluster-scoped grants the pipeline SA does not have.

How to fix it

Grant a ClusterRole with a ClusterRoleBinding

Scope the ClusterRole to the specific cluster-scoped verbs/resources the pipeline needs.

Terminal
kubectl create clusterrole ns-manager \
  --verb=get,list,create --resource=namespaces
kubectl create clusterrolebinding ci-ns-manager \
  --clusterrole=ns-manager --serviceaccount=ci:deployer

Verify the cluster-scoped permission

Terminal
kubectl auth can-i create namespaces \
  --as=system:serviceaccount:ci:deployer

How to prevent it

  • Use ClusterRole/ClusterRoleBinding for cluster-scoped resources, Role/RoleBinding for namespaced ones.
  • Keep cluster-scoped grants minimal and reviewed in git.
  • Pre-flight with kubectl auth can-i ... at the cluster scope.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →