Implementing RBAC for Kubernetes Cluster
Overview
Configure Kubernetes Role-Based Access Control (RBAC) to enforce least-privilege access to cluster resources. This skill covers Role/ClusterRole design, RoleBinding configuration, service account security, namespace isolation, and audit logging for multi-tenant Kubernetes environments.
Objectives
- Design RBAC role hierarchy for multi-tenant clusters
- Create granular Roles and ClusterRoles for different personas
- Configure RoleBindings and ClusterRoleBindings with least privilege
- Secure service accounts and limit their default permissions
- Integrate RBAC with external identity providers (OIDC)
- Audit and monitor RBAC usage with Kubernetes audit logs
Key Concepts
RBAC API Objects
- Role: Namespace-scoped permissions (pods, services, deployments within a namespace)
- ClusterRole: Cluster-wide permissions (nodes, namespaces, PVs, CRDs)
- RoleBinding: Grants Role to users/groups/serviceAccounts in a namespace
- ClusterRoleBinding: Grants ClusterRole cluster-wide
Kubernetes RBAC Verbs
get, list, watch: Read-only operations
create, update, patch: Write operations
delete, deletecollection: Destructive operations
impersonate: Assume identity of another user
escalate: Modify RBAC roles (highly privileged)
bind: Create RoleBindings (highly privileged)
Persona-Based Access Model
- Cluster Admin: Full cluster management (limit to 2-3 people)
- Namespace Admin: Full control within assigned namespace
- Developer: Deploy and manage workloads in assigned namespace
- Viewer: Read-only access to namespace resources
- CI/CD Service Account: Deploy workloads, manage configmaps/secrets
Implementation Steps
Step 1: Disable Default Permissive Settings
- Ensure
--authorization-mode=RBAC is enabled on API server
- Remove default cluster-admin bindings from non-admin users
- Disable auto-mounting of service account tokens in pods
- Restrict access to default service account in each namespace
Step 2: Create Custom Roles
# Developer Role - namespace scoped
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: app-team
name: developer
rules:
- apiGroups: ["", "apps", "batch"]
resources: ["pods", "deployments", "services", "configmaps", "jobs"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list"] # read secrets but limit create/update
- apiGroups: [""]
resources: ["pods/log", "pods/exec"]
verbs: ["get", "create"]
Step 3: Bind Roles to Users/Groups
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: developer-binding
namespace: app-team
subjects:
- kind: Group
name: "dev-team"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: developer
apiGroup: rbac.authorization.k8s.io
Step 4: Secure Service Accounts
- Create dedicated service accounts per application
- Disable automountServiceAccountToken for pods that don't need API access
- Use projected service account tokens with audience and expiry
- Bind minimum required permissions to each service account
Step 5: OIDC Integration
- Configure API server with OIDC flags (issuer-url, client-id, username-claim, groups-claim)
- Map OIDC groups to Kubernetes groups in RoleBindings
- Use short-lived tokens from OIDC provider
- Configure kubectl with OIDC authentication plugin
Step 6: Audit and Monitoring
- Enable Kubernetes audit logging (audit-policy.yaml)
- Log all RBAC-related events (role creation, binding changes)
- Alert on ClusterRoleBinding creation/modification
- Monitor for privilege escalation attempts
- Regular review of who has cluster-admin access
Security Controls
| Control |
NIST 800-53 |
Description |
| Access Control |
AC-3 |
RBAC enforcement |
| Least Privilege |
AC-6 |
Minimum necessary Kubernetes permissions |
| Account Management |
AC-2 |
Service account lifecycle |
| Audit |
AU-3 |
Kubernetes audit logging |
| Separation of Duties |
AC-5 |
Namespace isolation |
Common Pitfalls
- Granting cluster-admin to CI/CD pipelines
- Using wildcard (*) verbs or resources in ClusterRoles
- Not restricting pods/exec which allows container shell access
- Leaving default service account with broad permissions
- Not auditing who can create RoleBindings (privilege escalation vector)
Verification
1---2name: none-133description: Configure Kubernetes Role-Based Access Control (RBAC) to enforce least-privilege access to cluster resources. This skill covers Role/ClusterRole design, RoleBinding configuration, service account secu4domain: cybersecurity5subdomain: identity-access-management6tags: [iam, identity, access-control, authorization, rbac, kubernetes, k8s]7version: "1.0"8author: mahipal9license: MIT10metadata:11 source: https://skillmd.com/skills/njones17/none-1312---1314# Implementing RBAC for Kubernetes Cluster1516## Overview17Configure Kubernetes Role-Based Access Control (RBAC) to enforce least-privilege access to cluster resources. This skill covers Role/ClusterRole design, RoleBinding configuration, service account security, namespace isolation, and audit logging for multi-tenant Kubernetes environments.1819## Objectives20- Design RBAC role hierarchy for multi-tenant clusters21- Create granular Roles and ClusterRoles for different personas22- Configure RoleBindings and ClusterRoleBindings with least privilege23- Secure service accounts and limit their default permissions24- Integrate RBAC with external identity providers (OIDC)25- Audit and monitor RBAC usage with Kubernetes audit logs2627## Key Concepts2829### RBAC API Objects301. **Role**: Namespace-scoped permissions (pods, services, deployments within a namespace)312. **ClusterRole**: Cluster-wide permissions (nodes, namespaces, PVs, CRDs)323. **RoleBinding**: Grants Role to users/groups/serviceAccounts in a namespace334. **ClusterRoleBinding**: Grants ClusterRole cluster-wide3435### Kubernetes RBAC Verbs36- `get`, `list`, `watch`: Read-only operations37- `create`, `update`, `patch`: Write operations38- `delete`, `deletecollection`: Destructive operations39- `impersonate`: Assume identity of another user40- `escalate`: Modify RBAC roles (highly privileged)41- `bind`: Create RoleBindings (highly privileged)4243### Persona-Based Access Model44- **Cluster Admin**: Full cluster management (limit to 2-3 people)45- **Namespace Admin**: Full control within assigned namespace46- **Developer**: Deploy and manage workloads in assigned namespace47- **Viewer**: Read-only access to namespace resources48- **CI/CD Service Account**: Deploy workloads, manage configmaps/secrets4950## Implementation Steps5152### Step 1: Disable Default Permissive Settings531. Ensure `--authorization-mode=RBAC` is enabled on API server542. Remove default cluster-admin bindings from non-admin users553. Disable auto-mounting of service account tokens in pods564. Restrict access to default service account in each namespace5758### Step 2: Create Custom Roles59```yaml60# Developer Role - namespace scoped61apiVersion: rbac.authorization.k8s.io/v162kind: Role63metadata:64 namespace: app-team65 name: developer66rules:67- apiGroups: ["", "apps", "batch"]68 resources: ["pods", "deployments", "services", "configmaps", "jobs"]69 verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]70- apiGroups: [""]71 resources: ["secrets"]72 verbs: ["get", "list"] # read secrets but limit create/update73- apiGroups: [""]74 resources: ["pods/log", "pods/exec"]75 verbs: ["get", "create"]76```7778### Step 3: Bind Roles to Users/Groups79```yaml80apiVersion: rbac.authorization.k8s.io/v181kind: RoleBinding82metadata:83 name: developer-binding84 namespace: app-team85subjects:86- kind: Group87 name: "dev-team"88 apiGroup: rbac.authorization.k8s.io89roleRef:90 kind: Role91 name: developer92 apiGroup: rbac.authorization.k8s.io93```9495### Step 4: Secure Service Accounts96- Create dedicated service accounts per application97- Disable automountServiceAccountToken for pods that don't need API access98- Use projected service account tokens with audience and expiry99- Bind minimum required permissions to each service account100101### Step 5: OIDC Integration1021. Configure API server with OIDC flags (issuer-url, client-id, username-claim, groups-claim)1032. Map OIDC groups to Kubernetes groups in RoleBindings1043. Use short-lived tokens from OIDC provider1054. Configure kubectl with OIDC authentication plugin106107### Step 6: Audit and Monitoring108- Enable Kubernetes audit logging (audit-policy.yaml)109- Log all RBAC-related events (role creation, binding changes)110- Alert on ClusterRoleBinding creation/modification111- Monitor for privilege escalation attempts112- Regular review of who has cluster-admin access113114## Security Controls115| Control | NIST 800-53 | Description |116|---------|-------------|-------------|117| Access Control | AC-3 | RBAC enforcement |118| Least Privilege | AC-6 | Minimum necessary Kubernetes permissions |119| Account Management | AC-2 | Service account lifecycle |120| Audit | AU-3 | Kubernetes audit logging |121| Separation of Duties | AC-5 | Namespace isolation |122123## Common Pitfalls124- Granting cluster-admin to CI/CD pipelines125- Using wildcard (*) verbs or resources in ClusterRoles126- Not restricting pods/exec which allows container shell access127- Leaving default service account with broad permissions128- Not auditing who can create RoleBindings (privilege escalation vector)129130## Verification131- [ ] All users authenticate via OIDC (no static tokens/certs)132- [ ] No unnecessary ClusterRoleBindings to cluster-admin133- [ ] Developers limited to their assigned namespaces134- [ ] Service accounts use least-privilege roles135- [ ] automountServiceAccountToken disabled by default136- [ ] Audit logging captures RBAC changes137- [ ] `kubectl auth can-i` validates expected permissions per persona