Terminal prompt logo for Ali Haidry — alihaidry.dev~/AHAli Haidryalihaidry.dev
Phase 5 of 6Published

Runtime and cluster security

Assume someone gets a foothold. Make that foothold small: lock the control plane, check what is admitted, harden the pods, control what they can talk to, and watch what they do.

KubernetesGatekeeperCiliumFalcoTrivy OperatorUpdated Nov 2026

Goal

What this phase delivers

Phases 1 to 4 made the app correct, repeatable and automatically deployed. This phase asks what happens when something goes wrong or someone hostile gets in. A stolen kubeconfig, a vulnerable dependency or a bad image should each be stopped by a different layer.

  • Network hardeningThe API server and Key Vault answer only listed networks.Live
  • Admission controlGatekeeper checks every pod against seven policies, all enforced with deny.Live
  • Pod hardeningRead-only root filesystem, no extra capabilities, images pinned by digest.Live
  • Egress controlEach workload may reach only what it needs, and no API token is mounted.Live
  • Runtime detectionFalco watches system calls; Trivy Operator keeps vulnerability reports.Live

Steps and commands

Run these in dev first. Every step has a check that proves it worked, so you are never guessing.

  1. Restrict who can reach the API server and Key Vault

    Add authorized IP ranges and a Key Vault firewall in Terraform, then read the plan before you apply it.

    $ terraform init
    $ terraform plan -out p5.tfplan
    $ terraform show p5.tfplan | grep -E "will be|must be|forces replacement"
    $ terraform apply p5.tfplan
    Check: kubectl get nodes from a network that is not on the list times out; yours still works.
  2. Install Gatekeeper and roll policies out slowly

    Each constraint moves through dryrun, warn and deny, and only after its audit list is empty.

    $ kubectl get constrainttemplates
    $ kubectl get constraints
    $ kubectl describe k8spspreadonlyrootfilesystem | sed -n '/Violations/,$p'
    Check: the audit shows zero violations before you raise the enforcement level.
  3. Prove a policy both ways

    Reject a pod that breaks the rule, for the reason you expect, and admit the real deployment. The test pod must be compliant in every other respect.

    $ kubectl -n paktech-chat-dev apply --dry-run=server -f test-pod-tag-only.yaml
    # Forbidden: uses an image without a digest
    $ kubectl -n paktech-chat-dev apply --dry-run=server -f test-pod-digest.yaml
    Check: the tag-only pod is rejected with the digest message and the digest pod is admitted.
  4. Turn on egress policies in dev

    Take a baseline of what every pod can reach, enable the flag, then run the same test again and compare.

    $ bash scripts/egress-test.sh paktech-chat-dev     # before
    $ kubectl get networkpolicy -n paktech-chat-dev
    $ bash scripts/egress-test.sh paktech-chat-dev     # after
    Check: the metadata address and port 80 flip from OPEN to BLOCKED; postgres, redis and the app still work.
  5. Stop mounting the Kubernetes API token

    None of the app pods call the Kubernetes API, so the default service account token is pure risk.

    $ kubectl exec -n paktech-chat-dev deploy/api -- ls /var/run/secrets/kubernetes.io/serviceaccount
    # ls: cannot access ... No such file or directory
    Check: the pods start, secrets still mount, and the folder is gone.
  6. Check Falco and the vulnerability reports

    Run a throwaway pod that reads a sensitive file, then read what Falco and Trivy Operator saw.

    $ kubectl get pods -n falco -o wide
    $ kubectl logs -n falco -l app.kubernetes.io/name=falco --tail=20
    $ kubectl get vulnerabilityreports -n paktech-chat-dev -o wide
    Check: Falco logs a sensitive-file event with pod, namespace and image, and the reports list your images.

Workflow

Every control ships the same way: behind a flag that is off by default, so merging changes nothing until you enable it for one environment.

  1. STEP 1Flag offMerge, nothing changes
  2. STEP 2DevBaseline test first
  3. STEP 3VerifyAfter-test and app check
  4. STEP 4StagingSame test, same check
  5. STEP 5ProdManual sync

Rollback: Set the flag back to false (or lower the constraint to warn), merge, and Argo CD removes the policies.

Diagram

Five layers: API server, admission, pod hardening, network policies, runtime detection
Figure: the five layers. Each one assumes the one above it failed.

Issues met

These are real, from the run. The surprises are the useful part.

The API server was still reachable after the egress policies
Cause
Cilium evaluates policy on the translated backend address, so excluding the service range never matched.
Fix
Stop mounting the service account token (automountServiceAccountToken: false), so a reachable API server is useless to a pod.
Trivy Operator reported HIGH findings in my own images
Cause
Build tooling the runtime never uses: npm, corepack and yarn in the web image; setuptools and wheel in the api image.
Fix
Remove the tooling in the final image, in the same layer as the install. The web image went from 10 HIGH to none.
My "tooling removed" check printed success while Docker was stopped
Cause
The check ended in || echo ok, so it passed when the command never ran.
Fix
Run the command first, save its output, then grep the output.
A test pod was rejected by the wrong policy
Cause
A native admission policy fires before the webhook, so a pod without a security context never reached Gatekeeper.
Fix
Make the test pod compliant in every other respect, so only the rule under test can reject it.

Trade-offs

Authorized IP ranges or a private cluster
I chose: Authorized IP ranges

Why: It is cheap, quick to roll back and needs no VPN or jump host for a one-person lab.

It costs: The API endpoint stays public, so the allow-list must follow your IP when it changes.

Webhook fails open or fails closed
I chose: Fail open

Why: If the policy pod is down, the cluster can still be repaired.

It costs: A short window with no admission checks, covered by audits and the other layers.

Interview questions

How do you secure a Kubernetes cluster beyond the defaults?
In layers: restrict the control plane, enforce policy at admission, harden the pods, limit traffic in both directions, pin images by digest, and watch at runtime. Each layer assumes another failed.
What does a default-deny egress policy break first?
DNS. Allow kube-dns explicitly, then the specific upstreams, and remember the metadata address and private ranges when you allow the internet.
How do you prove a network control works?
Run a before-and-after test from inside the pods, including paths that must stay open and paths that must close. Controls without a failing test are assumptions.