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: implementing-rbac-for-kubernetes-cluster3description: 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 secu4license: Apache-2.05---6# Implementing RBAC for Kubernetes Cluster78## Overview9Configure 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.1011## Objectives12- Design RBAC role hierarchy for multi-tenant clusters13- Create granular Roles and ClusterRoles for different personas14- Configure RoleBindings and ClusterRoleBindings with least privilege15- Secure service accounts and limit their default permissions16- Integrate RBAC with external identity providers (OIDC)17- Audit and monitor RBAC usage with Kubernetes audit logs1819## Key Concepts2021### RBAC API Objects221. **Role**: Namespace-scoped permissions (pods, services, deployments within a namespace)232. **ClusterRole**: Cluster-wide permissions (nodes, namespaces, PVs, CRDs)243. **RoleBinding**: Grants Role to users/groups/serviceAccounts in a namespace254. **ClusterRoleBinding**: Grants ClusterRole cluster-wide2627### Kubernetes RBAC Verbs28- `get`, `list`, `watch`: Read-only operations29- `create`, `update`, `patch`: Write operations30- `delete`, `deletecollection`: Destructive operations31- `impersonate`: Assume identity of another user32- `escalate`: Modify RBAC roles (highly privileged)33- `bind`: Create RoleBindings (highly privileged)3435### Persona-Based Access Model36- **Cluster Admin**: Full cluster management (limit to 2-3 people)37- **Namespace Admin**: Full control within assigned namespace38- **Developer**: Deploy and manage workloads in assigned namespace39- **Viewer**: Read-only access to namespace resources40- **CI/CD Service Account**: Deploy workloads, manage configmaps/secrets4142## Implementation Steps4344### Step 1: Disable Default Permissive Settings451. Ensure `--authorization-mode=RBAC` is enabled on API server462. Remove default cluster-admin bindings from non-admin users473. Disable auto-mounting of service account tokens in pods484. Restrict access to default service account in each namespace4950### Step 2: Create Custom Roles51```yaml52# Developer Role - namespace scoped53apiVersion: rbac.authorization.k8s.io/v154kind: Role55metadata:56 namespace: app-team57 name: developer58rules:59- apiGroups: ["", "apps", "batch"]60 resources: ["pods", "deployments", "services", "configmaps", "jobs"]61 verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]62- apiGroups: [""]63 resources: ["secrets"]64 verbs: ["get", "list"] # read secrets but limit create/update65- apiGroups: [""]66 resources: ["pods/log", "pods/exec"]67 verbs: ["get", "create"]68```6970### Step 3: Bind Roles to Users/Groups71```yaml72apiVersion: rbac.authorization.k8s.io/v173kind: RoleBinding74metadata:75 name: developer-binding76 namespace: app-team77subjects:78- kind: Group79 name: "dev-team"80 apiGroup: rbac.authorization.k8s.io81roleRef:82 kind: Role83 name: developer84 apiGroup: rbac.authorization.k8s.io85```8687### Step 4: Secure Service Accounts88- Create dedicated service accounts per application89- Disable automountServiceAccountToken for pods that don't need API access90- Use projected service account tokens with audience and expiry91- Bind minimum required permissions to each service account9293### Step 5: OIDC Integration941. Configure API server with OIDC flags (issuer-url, client-id, username-claim, groups-claim)952. Map OIDC groups to Kubernetes groups in RoleBindings963. Use short-lived tokens from OIDC provider974. Configure kubectl with OIDC authentication plugin9899### Step 6: Audit and Monitoring100- Enable Kubernetes audit logging (audit-policy.yaml)101- Log all RBAC-related events (role creation, binding changes)102- Alert on ClusterRoleBinding creation/modification103- Monitor for privilege escalation attempts104- Regular review of who has cluster-admin access105106## Security Controls107| Control | NIST 800-53 | Description |108|---------|-------------|-------------|109| Access Control | AC-3 | RBAC enforcement |110| Least Privilege | AC-6 | Minimum necessary Kubernetes permissions |111| Account Management | AC-2 | Service account lifecycle |112| Audit | AU-3 | Kubernetes audit logging |113| Separation of Duties | AC-5 | Namespace isolation |114115## Common Pitfalls116- Granting cluster-admin to CI/CD pipelines117- Using wildcard (*) verbs or resources in ClusterRoles118- Not restricting pods/exec which allows container shell access119- Leaving default service account with broad permissions120- Not auditing who can create RoleBindings (privilege escalation vector)121122## Verification123- [ ] All users authenticate via OIDC (no static tokens/certs)124- [ ] No unnecessary ClusterRoleBindings to cluster-admin125- [ ] Developers limited to their assigned namespaces126- [ ] Service accounts use least-privilege roles127- [ ] automountServiceAccountToken disabled by default128- [ ] Audit logging captures RBAC changes129- [ ] `kubectl auth can-i` validates expected permissions per persona