Skip to main content

☸️ Secure Execution of Containers in Kubernetes Clusters

🌟 Objective​

Ensure that the execution of containers in Kubernetes environments - applications, pipelines, sidecars or agents - takes place with explicit restrictions, technical enforcement and verifiable evidence, reducing risks such as:

  • Execution with excessive privileges;
  • Escalation of permissions at node level;
  • Undue access to secrets, volumes or the network;
  • Executions outside the organisational baseline.

Kubernetes provides mechanisms, not guarantees. Execution security depends on correct configuration, active enforcement and continuous verification.


🧬 What secure execution in Kubernetes means (in SbD-ToE)​

In the SbD-ToE model, running containers securely in Kubernetes implies:

  • Declaring execution restrictions (securityContext, PSA);
  • Imposing those restrictions technically (admission control);
  • Verifying that the real state of the pod matches what is expected;
  • Maintaining traceability between decision, configuration and execution.

⚠️ A pod may be accepted by the cluster and still violate security expectations if the effective state is not verified.


⚠️ Configuration is not a guarantee of security​

It is essential to avoid the implicit equivalence:

  • ✔️ securityContext defined
  • ❌ automatically secure execution

There are three distinct levels:

  1. Platform capability What Kubernetes allows to be configured.
  2. Declared configuration What is in the manifests.
  3. Effective execution state What the pod actually applies at runtime.

This file explicitly addresses all three levels, not only the second.


📘 Applicable security mechanisms​

MechanismTechnical functionInherent limitation
securityContextDefines UID, capabilities, privilegesMay be bypassed if poorly applied
Pod Security StandardsPod acceptance baselineDoes not validate post-admission state
Kyverno / OPA (Gatekeeper)Blocking of out-of-policy configurationsDoes not assess contextual risk
NetworkPoliciesTraffic isolationRequires complete coverage
RuntimeClass / SandboxingReinforced isolationDoes not eliminate logical risk
Admission WebhooksPre-execution validationActs before runtime

🛠️ How to apply it correctly​

1️⃣ Declare mandatory minimum restrictions​

All pods must explicitly declare:

securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]

These declarations are necessary, but not sufficient.


2️⃣ Impose technical enforcement​

  • Enable Pod Security Admission (restricted or baseline);

  • Apply policies via Kyverno or OPA to:

    • prohibit privileged: true;
    • block images from outside an authorised origin;
    • require traceability labels and metadata.

Enforcement reduces human error; it does not replace governance.


3️⃣ Verify the effective state at runtime​

For critical workloads (L2/L3), it is necessary to:

  • Confirm the effective UID of the process;
  • Validate real mounts and permissions;
  • Verify that the seccomp / AppArmor profile is active;
  • Detect deviations (drift) after admission;
  • Record evidence for audit.

Without this verification, security is merely assumed.


📂 Where to configure, version and observe​

  • Manifests in versioned Git repositories;
  • Policies as code via GitOps;
  • Admission and rejection logs preserved;
  • Compliance and deviation metrics monitored;
  • Execution evidence associated with the workload.

✅ Good practices​

  • Treat Kubernetes as an execution platform, not as an autonomous security barrier;

  • Clearly separate:

    • configuration,
    • enforcement,
    • verification;
  • Apply policies progressively (audit → enforce);

  • Review permissions after cluster upgrades;

  • Document exceptions as temporary and traceable decisions;

  • Assume that configuration without observation is a hypothesis, not a fact.


📎 Cross-references​

DocumentRelation to execution in Kubernetes
02-runners-isolamento.mdRunners as ephemeral pods
04-hardening-containers.mdRestrictions applied to the runtime
05-policies-runtime-opa.mdTechnical enforcement at admission
09-riscos-processo-imagens.mdSeparation between intention and real state
15-aplicacao-lifecycle.mdIntegration into the SSDLC

☁️ Kubernetes executes what it is asked to. Security exists only when what is executed matches what was consciously decided.