Archive · Kubernetes

RBAC and Service Accounts

Grant people and workloads only the Kubernetes permissions they need.

1 min read

  • Kubernetes 1.22
  • rbac.authorization.k8s.io/v1

RBAC has four objects: Role and ClusterRole list permissions; RoleBinding and ClusterRoleBinding grant them to users, groups or service accounts.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: { name: pod-reader, namespace: dev }
rules:
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: { name: read-pods, namespace: dev }
subjects:
  - kind: ServiceAccount
    name: ci
    namespace: dev
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io
kubectl auth can-i list pods -n dev --as system:serviceaccount:dev:ci
kubectl auth can-i --list -n dev

Avoid cluster-admin for applications. Each workload should use its own service account, and automountServiceAccountToken: false for pods that never call the API. Since 1.21, tokens are time-limited projected tokens by default.