Simple definition#
Kubernetes security is defence in depth across four layers — the 4Cs: Cloud (accounts, networks), Cluster (API access, policies), Container (images, runtime settings) and Code (dependencies, secrets). A weakness in an outer layer undermines everything inside it.
flowchart LR
subgraph Cloud["Cloud / data centre"]
subgraph Cluster["Cluster"]
subgraph Container["Container"]
Code["Code"]
end
end
end
Security checkpoints on every deployment#
- Authenticate — who is calling? (OIDC users, client certificates, service-account tokens)
- Authorise — may they do this? (RBAC)
- Mutating admission — set safe defaults (labels, security context, image digests).
- Validating admission — reject unsafe specs (Pod Security Admission, Kyverno, OPA Gatekeeper, ValidatingAdmissionPolicy in CEL), verify image signatures.
- Persist to etcd (encrypted at rest).
- Run with least privilege; runtime detection catches what admission cannot.
Identity and access#
RBAC grants verbs on resources:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: { name: deployer, namespace: shop }
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: { name: ci-deployer, namespace: shop }
subjects: [{ kind: ServiceAccount, name: argocd-app, namespace: argocd }]
roleRef: { kind: Role, name: deployer, apiGroup: rbac.authorization.k8s.io }
- Humans authenticate through the corporate IdP (OIDC); avoid long-lived kubeconfigs and shared admin accounts.
- Use Roles in namespaces; reserve ClusterRoles for platform components. Never grant
cluster-adminto apps or CI. - Service accounts give Pods an identity; tokens are short-lived and audience-bound (projected tokens). Disable automount where not needed.
- Workload identity (EKS Pod Identity/IRSA, Azure Workload Identity, GKE Workload Identity) maps service accounts to cloud IAM roles — no cloud keys in Secrets.
Pod security#
Pod Security Standards, enforced by the built-in Pod Security Admission per namespace:
| Level | Meaning |
|---|---|
| Privileged | No restrictions (system components only) |
| Baseline | Blocks known privilege escalations (host namespaces, privileged containers) |
| Restricted | Hardened: non-root, drop all capabilities, no privilege escalation, seccomp RuntimeDefault |
apiVersion: v1
kind: Namespace
metadata:
name: shop
labels:
pod-security.kubernetes.io/enforce: restricted
A restricted container spec:
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: ["ALL"] }
seccompProfile: { type: RuntimeDefault }
User namespaces (GA in 1.36) map root inside a container to an unprivileged user on the host, reducing the impact of container escapes.
Policy as code#
| Engine | Language | Notes |
|---|---|---|
| ValidatingAdmissionPolicy | CEL, built in | No webhook to run; good for simple rules |
| Kyverno | YAML policies | Validate, mutate, generate, verify image signatures; CNCF graduated |
| OPA Gatekeeper | Rego | Very expressive; shared with other OPA use cases |
Typical rules: required labels and owners, no :latest tags, images only from approved registries and signed, resource requests required, no privileged Pods, Ingress/Route hostnames restricted per team.
Secrets#
- Kubernetes Secrets are only base64-encoded. Enable encryption at rest for etcd (ideally with a KMS provider) and restrict RBAC on Secrets.
- Prefer an external secret store — HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, GCP Secret Manager — synced with the External Secrets Operator or mounted with the Secrets Store CSI driver.
- Never commit secrets to Git; for GitOps use External Secrets references or encrypted secrets (SOPS, Sealed Secrets).
- Rotate automatically; prefer short-lived dynamic credentials.
Supply-chain security#
flowchart LR
SRC["Source<br/>signed commits, reviews"] --> BUILD["Build<br/>hermetic CI, SLSA provenance"]
BUILD --> SCAN["Scan<br/>SCA, SAST, image CVEs"]
SCAN --> SBOM["SBOM<br/>SPDX / CycloneDX"]
SBOM --> SIGN["Sign<br/>cosign / Sigstore"]
SIGN --> REG[("Registry<br/>image + attestations")]
REG --> ADM{"Admission<br/>verify signature,<br/>provenance, policy"}
ADM -->|"pass"| RUN["Run"]
ADM -->|"fail"| BLOCK["Block"]
- Minimal base images (distroless, Chainguard, Docker Hardened Images, UBI micro) reduce CVEs.
- SBOMs list every component; SLSA provenance proves how an artifact was built; cosign signs images and attestations; admission policies verify them.
- Deploy by digest, not tag.
Runtime security#
- Falco or Tetragon (eBPF) detect suspicious behaviour: shells in containers, unexpected network connections, writes to sensitive paths.
- Restrict egress with NetworkPolicies; audit-log the API server and ship logs to the SIEM.
- Node hardening: minimal, immutable OS images (Bottlerocket, Talos, Flatcar, RHCOS); auto-patching; no SSH.
Multi-tenancy#
| Model | Isolation | Use |
|---|---|---|
| Namespace per team + RBAC + quotas + policies + NetworkPolicies | Soft | Trusted internal teams |
| Virtual clusters (vCluster) | Medium | Teams needing their own API/CRDs |
| Dedicated node pools / sandboxed runtimes (gVisor, Kata) | Stronger | Untrusted or sensitive workloads |
| Separate clusters | Hard | Regulatory boundaries, very different trust levels |
Compliance and assessment#
- CIS Kubernetes Benchmark (kube-bench), NSA/CISA Kubernetes Hardening Guide, NIST SP 800-190 (container security).
- Continuous scanning of clusters and manifests (Trivy, Kubescape); policy reports from Kyverno/Gatekeeper.
Common mistakes#
Quick check#
What are the 4Cs? Cloud, Cluster, Container, Code — layers of defence where each depends on the outer ones.
What is the difference between authentication, authorisation and admission? Authentication establishes identity; authorisation (RBAC) checks permission for the verb and resource; admission validates or mutates the object itself against policy.
What does the restricted Pod Security Standard enforce?
Non-root, no privilege escalation, all capabilities dropped, seccomp RuntimeDefault, no host namespaces or privileged containers.
Why are Kubernetes Secrets not secure by default? They are only base64-encoded; you need etcd encryption at rest, tight RBAC and preferably an external secret store.
How do you ensure only trusted images run? Build with provenance, sign images with cosign, generate SBOMs, push by digest, and verify signatures and policies at admission (Kyverno/Gatekeeper).
What does runtime security add? Detection of malicious behaviour after admission — such as shells, unexpected connections or file changes — using eBPF tools like Falco or Tetragon.
Summary#
- Defend all 4Cs; every request passes authentication, RBAC and admission.
- Enforce Pod Security Standards (restricted) and policy as code (Kyverno, Gatekeeper, CEL).
- Keep secrets in external stores; encrypt etcd; use workload identity.
- Secure the supply chain: minimal images, SBOM, provenance, signatures, verification at admission.
- Detect at runtime; choose multi-tenancy isolation to match trust.
References#
- Kubernetes documentation, Security and Pod Security Standards.
- Kyverno, OPA Gatekeeper, Falco.
- Sigstore, SLSA.
- NSA/CISA, Kubernetes Hardening Guide.
- CIS, Kubernetes Benchmark.