Skip to main content
Kubernetes Security: RBAC, Pod Security & NetworkPolicies
Lesson 6 of 11

Kubernetes Security: RBAC, Pod Security & NetworkPolicies

Learn how to restrict Kubernetes control-plane access with least-privilege RBAC, enforce Pod Security Standards via securityContext hardening, isolate east-west pod traffic with NetworkPolicies, and audit manifests with Kubescape and Trivy.

Advanced 32 min Cloud & DevSecOps

Learning objectives

  • Distinguish namespace-scoped Roles from cluster-wide ClusterRoles and identify dangerous RBAC permissions
  • Harden Pod and container securityContext settings to satisfy the Kubernetes Restricted Pod Security Standard
  • Design default-deny and allow-list NetworkPolicy manifests to segment east-west pod traffic
  • Scan Kubernetes YAML manifests for RBAC and Pod misconfigurations using Kubescape and Trivy

Recommended first: Cloud IAM: Policies, Roles, Federation & Privilege PathsCloud Networking (VPCs, Security Groups) & Storage SecurityContainer & Docker Security: Namespaces, Rootless & Images

#Why this matters

Hardening a single Docker container is not enough once workloads run inside a Kubernetes cluster. A single compromised Pod sits on a shared cluster network and, by default, receives a mounted ServiceAccount token that lets it talk directly to the Kubernetes API server.

If that Pod also runs with privileged container settings, a wildcard RBAC role, and no network segmentation, an application bug turns into a cluster-wide breach. Kubernetes security rests on three core controls: RBAC for the control plane, Pod Security for the node runtime, and NetworkPolicies for east-west traffic.

#Kubernetes RBAC and ServiceAccount least privilege

Kubernetes authorizes API requests using Role-Based Access Control (RBAC). Two scopes exist:

  • Role and RoleBinding grant permissions inside one namespace (for example app-prod).
  • ClusterRole and ClusterRoleBinding grant permissions across every namespace or on cluster-scoped objects like nodes.

By default, Kubernetes mounts a ServiceAccount token at /var/run/secrets/kubernetes.io/serviceaccount/token inside every Pod. Applications that never query the Kubernetes API should disable this in the Pod spec with automountServiceAccountToken: false.

How a Pod request reaches the Kubernetes APIHow a Pod request reaches the Kubernetes APIPodAPI ServerAuthNRBACHTTPS request with ServiceAccount bearer tokenVerify JWT signature and ServiceAccount identityCheck RoleBinding / ClusterRoleBinding rulesAllow or Deny (403 Forbidden)JSON resource response or Forbidden

Watch closely for dangerous RBAC patterns during audits:

  • Wildcard verbs or resources (verbs: ["*"], resources: ["*"]).
  • Broad get or list access on secrets (exposes credentials across the namespace or cluster).
  • create on pods/exec (allows running commands inside other Pods) or pods (allows launching a privileged Pod).
  • escalate, bind, or impersonate verbs, which allow a principal to grant itself higher roles.

Use kubectl auth can-i to verify effective permissions for a ServiceAccount before deploying:

Checking ServiceAccount permissions with kubectl
$ kubectl auth can-i list secrets --all-namespaces --as=system:serviceaccount:app-prod:web-sa
no
$ kubectl auth can-i get configmaps -n app-prod --as=system:serviceaccount:app-prod:web-sa
yes

#Pod Security Standards and securityContext hardening

Kubernetes enforces runtime safety through Pod Security Admission (PSA), which replaced the deprecated PodSecurityPolicy in Kubernetes 1.25. Administrators label each namespace with enforce, audit, or warn modes targeting one of three Pod Security Standards:

Profile Intent Key restrictions
Privileged Unrestricted Allows host namespaces, root, and privileged containers (system daemons only)
Baseline Stops known escapes Blocks privileged: true, hostNetwork, hostPID, hostIPC, and dangerous hostPath mounts
Restricted Hardened best practice Enforces Baseline plus runAsNonRoot: true, allowPrivilegeEscalation: false, capabilities.drop: ["ALL"], and RuntimeDefault seccomp

Label a namespace to test policies safely with warn and audit before enforcing:

Applying Restricted Pod Security Admission labels
$ kubectl label namespace app-prod pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restricted

To pass the Restricted profile, configure securityContext on both the Pod and container, alongside readOnlyRootFilesystem: true for defense in depth.

#Isolating east-west traffic with NetworkPolicies

Without a NetworkPolicy, Kubernetes allows every Pod in every namespace to connect to every other Pod over the network. A NetworkPolicy is namespace-scoped and selects target Pods via podSelector. Once any NetworkPolicy selects a Pod for Ingress or Egress, all unselected traffic of that direction is denied.

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-ingress-allow-web
  namespace: app-prod
spec:
  podSelector:
    matchLabels:
      app: payment-api
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080

#Defending and common mistakes

  • Granting ClusterRole out of convenience: Always prefer a namespace-scoped Role and RoleBinding unless cluster-wide access is genuinely required.
  • Forgetting automountServiceAccountToken: false: Most application Pods never talk to the Kubernetes API; leaving tokens mounted hands attackers free API credentials on RCE.
  • Writing egress policies that block DNS: If you add policyTypes: ["Egress"] without explicitly allowing UDP/TCP port 53 to kube-dns / coredns, service discovery breaks immediately.
  • Turning on enforce=restricted blindly: Rolling out enforce=restricted on an active namespace blocks non-compliant Pods at admission time; label with warn and audit first and fix manifests with kubescape or trivy config.
  • Assuming NetworkPolicy works without verifying the CNI: Test blocked ports between two lab Pods after applying a policy to confirm your CNI actually enforces rules.

#Summary

Securing Kubernetes requires defense in depth across identity, runtime, and network boundaries. Replace wildcard ClusterRole rules with namespace-scoped Role permissions and disable unnecessary ServiceAccount token mounting. Enforce the Restricted Pod Security Standard through hardened securityContext fields, segment east-west Pod traffic with default-deny NetworkPolicy manifests on a policy-aware CNI, and validate YAML continuously with kubescape and trivy config.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge