✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes Authorization and RBAC

Kubernetes Authorization and RBAC manage access to cluster resources using role-based policies for secure, granular permissions.

Kubernetes Authorization and RBAC is the mechanism that controls access to the Kubernetes API and resources by defining who can perform what actions within a cluster. It ensures that only authorized users, groups, or service accounts can execute specific operations, thereby enforcing security boundaries and minimizing the risk of unauthorized access or unintended changes to cluster state. RBAC (Role-Based Access Control) is the primary authorization mode in Kubernetes, which uses roles and role bindings to grant permissions based on roles assigned to subjects.


Authorization in Kubernetes

Authorization is the process of determining if a user or service account has permission to perform a given action on a Kubernetes resource after authentication has been established. Kubernetes supports multiple authorization plugins, each providing different models of access control:

  • Node: Authorizes kubelet requests based on node identity.
  • ABAC (Attribute-Based Access Control): Uses JSON policies based on user attributes.
  • Webhook: Delegates authorization decisions to an external REST service.
  • AlwaysAllow: Permissive mode that allows all requests.
  • AlwaysDeny: Denies all requests.
  • RBAC (Role-Based Access Control): Grants permissions through roles assigned to users or service accounts.

RBAC is the most widely used and recommended method because of its flexibility and granularity.


Role-Based Access Control (RBAC)

RBAC in Kubernetes provides fine-grained control over user permissions by assigning roles that define a set of allowed actions on resources within namespaces or the entire cluster.

Core RBAC Concepts

  • Role: Defines a set of permissions (rules) within a specific namespace. Roles specify which API groups, resources, and verbs (actions) are allowed.
  • ClusterRole: Similar to Role but applies cluster-wide or to non-namespaced resources.
  • RoleBinding: Grants the permissions defined in a Role to a user, group, or service account within a namespace.
  • ClusterRoleBinding: Grants ClusterRole permissions to subjects cluster-wide.

RBAC Components and Structure

  • Rules: Each rule consists of API groups, resources, resource names (optional), and verbs. Verbs represent actions such as get, list, create, update, delete, watch, and patch.
  • Subjects: Entities (users, groups, or service accounts) that are granted permissions through role bindings.
  • Namespace Scope: Roles and RoleBindings are namespace-scoped; ClusterRoles and ClusterRoleBindings are cluster-scoped.

RBAC Example

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: development
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: development
subjects:
- kind: User
  name: jane-doe
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

In this example, user jane-doe is granted read-only access to pods within the development namespace.


RBAC Evaluation Process

When a request is made to the Kubernetes API server, the authorization module evaluates if the request is allowed based on the following criteria:

  1. Authentication: Identifies the user or service account making the request.
  2. Authorization: Checks if the authenticated identity has permissions by matching against existing Roles/ClusterRoles and their bindings.
  3. Decision: The request is either allowed or denied.

The authorization is based on the request attributes including the user, requested verb, resource, namespace, and subresource.


Best Practices for Kubernetes Authorization and RBAC

  • Principle of Least Privilege: Grant only the minimum permissions necessary for users or applications to function.
  • Use Namespaces to Isolate Resources: Apply Roles and RoleBindings within namespaces to limit scope.
  • Prefer ClusterRoles for Common Permissions: Define reusable ClusterRoles for permissions that apply across namespaces.
  • Audit and Review Permissions Regularly: Continuously monitor and review RBAC policies to detect excessive or unused permissions.
  • Use Service Accounts for Automation: Assign specific permissions to service accounts used by applications or controllers.
  • Enable RBAC Enforcement: RBAC must be enabled on the API server via the --authorization-mode=RBAC flag for the rules to take effect.

Additional Authorization Features

  • Aggregation of ClusterRoles: Allows combining multiple ClusterRoles into a single logical role via label selectors for easier management.
  • Impersonation: Certain users can impersonate others if granted permission, useful for administrative actions.
  • Admission Controllers: Work alongside RBAC to enforce policies during API requests beyond authorization, such as resource quotas and security contexts.

Troubleshooting and Debugging RBAC

To debug RBAC permission issues, Kubernetes provides commands and tools:

  • Use kubectl auth can-i to check if a user or service account can perform a specific action.
kubectl auth can-i create pods --namespace=development --as=jane-doe
  • Check RoleBindings and ClusterRoleBindings to understand assigned permissions.
  • Enable audit logging on the API server to track authorization decisions.
  • Review error messages in API server logs related to authorization denials.

Kubernetes Authorization and RBAC form the backbone of access control in Kubernetes environments, enabling secure multi-tenant cluster operation with precise control over resource access and actions.