☁ Cloud, Hardware & Emerging

Kubernetes Security Best Practices

Kubernetes automates the deployment and scaling of containers across a cluster of machines, but its flexibility creates a large, layered attack surface, which makes Kubernetes security a specialized concern. A single over-privileged service account or an unrestricted network path between pods can turn a minor foothold into full cluster compromise. Securing Kubernetes means hardening the control plane, constraining workloads, and carefully controlling the identities and network paths that connect them.

The Kubernetes Threat Model

A Kubernetes cluster has two broad parts, and attackers target both. The control plane includes the API server, the etcd datastore, the scheduler, and the controller manager. The worker nodes run the kubelet and the container runtime that host your pods.

Several properties shape the threat model:

  • The API server is the single front door; nearly every action flows through it, so its authentication and authorization are critical.
  • etcd stores the entire cluster state, including secrets, so it must be encrypted and locked down.
  • By default, pods can reach one another freely, which enables lateral movement once any workload is breached.
  • A compromised node exposes every pod scheduled on it, including their tokens and mounted secrets.

Because so much power converges on a handful of components, defense in depth is essential. No single control, whether authentication, authorization, or network policy, should be the only barrier between an attacker and the cluster. The sections below work through those layers in order, from identity to admission control.

Authentication and RBAC

Kubernetes separates authentication (who you are) from authorization (what you may do). Authentication can use client certificates, tokens, or an external identity provider through OIDC. Authorization is governed primarily by role-based access control (RBAC).

Sound RBAC practice follows least privilege:

  • Grant narrowly scoped roles bound to specific namespaces rather than cluster-wide power.
  • Avoid binding users or service accounts to the built-in cluster-admin role.
  • Reject wildcard verbs and resources in roles, which quietly grant far more than intended.
  • Give each workload its own service account, and disable automatic token mounting where a pod does not call the API.

Service account tokens deserve special care, because a pod that mounts a token can use it to call the API server, so a compromised container inherits whatever permissions that account holds. Keeping accounts narrowly scoped, turning off unnecessary token mounts, and preferring short-lived, audience-bound tokens all shrink what an attacker gains from a single breached pod. Enabling audit logging on the API server then records who performed which action, which is indispensable when reconstructing an incident later.

Restricting Workloads with Pod Security

Even correctly authorized workloads must run with limited privileges. The Pod Security Standards define three profiles, privileged, baseline, and restricted, and Pod Security Admission enforces the chosen profile at the namespace level. Individual pods are then constrained through their securityContext:

apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    seccompProfile:
      type: RuntimeDefault
  containers:
    - name: app
      image: registry.example.com/app@sha256:...
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true
        capabilities:
          drop: ["ALL"]

Running as non-root, dropping all capabilities, mounting a read-only root filesystem, and blocking privilege escalation together sharply limit what a compromised container can attempt.

Network Segmentation with Network Policies

Because pods communicate freely by default, network segmentation is one of the most impactful hardening steps. A NetworkPolicy lets you define which pods may talk to which, ideally starting from a default-deny posture and then allowing only required flows.

Effective segmentation includes:

  • A default-deny policy per namespace so nothing is reachable unless explicitly permitted.
  • Ingress rules that admit traffic only from the specific workloads that need it.
  • Egress rules that restrict where pods can send data, containing exfiltration and callbacks.

Protecting Secrets and etcd

Kubernetes Secrets are only base64-encoded, not encrypted, in their default form. Anyone who can read a secret object or reach etcd can recover the plaintext. Protect them by enabling encryption at rest for etcd, restricting RBAC access to secret objects, and limiting which pods mount which secrets.

For stronger control, many teams integrate an external secrets system that injects short-lived credentials at runtime rather than storing long-lived values in the cluster, as covered in secrets management.

Admission Control and Supply Chain

Admission controllers intercept requests to the API server after authentication and authorization, and can validate or mutate them before objects are created. They are the enforcement point for organizational policy: rejecting privileged pods, requiring resource limits, blocking untrusted registries, or demanding signed images.

This dovetails with image-level hardening. Verifying provenance and scanning images before they reach the cluster, as described in container security, keeps known-bad workloads from ever being admitted.

Key Takeaways

  • Treat the API server and etcd as crown jewels; every action and all secrets pass through them.
  • Apply least-privilege RBAC, avoid cluster-admin bindings and wildcards, and give each workload its own service account.
  • Enforce Pod Security Standards and set a restrictive securityContext with non-root users and dropped capabilities.
  • Adopt default-deny NetworkPolicies to stop unrestricted pod-to-pod lateral movement.
  • Encrypt etcd, treat Kubernetes Secrets as sensitive, and use admission control to enforce image and workload policy.
kubernetesrbacnetwork-policypod-securitycluster-hardening

Frequently asked questions

What is Kubernetes security?

Kubernetes security is the practice of protecting a Kubernetes cluster and its workloads across the control plane, worker nodes, network, and application layers. It includes securing the API server, enforcing role-based access control, isolating workloads, managing secrets, and applying policies that restrict what pods can do.

What is RBAC in Kubernetes?

Role-based access control in Kubernetes governs which users and service accounts can perform which actions on which resources. It works by binding subjects to roles that grant specific permissions, and following least privilege with RBAC is one of the most important defenses against cluster compromise.

Why is the Kubernetes API server a critical asset to protect?

The API server is the central control point for the entire cluster, and anyone who gains broad access to it can read secrets, deploy workloads, and control every node. Protecting it requires strong authentication, tight role-based access control, network restrictions, and audit logging so that access is limited and traceable.

What are Kubernetes network policies?

Network policies are rules that control which pods can communicate with each other and with external endpoints. By default Kubernetes allows all pod-to-pod traffic, so network policies are needed to segment workloads and limit lateral movement if a pod is compromised.

How does a compromised pod lead to cluster takeover?

An attacker who compromises a pod may abuse an over-privileged service account token, exploit a container breakout to reach the node, or access the kubelet to pivot further. From there, weak access controls or exposed credentials can let them escalate to control the API server and the entire cluster.

How do I secure a Kubernetes cluster?

Enforce least-privilege role-based access control, restrict and authenticate access to the API server, and apply pod security standards that block privileged containers and host access. Add network policies for segmentation, encrypt and externalize secrets, keep components patched, and enable audit logging and runtime monitoring.

Try it hands-on

K0G is an open toolkit of browser-based security utilities — hashing, encoding, JWT, certificates, crypto and more, all running locally in your browser.

Explore the tools →