The source doc has no Part 1 heading here; these sections are grouped as Part 1.
PART 1: CORE THEORY
1. The Kubernetes Security Model
Kubernetes security is built on three pillars that are independent and must all be configured correctly. A failure in any one breaks the whole chain.
The Three Pillars
text
┌─────────────────────────────────────────────────────────────┐
│ KUBERNETES SECURITY MODEL │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AUTHENTICATION (AuthN) │ │
│ │ "Who are you?" │ │
│ │ → Certificates, tokens, OIDC, webhooks │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AUTHORIZATION (AuthZ) │ │
│ │ "What can you do?" │ │
│ │ → RBAC, ABAC, Node, Webhook │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ ADMISSION CONTROL │ │
│ │ "Should we allow this?" │ │
│ │ → Mutating → Validating webhooks/policies │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Key Principles
| Principle | Meaning |
| Zero-Trust | No implicit trust; verify everything |
| Least Privilege | Minimum permissions necessary |
| Defense in Depth | Multiple independent layers |
| Separation of Duties | No single role has all power |
2. Authentication
Authentication verifies the identity of the caller. The API server supports several methods.
Authentication Methods
| Method | Use Case |
| X.509 Client Certificates | Human admins, kubelet |
| ServiceAccount Tokens | Workloads inside the cluster |
| OIDC | Enterprise SSO (Okta, Azure AD, Google) |
| Webhook Token Authentication | External auth service |
| Authenticating Proxy | Header-based (for proxies) |
| Bootstrap Tokens | Node bootstrap |
| Static Token File | Legacy (deprecated) |
X.509 Client Certificates
The most common method for admins and kubelets. The Common Name (CN) becomes the username; Organization (O) becomes the group.
text
Subject: CN=alice, O=developers, O=engineering
│ │
│ └── Groups: [developers, engineering]
└── Username: alice
The API server verifies the certificate is signed by its CA.
ServiceAccount Tokens
Every Pod can have a ServiceAccount. Kubernetes mints a JWT token for it.
Pre-1.21: Static, never-expiring tokens.
1.21+ (projected tokens): Time-limited, audience-bound, auto-rotated.
yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
serviceAccountName: my-sa
containers:
- name: app
image: myapp
volumeMounts:
- name: token
mountPath: /var/run/secrets/kubernetes.io/serviceaccount
readOnly: true
volumes:
- name: token
projected:
sources:
- serviceAccountToken:
path: token
expirationSeconds: 3600
audience: https://kubernetes.default.svc
The token is mounted at /var/run/secrets/kubernetes.io/serviceaccount/token.
Key fact: By default, every Pod gets a ServiceAccount token. This is a security concern for Pods that don't need cluster API access. Disable with automountServiceAccountToken: false.
OIDC (OpenID Connect)
Enterprise SSO integration. The API server validates tokens from an OIDC provider.
yaml
# kube-apiserver flags
--oidc-issuer-url=https://accounts.google.com
--oidc-client-id=kubernetes
--oidc-username-claim=email
--oidc-groups-claim=groups
Anonymous Requests
By default, unauthenticated requests are treated as system:anonymous (group system:unauthenticated). This is normally denied by RBAC, but can be explicitly allowed.
Security tip: Disable anonymous auth unless you have a specific reason:
text
--anonymous-auth=false
Username Conventions
| Prefix | Meaning |
system: | Kubernetes internal |
system:serviceaccount:<ns>:<name> | ServiceAccount |
system:node:<name> | Kubelet |
system:apiserver | API server |
| (no prefix) | External user |
Authentication Best Practices
| Practice | Why |
| Use OIDC for humans | Centralized, auditable |
| Rotate client certificates | Short-lived, limited blast radius |
| Disable static tokens | Deprecated, hard to rotate |
| Disable anonymous auth | Prevent unauthorized access |
| Use bound ServiceAccount tokens | Auto-rotated, audience-scoped |
| Audit all authentication | Detect anomalies |
3. Authorization
Once authenticated, Kubernetes checks if the user is authorized to perform the requested action.
Authorization Modes
| Mode | Description | Use Case |
| RBAC | Role-Based Access Control | Default, recommended |
| ABAC | Attribute-Based Access Control | Legacy, file-based |
| Node | Node authorizer | Kubelet authorization |
| Webhook | External authorization | Custom policies |
| AlwaysAllow | Everything is allowed | ❌ Never in production |
| AlwaysDeny | Everything is denied | Testing |
Multiple Authorization Modes
The API server checks modes in order and uses the first one that makes a decision. If RBAC denies, but Webhook allows, the request is allowed (Webhook is checked last).
Common configuration:
text
--authorization-mode=Node,RBAC
The RBAC Verb Set
| Verb | Action |
| get | Read one resource |
| list | List resources |
| watch | Watch for changes |
| create | Create new |
| update | Update existing (full) |
| patch | Update (partial) |
| delete | Delete one |
| deletecollection | Delete many |
The RBAC Resource Set
| Resource | Example |
| pods | Core Pod resources |
| pods/log | Pod subresource (logs) |
| pods/exec | Pod subresource (exec) |
| deployments | Apps group |
| secrets | Sensitive |
| nodes | Cluster-level |
| configmaps | Non-sensitive config |
4. RBAC
RBAC (Role-Based Access Control) is the default and recommended authorization mode in Kubernetes. It grants permissions via Roles and Bindings.
The Four Objects
| Object | Scope | Purpose |
| Role | Namespace | Permissions within a namespace |
| ClusterRole | Cluster | Permissions cluster-wide |
| RoleBinding | Namespace | Bind Role or ClusterRole to subjects |
| ClusterRoleBinding | Cluster | Bind ClusterRole to subjects cluster-wide |
Role (Namespace-Scoped)
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
ClusterRole (Cluster-Scoped)
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
RoleBinding
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: alice-pod-reader
namespace: production
subjects:
- kind: User
name: alice
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
ClusterRoleBinding
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: alice-node-reader
subjects:
- kind: User
name: alice
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io
Subjects
| Kind | Example |
| User | alice, bob@example.com |
| Group | developers, system:masters |
| ServiceAccount | default/my-sa (namespace/name) |
The Secret to RBAC: Bindings vs Permissions
RoleBindings cannot grant permissions the Role doesn't have. You can bind a ClusterRole to a namespace using a RoleBinding — this grants the ClusterRole's permissions within that namespace only.
Example: Bind cluster-admin ClusterRole to a RoleBinding in namespace production → the user has admin permissions within production only.
Default ClusterRoles
Kubernetes ships with built-in ClusterRoles:
| ClusterRole | Purpose |
| cluster-admin | Full cluster access |
| admin | Namespace admin (most resources) |
| edit | Edit most resources (no RBAC) |
| view | Read-only (no Secrets) |
Default ServiceAccounts
Each namespace has a default ServiceAccount. Pods that don't specify one use it.
Security tip: Don't grant permissions to default; create per-workload ServiceAccounts.
RBAC Decision Logic
When a request comes in:
- Gather all Roles/ClusterRoles bound to the user via RoleBindings/ClusterRoleBindings.
- Check if any rule allows the request.
- If yes → allow. If no → deny.
Key insight: RBAC is additive. There are no deny rules. You can only grant permissions, not remove them.
RBAC Verbs and Resources (Common Combinations)
| Need | Role Rule |
| Read Pods | resources: ["pods"], verbs: ["get", "list", "watch"] |
| Read Pod logs | resources: ["pods/log"], verbs: ["get"] |
| Exec into Pods | resources: ["pods/exec"], verbs: ["create"] |
| Port forward | resources: ["pods/portforward"], verbs: ["create"] |
| List Secrets | resources: ["secrets"], verbs: ["list"] |
| Deploy | resources: ["deployments"], verbs: ["create", "update"] |
| Delete namespace | resources: ["namespaces"], verbs: ["delete"] |
RBAC Anti-Patterns
| Anti-Pattern | Risk |
| cluster-admin for humans | Full cluster compromise |
Wildcard verbs (*) | Unintended permissions |
Wildcard resources (*) | Unintended scope |
Binding cluster-admin via RoleBinding | Namespace-wide admin |
| default ServiceAccount with permissions | Every Pod gets it |
| No RBAC auditing | Drift, unused perms |
RBAC Verification
bash
# Check what you can do
kubectl auth can-i --list
# Check specific permission
kubectl auth can-i create pods
kubectl auth can-i delete deployments -n production
# Check as another user
kubectl auth can-i list secrets --as=system:serviceaccount:default:my-sa
kubectl auth can-i list secrets --as=alice
# Check cluster-wide
kubectl auth can-i list pods --all-namespaces
The kubectl who-can Plugin
bash
# Install krew plugin
kubectl krew install who-can
# Check who can do something
kubectl who-can list secrets
kubectl who-can delete pods -n production
5. ServiceAccounts
A ServiceAccount is an identity for processes running in Pods. It's how Pods authenticate to the Kubernetes API.
Creating a ServiceAccount
yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app-sa
namespace: production
bash
kubectl create serviceaccount my-app-sa -n production
Using a ServiceAccount in a Pod
yaml
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
serviceAccountName: my-app-sa
automountServiceAccountToken: false # Best practice
containers:
- name: app
image: my-app:1.0
The ServiceAccount Token
Pre-1.21 (legacy): Static Secret-backed tokens.
1.21+ (projected): Time-limited, audience-bound tokens.
The token is mounted at:
text
/var/run/secrets/kubernetes.io/serviceaccount/token
/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
/var/run/secrets/kubernetes.io/serviceaccount/namespace
Disabling Auto-Mounting
By default, every Pod gets a ServiceAccount token. If your Pod doesn't need to call the Kubernetes API:
yaml
spec:
automountServiceAccountToken: false
Or disable at the ServiceAccount level:
yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: no-api-sa
automountServiceAccountToken: false
Why this matters: A leaked token allows an attacker to make API calls with the Pod's identity. If the Pod doesn't need the API, don't give it a token.
Binding a ServiceAccount to RBAC
ServiceAccounts can be subjects in RoleBindings:
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: my-app-pod-reader
namespace: production
subjects:
- kind: ServiceAccount
name: my-app-sa
namespace: production
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Workload Identity (Cloud)
ServiceAccounts are also used for cloud identity:
- AWS: IRSA (IAM Roles for Service Accounts)
- GCP: Workload Identity
- Azure: Azure AD Workload Identity
These allow Pods to assume cloud IAM roles without static credentials.
Best Practices
| Practice | Why |
| One ServiceAccount per workload | Least privilege |
| Disable auto-mount when not needed | Reduce attack surface |
| Use projected tokens | Auto-rotated |
| Bind to minimal RBAC | Least privilege |
| Audit ServiceAccount usage | Detect anomalies |
Never use default | Shared across namespace |
| Use workload identity | No static cloud credentials |
1:1 copy of the class Google Doc, code in code blocks.