Kubernetes_rbac
Kubernetes RBAC
Section titled “Kubernetes RBAC”Overview
Section titled “Overview”Role-Based Access Control (RBAC) is a method of regulating access to Kubernetes resources based on the roles of individual users or service accounts within a cluster.
RBAC API Resources
Section titled “RBAC API Resources”┌─────────────────────────────────────────────────────────────────┐│ RBAC Authorization ││ ││ ┌────────────────┐ ┌────────────────┐ ││ │ Role │────────▶│ RoleBinding │ ││ │ (namespace) │ │ (namespace) │ ││ └────────────────┘ └────────────────┘ ││ │ │ ││ │ ▼ ││ │ ┌────────────────┐ ││ └────────────────▶│ Subject │ ││ │ (User/Group/ │ ││ │ ServiceAccount│ ││ └────────────────┘ ││ ││ ┌────────────────┐ ┌────────────────┐ ││ │ ClusterRole │───────▶│ClusterRoleBinding│ ││ │ (cluster-wide)│ │ (cluster-wide) │ ││ └────────────────┘ └────────────────┘ │└─────────────────────────────────────────────────────────────────┘API Objects
Section titled “API Objects”| Object | Scope | Description |
|---|---|---|
| Role | Namespace | Grants permissions within a specific namespace |
| ClusterRole | Cluster | Grants permissions cluster-wide or to cluster-scoped resources |
| RoleBinding | Namespace | Binds a Role or ClusterRole to subjects within a namespace |
| ClusterRoleBinding | Cluster | Binds a ClusterRole to subjects cluster-wide |
Roles and ClusterRoles
Section titled “Roles and ClusterRoles”Role (Namespace-scoped)
Section titled “Role (Namespace-scoped)”apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: default name: pod-readerrules:- apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"]ClusterRole (Cluster-scoped)
Section titled “ClusterRole (Cluster-scoped)”apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: secret-readerrules:- apiGroups: [""] resources: ["secrets"] verbs: ["get", "list", "watch"]Multiple Rules
Section titled “Multiple Rules”apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: myapp name: myapp-adminrules:# Rule 1: Pods- apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch", "create", "update", "delete"]# Rule 2: ConfigMaps- apiGroups: [""] resources: ["configmaps"] verbs: ["get", "list", "watch", "create", "update", "delete"]# Rule 3: Services- apiGroups: [""] resources: ["services"] verbs: ["get", "list", "watch", "create", "update", "delete"]# Rule 4: Deployments- apiGroups: ["apps"] resources: ["deployments"] verbs: ["get", "list", "watch", "create", "update", "delete", "patch"]Common verbs:
get- Read single resourcelist- List resourceswatch- Watch for changescreate- Create new resourcesupdate- Update existing resourcespatch- Partially update resourcesdelete- Delete resourcesdeletecollection- Delete multiple resources
RoleBinding
Section titled “RoleBinding”Binding to a User
Section titled “Binding to a User”apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: read-pods namespace: defaultsubjects:# User- kind: User name: jane apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.ioBinding to a Group
Section titled “Binding to a Group”subjects:# Group- kind: Group name: developers apiGroup: rbac.authorization.k8s.ioBinding to a ServiceAccount
Section titled “Binding to a ServiceAccount”subjects:# ServiceAccount- kind: ServiceAccount name: myapp-sa namespace: myappClusterRoleBinding
Section titled “ClusterRoleBinding”Grant cluster-wide access:
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: read-all-podssubjects:- kind: User name: admin-user apiGroup: rbac.authorization.k8s.ioroleRef: kind: ClusterRole name: pod-reader-cluster apiGroup: rbac.authorization.k8s.ioReusing ClusterRoles
Section titled “Reusing ClusterRoles”ClusterRoles can be bound to specific namespaces:
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: read-pods-in-namespace namespace: myappsubjects:- kind: User name: jane apiGroup: rbac.authorization.k8s.ioroleRef: kind: ClusterRole name: pod-reader # Reuse cluster-wide role apiGroup: rbac.authorization.k8s.ioBuilt-in ClusterRoles
Section titled “Built-in ClusterRoles”Kubernetes provides many built-in ClusterRoles:
# List built-in ClusterRoleskubectl get clusterrolesKey built-in roles:
| ClusterRole | Description |
|---|---|
| admin | Full access within a namespace |
| edit | Read/write access to most resources |
| view | Read-only access to most resources |
| cluster-admin | Super-user access |
| system:node | Required for kubelet |
| system:kube-scheduler | Required for scheduler |
| system:controller:* | Required for controllers |
Practical Examples
Section titled “Practical Examples”Developer Role
Section titled “Developer Role”apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: development name: developerrules:# Deployments, StatefulSets, DaemonSets- apiGroups: ["apps"] resources: ["deployments", "statefulsets", "daemonsets"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]# Pods, Services, ConfigMaps, Secrets (read-only for secrets)- apiGroups: [""] resources: ["pods", "services", "configmaps", "endpoints"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]- apiGroups: [""] resources: ["secrets"] verbs: ["get", "list", "watch"]# Jobs, CronJobs- apiGroups: ["batch"] resources: ["jobs", "cronjobs"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]# Ingress- apiGroups: ["networking.k8s.io"] resources: ["ingresses"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]Read-Only Role
Section titled “Read-Only Role”apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: production name: viewerrules:- apiGroups: ["*"] resources: ["*"] verbs: ["get", "list", "watch"]CI/CD Service Account
Section titled “CI/CD Service Account”# Create ServiceAccountapiVersion: v1kind: ServiceAccountmetadata: name: ci-pipeline namespace: myapp---# Create Role for deploymentsapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: myapp name: ci-deployerrules:- apiGroups: ["apps"] resources: ["deployments"] verbs: ["get", "list", "watch", "update", "patch"]- apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"]- apiGroups: [""] resources: ["pods/log"] verbs: ["get", "list"]---# Bind Role to ServiceAccountapiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: ci-deployer-binding namespace: myappsubjects:- kind: ServiceAccount name: ci-pipeline namespace: myapproleRef: kind: Role name: ci-deployer apiGroup: rbac.authorization.k8s.ioAggregated ClusterRoles
Section titled “Aggregated ClusterRoles”Use labels to aggregate rules:
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: custom-metrics-reader labels: rbac.example.com/aggregate-to-monitoring: "true"rules:- apiGroups: ["custom.metrics.k8s.io"] resources: ["*"] verbs: ["get", "list"]---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: monitoring-readeraggregationRule: clusterRoleSelectors: - matchLabels: rbac.example.com/aggregate-to-monitoring: "true"rules: [] # Rules are automatically aggregatedChecking Permissions
Section titled “Checking Permissions”# Check what permissions a user haskubectl auth can-i get pods --as=jane
# Check namespace permissionskubectl auth can-i get pods -n myapp --as=jane
# Check if user can create deploymentskubectl auth can-i create deployments --as=jane
# List all permissions (as admin)kubectl auth can-i --list --as=janeDebugging RBAC
Section titled “Debugging RBAC”# Check RoleBindings in a namespacekubectl get rolebindings -n <namespace>
# Describe a RoleBindingkubectl describe rolebinding <name> -n <namespace>
# Check ClusterRoleBindingskubectl get clusterrolebindings
# Describe a ClusterRoleBindingkubectl describe clusterrolebinding <name>
# Show roles and bindings for a userkubectl auth reconcile -f my-rbac-config.yamlCommon Patterns
Section titled “Common Patterns”Namespace Admin
Section titled “Namespace Admin”# Create namespaceapiVersion: v1kind: Namespacemetadata: name: team-a---# Grant admin access to namespaceapiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: team-a-admin namespace: team-asubjects:- kind: Group name: team-a apiGroup: rbac.authorization.k8s.ioroleRef: kind: ClusterRole name: admin apiGroup: rbac.authorization.k8s.ioCross-Namespace Access
Section titled “Cross-Namespace Access”apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: cross-namespace-readerrules:- apiGroups: [""] resources: ["pods", "services"] verbs: ["get", "list", "watch"]- apiGroups: [""] resources: ["namespaces"] resourceNames: ["frontend", "backend"] verbs: ["get", "list", "watch"]Best Practices
Section titled “Best Practices”- Use ServiceAccounts for applications: Don’t use default user credentials
- Principle of least privilege: Grant minimum required permissions
- Use namespace-scoped roles: Prefer Role over ClusterRole when possible
- Document RBAC policies: Keep RBAC configuration in version control
- Audit regularly: Review who has access to what
- Use groups: Assign permissions to groups, not individual users
- Test before production: Verify permissions work as expected
Summary
Section titled “Summary”RBAC is essential for:
- Security: Control who can access what
- Compliance: Meet regulatory requirements
- Multi-tenancy: Isolate teams and workloads
- Auditing: Track access patterns
Key concepts:
- Role/ClusterRole: Define what can be done
- RoleBinding/ClusterRoleBinding: Define who can do it
- Subjects: Users, Groups, or ServiceAccounts
- Verbs: Actions that can be performed
- Resources: API objects being accessed