RBAC in Kubernetes.
Role-Based Access Control (RBAC) is a key security feature in Kubernetes that allows administrators to dynamically configure policies to control who can access specific Kubernetes resources and what actions they can perform. Here's an in-depth explanation:
Key Concepts of RBAC
Role:
A Role contains a set of permissions, which are rules defining what actions can be performed on which resources.
Roles are namespaced. They apply only to resources within a specific namespace.
ClusterRole:
- Similar to a Role, but ClusterRoles are not namespaced. They can be used to grant permissions cluster-wide or across all namespaces.
RoleBinding:
A RoleBinding grants the permissions defined in a Role to a user, group, or service account within a specific namespace.
It links a Role to one or more subjects (users, groups, or service accounts).
ClusterRoleBinding:
- Similar to RoleBinding, but ClusterRoleBinding grants permissions to users, groups, or service accounts across the entire cluster.
How RBAC Works
Defining a Role or ClusterRole:
You create a Role or ClusterRole to specify the permissions.
Example:
kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: namespace: default name: pod-reader rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "watch", "list"]
Binding the Role/ClusterRole to Subjects:
Use RoleBinding or ClusterRoleBinding to associate the Role or ClusterRole with specific users, groups, or service accounts.
Example:
kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-pods namespace: default subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
Detailed Steps for RBAC Setup
Create a Role/ClusterRole:
- Define what actions are permitted on which resources.
Create a RoleBinding/ClusterRoleBinding:
- Link the Role/ClusterRole to a subject (user, group, or service account).
Test the RBAC Configuration:
Verify the permissions using
kubectl auth can-i
command.Example: kubectl auth can-i get pods --namespace=default
Use Cases of RBAC
Namespace Isolation: Assign specific permissions to users within a namespace without affecting other namespaces.
Cluster Administration: Use ClusterRoles and ClusterRoleBindings for cluster-wide administrative tasks.
Service Account Permissions: Limit the access of service accounts to only the necessary resources and actions.
RBAC is a powerful mechanism in Kubernetes that provides fine-grained access control, enhancing security and ensuring proper governance in a Kubernetes environment.
Subscribe to my newsletter
Read articles from Harish Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harish Sharma
Harish Sharma
Devops Engineer.