QA
Intermediate LevelDocker & Deployment

How does Kubernetes Role-Based Access Control (RBAC) restrict API access?

❓ Interview Prompt: Explain RBAC Roles, ClusterRoles, RoleBindings, ServiceAccounts, and least-privilege API access patterns in Kubernetes.

Kubernetes RBAC controls who (subject) can do what (verbs) on which resources (API objects).

RBAC Components:

1
Role (namespace-scoped): Defines allowed actions on resources within a specific namespace.
2
ClusterRole (cluster-scoped): Like Role but works across all namespaces — or for cluster-level resources (Nodes, PersistentVolumes).
3
RoleBinding: Binds a Role to a Subject within a namespace.
4
ClusterRoleBinding: Binds a ClusterRole to a Subject cluster-wide.
5
Subject Types: ServiceAccounts (for pods), Users (humans), Groups.

ServiceAccounts for Pods:

Every Pod runs with a ServiceAccount — its token is auto-mounted at /var/run/secrets/kubernetes.io/serviceaccount/token and used to authenticate to the Kubernetes API.

Best Practice: Create dedicated ServiceAccounts per application with minimum required permissions. Never use the default ServiceAccount with broad permissions.

Common Verbs:

get, list, watch, create, update, patch, delete, deletecollection.

Code Implementation Example

yaml
# Step 1: Create a dedicated ServiceAccount for the API pod
apiVersion: v1
kind: ServiceAccount
metadata:
  name: api-sa
  namespace: app
automountServiceAccountToken: false   # opt-in, not auto-mount
---
# Step 2: Define a Role — read-only access to ConfigMaps and Secrets
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: api-config-reader
  namespace: app
rules:
  - apiGroups: [""]
    resources: ["configmaps", "secrets"]
    verbs: ["get", "list"]       # read-only — no create/delete
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "watch"]
---
# Step 3: Bind the Role to the ServiceAccount
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: api-config-reader-binding
  namespace: app
subjects:
  - kind: ServiceAccount
    name: api-sa
    namespace: app
roleRef:
  kind: Role
  name: api-config-reader
  apiGroup: rbac.authorization.k8s.io
💡Least-privilege RBAC: dedicated ServiceAccount with read-only ConfigMap/Secret access — no cluster-wide permissions.
🎯 Interview Key Takeaway

RBAC = Role (what) + RoleBinding (who). Create per-app ServiceAccounts with minimum verbs. Never grant wildcard (*) permissions or bind to the default ServiceAccount.

#Kubernetes#RBAC#ServiceAccount#Security#Least Privilege#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?