Sub topic 18
Authentication, Authorization, and RBAC Overview
This chapter assumes you know the API server's role as the front door, the difference between Pods and Deployments, and how namespaces and labels work. It ends with a short look at how Kubernetes is extended.
How a request is checked
Every request to the API server, from kubectl or from a controller, passes through a fixed sequence of checks before anything is stored in etcd.
- Authentication identifies the caller. Common methods are client certificates, bearer tokens, and OpenID Connect. Kubernetes has no user objects for people. It trusts the identity the authenticator reports.
- Authorization decides whether that identity may perform the action. A failed check returns HTTP 403.
- Admission control can validate or modify the object, for example by applying defaults or enforcing policy.
Processes running inside Pods authenticate as a ServiceAccount, which is a namespaced identity that is an actual API object.
RBAC
Role-based access control (RBAC) is the usual authorizer. Access is denied by default, and rules only add permissions. Four objects make it work:
| Object | Scope | Purpose |
|---|---|---|
| Role | Namespace | Lists allowed verbs on resources |
| ClusterRole | Cluster | Same, for cluster-wide or non-namespaced resources |
| RoleBinding | Namespace | Grants a Role to users, groups, or ServiceAccounts |
| ClusterRoleBinding | Cluster | Grants a ClusterRole everywhere |
This Role and binding let the user jane read Pods in the default namespace:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: default
name: read-pods
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioTest the result with impersonation:
kubectl auth can-i list pods --as jane -n default
kubectl auth can-i delete pods --as jane -n defaultThe first command prints yes and the second prints no.
Custom resources and operators
Kubernetes is extensible because the API server serves any resource type registered with it. A CustomResourceDefinition (CRD) adds a new kind to the API:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: backups.example.com
spec:
group: example.com
scope: Namespaced
names:
plural: backups
singular: backup
kind: Backup
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
schedule:
type: stringAfter you apply it, kubectl get backups works. Objects are stored in etcd, and RBAC and authentication apply to them like any built-in resource.
A CRD alone only stores data. An operator pairs a CRD with a custom controller that runs the same reconciliation loop you saw earlier. It watches Backup objects and acts to make reality match each one. Operators encode operational knowledge, such as how to back up or upgrade a database.
Exercise
A user authenticates successfully but gets a 403 when listing Pods. Which stage rejected the request, and what would you create to fix it?
Show answer
Authorization rejected it, because authentication succeeded. Create a Role (or ClusterRole) that allows the list verb on pods, then bind it to the user with a RoleBinding (or ClusterRoleBinding).