Kubernetes in depth · Lesson 9 of 11 · 3 min read
Security: authentication, RBAC and admission
Authentication
Kubernetes has no user database. Humans authenticate with client certificates, OIDC tokens from an identity provider, or cloud mechanisms (EKS: IAM → access entries). Workloads authenticate with ServiceAccount tokens: since 1.24, pods get bound, time-limited projected tokens (audience-bound, auto-rotated) instead of long-lived Secret-based tokens. Disable mounting where not needed: automountServiceAccountToken: false.
RBAC
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: { name: deployer, namespace: shop }
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "update", "patch"]
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: { name: ci-deployer, namespace: shop }
subjects:
- { kind: ServiceAccount, name: ci, namespace: shop }
- { kind: Group, name: checkout-devs, apiGroup: rbac.authorization.k8s.io }
roleRef: { kind: Role, name: deployer, apiGroup: rbac.authorization.k8s.io }
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
- Role (namespaced) vs ClusterRole (cluster-wide or reusable). RoleBinding grants within a namespace (can reference a ClusterRole to reuse it); ClusterRoleBinding grants cluster-wide.
- RBAC is additive only — no deny rules.
- Verbs: get, list, watch, create, update, patch, delete, deletecollection; plus special ones like
escalate,bind,impersonate. Subresources:pods/exec,pods/log,deployments/scale. - Dangerous permissions:
secretsget/list,pods/exec, creating pods (can mount any ServiceAccount or hostPath),escalate/bind,nodes/proxy, wildcard*. - Built-in roles:
cluster-admin,admin,edit,view(aggregated). - Check with
kubectl auth can-i create deployments -n shop --as system:serviceaccount:shop:ciandkubectl auth can-i --list.
Admission control and Pod Security
- Pod Security Admission enforces the Pod Security Standards per namespace with labels:
privileged,baseline,restricted, in modesenforce,audit,warn:
metadata:
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/warn: restricted
- Policy engines (Gatekeeper/OPA, Kyverno) and built-in ValidatingAdmissionPolicy (CEL) cover custom rules: allowed registries, required labels, no
latesttags, resource limits required.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Pod securityContext
securityContext: # pod level
runAsNonRoot: true
runAsUser: 10001
fsGroup: 10001
seccompProfile: { type: RuntimeDefault }
containers:
- name: app
securityContext: # container level
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: ["ALL"] }
Audit logging
The API server can log every request (who, what, when, result) per an audit policy. On EKS, enable the audit control plane log type to CloudWatch — essential for incident investigation ("who deleted this Deployment?").