Kubernetes DevOps AutomationDevOps GuidesDevSecOpsKubernetes Guide

Article & Tutorial

Kubernetes Namespaces and RBAC Explained: Roles, Bindings, ServiceAccounts and Access Control

Learn Kubernetes Namespaces and RBAC with practical YAML examples for Roles, ClusterRoles, RoleBindings, ServiceAccounts, least privilege, permission…

Contents

Kubernetes Namespaces and RBAC Explained

Namespaces organize workloads and policies. Role-Based Access Control, or RBAC, decides which users, groups and applications may perform specific actions. This guide explains both concepts with practical, production-oriented examples.

What problem do Namespaces solve?

A Kubernetes cluster may host many applications, teams and environments. Without a logical boundary, hundreds of Pods, Services, Secrets and ConfigMaps can become difficult to manage. A Namespace groups namespaced resources so teams can apply access rules, quotas, naming conventions and operational ownership more safely.

Environment separation

Use namespaces such as dev , staging and production .

Team separation

Give application teams access only to the namespaces they own.

Policy boundary

Apply RBAC, ResourceQuota, LimitRange and NetworkPolicy per namespace.

Default Kubernetes Namespaces

  • Namespace — Purpose
  • default — Used when no namespace is specified. Avoid placing every production workload here.
  • kube-system — Contains Kubernetes system components and add-ons.
  • kube-public — Contains publicly readable cluster information when required.
  • kube-node-lease — Stores node lease objects used for node heartbeat performance.

Create and use a Namespace

kubectl create namespace dev kubectl get namespaces kubectl get all -n dev kubectl config set-context --current --namespace=dev

The final command changes the default namespace for the current kubectl context. Verify it before running destructive commands:

kubectl config view --minify | grep namespace

Namespace YAML with labels

apiVersion: v1 kind: Namespace metadata: name: dev labels: environment: development owner: platform-team

What is Kubernetes RBAC?

RBAC evaluates a request using four basic questions: who is making the request, which action is requested, which resource is targeted and where the resource exists. Permissions are additive: Kubernetes combines all applicable roles. RBAC does not provide an explicit deny rule.

The four primary RBAC objects

  • Object — Scope — Purpose
  • Role — One namespace — Defines allowed operations on namespaced resources.
  • ClusterRole — Cluster-wide definition — Defines permissions for cluster-scoped resources or reusable namespaced permissions.
  • RoleBinding — One namespace — Grants a Role or ClusterRole to subjects inside a namespace.
  • ClusterRoleBinding — Entire cluster — Grants a ClusterRole across the whole cluster.

RBAC subjects: users, groups and ServiceAccounts

  • User: An external human identity authenticated through certificates, OIDC, cloud IAM or another identity provider.
  • Group: A collection of users supplied by the authentication system.
  • ServiceAccount: A Kubernetes identity used by Pods, controllers and automation.

Kubernetes stores ServiceAccounts as API objects, but it does not normally manage human user accounts itself.

Practical example: read-only Pod access in dev

1. Create the ServiceAccount

apiVersion: v1 kind: ServiceAccount metadata: name: app-reader namespace: dev

2. Create a namespace-scoped Role

apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: pod-reader namespace: dev rules: - apiGroups: [""] resources: ["pods", "pods/log"] verbs: ["get", "list", "watch"]

3. Bind the Role to the ServiceAccount

apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: app-reader-pod-access namespace: dev subjects: - kind: ServiceAccount name: app-reader namespace: dev roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: pod-reader

The roleRef points to the permission definition. The subjects section identifies who receives those permissions.

Understanding apiGroups, resources and verbs

  • Field — Meaning — Examples
  • apiGroups — API group containing the resource — "" for Pods and Services; apps for Deployments
  • resources — Object types or subresources — pods , deployments , pods/log
  • verbs — Allowed operations — get , list , watch , create , update , patch , delete

Example: Deployment operator Role

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

This role allows application deployment work without granting permission to delete namespaces, modify cluster nodes or read every Secret.

Role versus ClusterRole

A ClusterRole can be used in two ways:

  1. A RoleBinding can grant the ClusterRole only inside one namespace.
  2. A ClusterRoleBinding can grant it across the entire cluster.

ClusterRole for viewing nodes

apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: node-viewer rules: - apiGroups: [""] resources: ["nodes"] verbs: ["get", "list", "watch"]

Nodes are cluster-scoped resources, so a namespace Role cannot grant node access.

Test permissions before troubleshooting applications

