## Security & RBAC in Kubernetes
Security in Kubernetes is multifaceted, covering authentication, authorization, network controls, and workload isolation. Role-Based Access Control (RBAC) is the primary authorization mechanism, defining what authenticated users and processes can do.
## Authentication and Authorization
Authentication verifies the identity of a user or process (e.g., client certificates, service account tokens, OIDC). Once authenticated, Authorization determines if the identity is permitted to perform a requested action on a resource. The Kubernetes API server handles both.
## Role-Based Access Control (RBAC)
RBAC uses four key objects:
Use `kubectl auth can-i <verb> <resource> --namespace <namespace>` to check permissions.
## Service Accounts
Service Accounts provide an identity for processes running in pods. Each namespace has a default service account. Pods are automatically assigned the `default` service account of their namespace unless specified otherwise. A token for the service account is mounted into the pod at `/var/run/secrets/kubernetes.io/serviceaccount/token`. You can disable this with `automountServiceAccountToken: false` in the pod or service account specification.
## Secrets
Secrets are used to store sensitive information like passwords, OAuth tokens, and SSH keys. They are Base64 encoded, not encrypted by default at rest (unless configured with an encryption provider). Secrets can be mounted as data volumes or exposed as environment variables within pods. Common types include `Opaque`, `kubernetes.io/dockerconfigjson`, and `kubernetes.io/tls`.
## Network Policies
Network Policies specify how groups of pods are allowed to communicate with each other and with external network endpoints. They define ingress (incoming) and egress (outgoing) rules based on pod selectors and namespace selectors. Network Policies are enforced by a Network Policy Controller, which is typically part of the Container Network Interface (CNI) plugin (e.g., Calico, Cilium).
## Pod Security Standards (PSS)
Pod Security Standards (PSS) replaced Pod Security Policies (PSPs) to enforce security best practices for pods. PSS defines three levels:
PSS are enforced by the PodSecurity admission controller, which can be configured to `enforce`, `audit`, or `warn` based on security levels for specific namespaces using labels like `pod-security.kubernetes.io/enforce: restricted`.