Container and Kubernetes security basics: images, pods and clusters


SaveAdd to Google
Illustration: stacked containers with a shield

At a glance

Audience
Developers, DevOps & IT
IT & security teams
Difficulty
Advanced
Time needed (minutes)
60 min
Last reviewed
Nov 22, 2026

What you need

  • Applications built as container images
  • Access to a Kubernetes cluster (managed or self-run) with kubectl
  • A container scanner such as Trivy (free)

Containers make it easy to ship software, and just as easy to ship a vulnerable library, a root process or a cluster that trusts every pod. Attackers who break into one container try to escape to the node, read secrets and move across the cluster. This guide covers the controls that stop each step: small images, locked-down pods and a cluster that denies by default.

📦

Small, scanned images

Less inside means less to patch and less to exploit.

🛡️

Restricted pods

No root, no privileges, a read-only file system.

🚧

A cluster that says no

Least-privilege RBAC and network policies.

1. Build small images and scan them

❌ A full operating system, running as root

FROM node:latest
COPY . /app
RUN npm install
CMD ["node", "/app/server.js"]

✅ A multi-stage build, a minimal base, a non-root user

FROM node:22-bookworm-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .

FROM gcr.io/distroless/nodejs22-debian12
WORKDIR /app
COPY --from=build /app /app
USER nonroot
CMD ["server.js"]
  • Pin base images to a version (and, in production, a digest), never latest.
  • Scan in CI and fail the build on serious, fixable flaws: trivy image registry.example.ca/app:1.4.2.
  • Rebuild images regularly, even without code changes, to pick up base image fixes.
  • Never bake secrets into an image: anyone who can pull the image can read every layer.

2. Lock down every pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
spec:
  template:
    spec:
      automountServiceAccountToken: false
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: api
          image: registry.example.ca/api@sha256:…
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
          resources:
            limits:
              memory: "512Mi"

Then make Kubernetes enforce it. The built-in Pod Security Admission applies the restricted Pod Security Standard to a whole namespace with one label:

kubectl label namespace production \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/warn=restricted

❌ Never in production

  • privileged: true, hostNetwork, hostPID or a hostPath mount of the node's file system
  • Mounting the container runtime socket into a pod: it gives control of the whole node

3. Least privilege in the cluster

  1. RBAC per workload. Give each application its own service account with only the permissions it needs, in its own namespace. Avoid cluster-admin bindings and wildcards ("*") in roles.
  2. People sign in through your identity provider with short-lived credentials and multi-factor authentication, never shared kubeconfig files with static tokens.
  3. Network policies deny by default. Start each namespace with a policy that blocks all traffic, then allow only the flows you need. Your network plugin must support network policies.
  4. Keep the API server private, reachable only from your network or through your cloud provider's private endpoint.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: production
spec:
  podSelector: {}
  policyTypes: ["Ingress", "Egress"]

4. Handle secrets properly

  • Kubernetes Secrets are only base64-encoded. Turn on encryption at rest for them (managed clusters usually offer it with your cloud key service).
  • Prefer an external secrets manager (cloud secret managers, HashiCorp Vault) synced with a tool such as External Secrets Operator.
  • Limit who can get or list secrets: in RBAC, that permission is as powerful as the secrets themselves.

5. Keep it current and watch it

  • Stay on a supported Kubernetes minor version; each is supported for about 14 months upstream.
  • Turn on API server audit logs and send them to your logging system.
  • Add runtime detection (for example Falco) to alert on shells in containers and unexpected processes.
  • Check clusters against the free CIS Kubernetes Benchmark, with a tool such as kube-bench.

Assume one container will be compromised. Design so that is all the attacker gets.

Container and cluster checklist

  • Minimal, pinned base images, scanned in CI and rebuilt regularly
  • Non-root, read-only, no extra capabilities, no privilege escalation
  • Restricted Pod Security Standard enforced on production namespaces
  • One service account per workload; no default token mounted
  • Default-deny network policies
  • Secrets encrypted at rest or kept in an external manager
  • Supported Kubernetes version; audit logs and runtime alerts on

Keep learning

Next: secure the pipeline that builds your images, and lock down your cloud account.

All guides for developers, DevOps and IT →

  • Containers
  • Kubernetes
  • Docker
  • DevOps

Sources

Editor's Picks

The best of cybersecuritynews.ca, delivered to you weekly.

On Display
Linux

Linux KVM Flaw Lets Guest VMs Reach Host Memory on AMD Servers With Nested Virtualization

Sonic6

SonicWall Tells Customers to Pull Gen 6 Firewalls Off the Internet

Staples Canada says attackers accessed customer records on its Staples Print website between November 1 and 5, 2026.

Staples Canada Confirms Breach of Print Website, Customer Contact Details and Encrypted Passwords Exposed