kubectl auth can-i get pods -n dev kubectl auth can-i delete pods -n dev kubectl auth can-i list secrets -n dev \ --as=system:serviceaccount:dev:app-reader kubectl auth can-i --list -n dev \ --as=system:serviceaccount:dev:app-reader

Administrators can also impersonate a user or group while testing:

kubectl auth can-i create deployments -n dev --as=alice kubectl auth can-i get pods -n production --as=alice --as-group=developers

Common RBAC errors and how to fix them

  • Symptom — Likely cause — Check
  • Forbidden — No matching permission — Run kubectl auth can-i for the exact subject, verb, resource and namespace.
  • RoleBinding exists but access fails — Wrong subject name, namespace or roleRef — Describe the binding and verify all references.
  • Pod cannot call the API — Wrong ServiceAccount or missing token permission — Check spec.serviceAccountName and the bound role.
  • User has too much access — Broad ClusterRoleBinding or wildcard rules — Review all bindings and avoid * where possible.

Useful investigation commands

kubectl get roles,rolebindings -n dev kubectl get clusterroles,clusterrolebindings kubectl describe role pod-reader -n dev kubectl describe rolebinding app-reader-pod-access -n dev kubectl get serviceaccounts -n dev kubectl get pod APP_POD -n dev -o jsonpath='{.spec.serviceAccountName}' kubectl api-resources

Namespaces with ResourceQuota

RBAC controls actions, while ResourceQuota controls consumption. They solve different problems and are often used together.

apiVersion: v1 kind: ResourceQuota metadata: name: dev-quota namespace: dev spec: hard: requests.cpu: "4" requests.memory: 8Gi limits.cpu: "8" limits.memory: 16Gi pods: "30" services: "10"

Production namespace and RBAC design example

Developers

Create and update workloads in development; read production logs only.

CI/CD ServiceAccount

Patch a specific Deployment in an approved namespace, not administer the cluster.

Platform team

Manage namespaces, admission policies and shared infrastructure through audited roles.

Auditors

Read resources and events without create, patch or delete privileges.

Common mistakes

  • Granting cluster-admin because a smaller role takes longer to design.
  • Using the default namespace for all applications.
  • Binding permissions to the default ServiceAccount.
  • Using wildcard resources and verbs without a documented need.
  • Allowing broad Secret access, which may expose credentials and ServiceAccount tokens.
  • Confusing authentication with authorization: a valid login does not automatically grant permissions.
  • Deleting a namespace without understanding that its namespaced resources will also be removed.

RBAC best practices

  • Apply least privilege and start with read-only access.
  • Prefer RoleBindings over ClusterRoleBindings for application teams.
  • Create a dedicated ServiceAccount for each workload or automation purpose.
  • Do not grant applications permission to create or modify RBAC objects unless absolutely required.
  • Use groups instead of maintaining individual human bindings at scale.
  • Keep RBAC YAML in Git and review changes through pull or merge requests.
  • Audit effective permissions regularly and remove stale bindings.
  • Protect production with stronger approval, identity and logging controls.

Hands-on practice lab

kubectl create namespace rbac-lab kubectl create serviceaccount reader -n rbac-lab kubectl create role pod-reader \ --verb=get,list,watch \ --resource=pods \ -n rbac-lab kubectl create rolebinding reader-binding \ --role=pod-reader \ --serviceaccount=rbac-lab:reader \ -n rbac-lab kubectl auth can-i list pods -n rbac-lab \ --as=system:serviceaccount:rbac-lab:reader kubectl auth can-i delete pods -n rbac-lab \ --as=system:serviceaccount:rbac-lab:reader

The first permission check should return yes ; the delete check should return no .

Frequently asked questions

No. It organizes resources and provides a scope for policies, but effective isolation also depends on RBAC, network controls, workload security and the surrounding infrastructure.

No. Nodes are cluster-scoped. Use a ClusterRole for node permissions.

Yes. The ClusterRole permissions are then granted only inside the RoleBinding's namespace.

No. Standard RBAC permissions are additive. Avoid granting the unwanted permission in the first place.

Prefer a dedicated ServiceAccount with explicitly defined minimal permissions.

Related Melochip guides

  • Kubernetes Learning Path
  • Kubernetes Security Best Practices
  • Kubernetes ConfigMaps and Secrets Explained
  • Kubernetes Troubleshooting Guide

Official references

  • Kubernetes Namespaces documentation
  • Kubernetes RBAC documentation
  • Kubernetes RBAC good practices

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.