πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Security intermediate ⏱ 15 minutes K8s 1.28+

RBAC Least Privilege Kubernetes

Configure Kubernetes RBAC with least-privilege Roles, ClusterRoles, and service account bindings. Audit permissions, restrict secrets access.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: Use namespace-scoped Role (not ClusterRole) wherever possible, bind to specific ServiceAccount (not default), never grant * verbs on secrets, and audit with kubectl auth can-i --list --as=system:serviceaccount:ns:sa.

The Problem

The default Kubernetes ServiceAccount in each namespace may have more permissions than needed. Common RBAC mistakes: granting cluster-admin to CI/CD pipelines, using ClusterRoleBinding when RoleBinding suffices, and wildcard verbs on sensitive resources like secrets.

The Solution

Application Service Account

apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-app
  namespace: production
automountServiceAccountToken: false
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: my-app-role
  namespace: production
rules:
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "watch"]
    resourceNames: ["my-app-config"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]
    resourceNames: ["my-app-tls"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: my-app-binding
  namespace: production
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: my-app-role
subjects:
  - kind: ServiceAccount
    name: my-app
    namespace: production

CI/CD Runner (Minimal Permissions)

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: cicd-runner
  namespace: staging
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "patch", "update"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get"]

Audit Permissions

# What can this service account do?
kubectl auth can-i --list \
  --as=system:serviceaccount:production:my-app \
  -n production

# Can it read secrets?
kubectl auth can-i get secrets \
  --as=system:serviceaccount:production:my-app \
  -n production

# Find overprivileged ClusterRoleBindings
kubectl get clusterrolebindings -o json | \
  jq '.items[] | select(.roleRef.name=="cluster-admin") | .subjects[]'

Common Issues

Pod can’t read ConfigMaps after RBAC lockdown

Set automountServiceAccountToken: true (or mount token explicitly) and ensure the Role includes the specific ConfigMap name in resourceNames.

β€œforbidden” errors in CI/CD pipeline

Check which ServiceAccount the runner uses and what Role is bound. Use kubectl auth can-i to debug.

Best Practices

  • automountServiceAccountToken: false by default β€” only mount when the pod needs API access
  • resourceNames to restrict access to specific ConfigMaps/Secrets β€” not all in the namespace
  • Role (not ClusterRole) for application workloads β€” namespace-scoped by default
  • Separate ServiceAccount per application β€” don’t share the default SA
  • Audit regularly β€” kubectl auth can-i --list for each service account

Key Takeaways

  • Default ServiceAccount may have more permissions than expected β€” always create dedicated SAs
  • Use resourceNames to restrict access to specific resources, not all of a type
  • automountServiceAccountToken: false prevents unnecessary API server access
  • kubectl auth can-i is your RBAC debugging tool
  • Role + RoleBinding for namespaced access; ClusterRole + ClusterRoleBinding only when truly needed
#rbac #security #least-privilege #service-account #roles
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens