Ravindra BagaleCourses & study guides Track your progress

Guides

Kubernetes Security Basics for Defenders

Kubernetes security basics for defenders: control who can change the cluster (RBAC), isolate workloads (namespaces and network policy), keep secrets out of images, run containers with least privilege, and watch the API audit log — so one bad pod does not become a whole-cluster incident.

Friends! kubectl power and an exposed dashboard = classic foot-gun. Today beginner K8s defence: RBAC, network policy, secrets, non-root, admission themes. No exploit recipes. Own cluster / authorised lab only — do not scan random clusters on the public internet.

Quick answer

Kubernetes security — vertical starter checklist:

  1. Never leave the API server or dashboard reachable from the open internet without strong auth.
  2. Avoid standing cluster-admin for humans; prefer namespaced roles + MFA on the identity provider.
  3. Separate prod / non-prod namespaces; default-deny network policy where the CNI supports it.
  4. Mount secrets as short-lived volumes — not baked into images or ConfigMaps with passwords.
  5. Run as non-root; drop risky capabilities; set resource limits.
  6. Scan images in CI; pin digests when you can.
  7. Enable and ship API server audit logs to your SIEM.

Tiny mental model:

Identity → RBAC → Admission → Runtime (pod security) → Network → Audit
Exposed dashboard = “please crypto-mine me” era lessons
kubectl = production change control, not a toy

What do I need before this guide?

What are we protecting in a cluster?

Kubernetes security basics Cluster layers: RBAC and network policy around pods, secrets kept out of images, and admission controls before workloads run. Kubernetes cluster (defence layers) RBAC Least privilege No cluster-admin Network Policies Namespaces Workloads Non-root Secrets out pod

Cluster defence layers: RBAC, network policy and hardened workloads with secrets kept out of images — plus audit logs.

Layers beginners should name in interviews:

  1. Cluster identity — who can talk to the API server.
  2. RBAC — which verbs on which resources.
  3. Admission / policy — block privileged pods before they schedule (vendor tools vary).
  4. Workload hardening — non-root, read-only root FS themes, seccomp where supported.
  5. Network — east-west limits so a web pod cannot freely hit the metadata service or payroll DB.
  6. Secrets & supply chain — image provenance, SCA, secret managers.
  7. Detection — audit logs, runtime sensors, SIEM alerts on weird exec / secret reads.

Educational warning: practise on minikube / kind / a cloud sandbox you own. Do not “test RBAC” on a shared company prod cluster without change tickets — you can lock teams out or expose services.

Real incident: Tesla Kubernetes cryptojacking (2018)

Public reporting on a 2018 Tesla cloud incident described an unauthenticated Kubernetes dashboard exposure that attackers used to run cryptocurrency mining workloads. The lasting lesson for every student cluster: management UIs and API endpoints are crown jewels. Convenience “open for debugging” kills.

Takeaways (vertical):

  1. What happened — cluster console exposure leading to abusive workloads (mining) in public analyses.
  2. What went wrong (theme) — internet-facing admin surface without adequate authentication.
  3. Care-take — private control planes; SSO + MFA; no anonymous dashboard.
  4. Care-take — network policies and egress controls limit miner profit even after a foothold.
  5. Care-take — alert on sudden CPU / new Deployments in sensitive namespaces.
  6. Bonus parallel — Capital One–era cloud identity lessons: metadata and over-broad roles still matter beside K8s RBAC.

Red Team vs Blue Team (awareness only)

Red Team — what attackers try

  • Find open dashboards, kubelets, or mis-bound Services of type LoadBalancer.
  • Steal service account tokens from mounts inside a compromised pod (high-level).
  • Escape or widen access when pods run privileged (themes — not a how-to).
  • Abuse CI credentials that can kubectl apply to prod.

Blue Team — defend, detect, respond

  • SSO to the API; short-lived creds; no long-lived kubeconfig on laptops without disk encryption + MFA.
  • Pod security standards / policies that deny privileged by default.
  • Secrets in KMS-backed stores; rotate on leak.
  • Audit log → SIEM: exec, secret get bursts, new ClusterRoleBindings.
  • Break-glass admin with tickets and alarms.

How do I apply basics step by step?

Step 1 — Lock the front door

  1. Confirm the API server endpoint is not anonymously usable from the internet.
  2. Remove or strongly authenticate any dashboard.
  3. Restrict kubectl access via your identity provider groups.

Step 2 — RBAC least privilege

  1. Inventory ClusterRoles and RoleBindings.
  2. Replace standing cluster-admin for humans with namespace-scoped roles.
  3. CI bots get only the verbs they need (often apply in one namespace).

Step 3 — Namespace and network boundaries

  1. One team / app per namespace when practical.
  2. Default-deny ingress/egress policies; open only documented paths.
  3. Keep kube-system and ingress controllers tightly watched.

Step 4 — Workload hardening

  1. runAsNonRoot, drop capabilities, read-only root filesystem where apps allow.
  2. CPU/memory limits — noisy neighbours and fork bombs hurt everyone.
  3. Prefer distroless or minimal base images from maintained sources.

Step 5 — Secrets and images

  1. No passwords in environment variables committed to git.
  2. Image scan in CI; block critical fixable CVEs on prod paths.
  3. Pin versions; record SBOMs for cluster images you ship.

Step 6 — Detect and practise (lab)

  1. On a local kind/minikube cluster you own, create a read-only role and prove AccessDenied on delete.
  2. Turn on audit logging (or provider equivalent) and ship a sample to your lab SIEM.
  3. Deploy a deliberate too-open Service only inside host-only / local networking — never on a public VPS without auth.
  4. Tabletop: “dashboard open for 2 hours — revoke, rotate, review Deployments”.

Starter policy themes (descriptive, not a paste-blind prod YAML)

  1. Deny privileged containers in app namespaces.
  2. Deny hostNetwork / hostPID for normal apps.
  3. Allow egress to approved CIDRs / FQDNs only (mesh or DNS policy depending on stack).
  4. Require labels: owner, app, data-class — so SOC knows who to page.

Ravindra Bagale's Tip

💡 Students install kind and expose the dashboard NodePort 0.0.0.0/0 on a public VPS. Then they say "it's a lab". Even a lab should bind localhost / VPN. Remember the Tesla-2018 lesson. In interviews say RBAC + network policy + non-root + audit. Stay alert!

Managed Kubernetes vs DIY — shared habits

  1. Cloud-managed control planes still need your RBAC, network policies and workload hardening.
  2. Node OS patching may be shared with the provider — read their shared-responsibility doc for your service.
  3. Store kubeconfigs in a password manager or SSO flow; never commit them to git.
  4. Treat every LoadBalancer / public Ingress as an internet service with authn/authz and rate limits.
  5. Keep a paper (or wiki) map: namespace → owner → data class → on-call.

Care-take — cluster habits that age well

  1. GitOps with reviews for prod manifests — no cowboy kubectl on Friday night without tickets.
  2. Separate prod cluster accounts / projects from personal experiments.
  3. Rotate service account tokens and cloud node roles on a schedule.
  4. Backup etcd / control plane recovery tested like any DR plan.
  5. Patch nodes and middlewares; old kube versions are CVE magnets.
  6. When someone leaves: revoke IdP group that gates cluster access first.

How do I fix common Kubernetes security mistakes?

Ghabru naka 😅 — these are the usual ones:

Symptom Likely cause Fix
Crypto miners in namespace Open admin surface / weak RBAC Private API; authn; audit Deployments
Secret in git history Env baked in manifests Rotate; use secret store; scan git
Pod can reach all DBs No network policy Default deny; allow-list flows
“AccessDenied” chaos Over-tight without break-glass Monitored emergency RoleBinding
Node disk full No resource limits / log spam Limits + retention + alerts
CI can destroy cluster Bot has cluster-admin Namespace-scoped apply only

Try it at home

On kind or minikube on your laptop (or a cloud sandbox billed to you):

  1. Create a namespace learnfast-lab and a Role that can get/list pods only; prove delete fails.
  2. List Services; ensure nothing sensitive is published on a public IP for “convenience”.
  3. Write three SIEM alert ideas (new ClusterRoleBinding, secret list burst, Deployment in kube-system by odd user).
  4. One-sentence Tesla-2018 lesson in your notes.
  5. Tear down the lab when done; do not leave dashboards on cheap VPS hosts.

Got it? K8s security = API locked + RBAC least privilege + network/secrets/non-root + audit. Do not forget the Tesla dashboard lesson. Practise in your own lab. Next: learn DevSecOps pipeline.

Frequently asked questions

Why is an open Kubernetes dashboard dangerous?

It is an admin console — public reporting on Tesla 2018 showed cryptojacking after dashboard exposure.

What is RBAC in Kubernetes?

Role-based access control deciding which identities may run which verbs on which resources.

Should pods run as root?

Prefer non-root with dropped capabilities unless a rare exception is documented.

Where do secrets belong?

In a secret store / KMS-backed mechanism — not in git, images or casual ConfigMaps.

Is this guide for attacking clusters?

No. It is defensive hardening on clusters you own or are authorised to administer.

Where are related lessons?

Cloud IAM, network security, secrets and detection chapters on this site.