Kubernetes Security: Pod Security and Network Policies

Khimananda Oli 6 min read Virtualization
Kubernetes Security: Pod Security and Network Policies

By Khimananda Oli | Last reviewed: August 2026

Securing a Kubernetes cluster requires enforcing boundaries at both the workload and network layers simultaneously. Kubernetes Security: Pod Security and Network Policies provides this defense-in-depth by restricting what containers can do and controlling which services they can communicate with. Without these controls, a single compromised pod can lead to full cluster takeover or lateral movement across namespaces. This guide covers the exact configurations needed to implement these safeguards effectively.

Defense-in-Depth LayersPod Security AdmissionNamespace Labels (Enforce)Blocks Privileged ContainersNetwork PoliciesIngress/Egress FilteringMicro-segmentationSecure Workload RuntimeLeast Privilege + Isolation
Layered approach to Kubernetes Security: Pod Security and Network Policies combining admission control with runtime network isolation

How Do You Configure Pod Security Standards in Kubernetes?

Pod Security Admission (PSA) replaced the deprecated PodSecurityPolicy in Kubernetes 1.25+. It operates via namespace labels rather than custom resources, making it simpler to audit and manage. For teams familiar with deploying apps to Kubernetes clusters, PSA adds a mandatory validation layer before pods are even scheduled.

Understanding the Three Security Levels

  • Privileged: Unrestricted policy, allowing all capabilities including host access. Use only for system namespaces like kube-system.
  • Baseline: Minimally restrictive, preventing known privilege escalations (no hostNetwork, hostPID, privileged containers). Suitable for most legacy workloads.
  • Restricted: Heavily locked down, requiring non-root users, read-only root filesystems, and dropped capabilities. Mandatory for sensitive data processing.

Applying Namespace Labels for Enforcement

You must label namespaces to activate PSA. The three modes—enforce, audit, and warn—allow gradual rollout without breaking existing deployments:

# Enforce restricted standard on production namespace
kubectl label namespace production \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/audit=restricted \
  pod-security.kubernetes.io/warn=restricted \
  --overwrite

# Verify labels applied correctly
kubectl get namespace production --show-labels

A common mistake is applying enforce mode immediately. Start with warn and audit modes for two weeks, review violations in API server logs, fix offending manifests, then switch to enforce. This prevents outages during security hardening.

What Are Kubernetes Network Policies and How Do They Work?

NetworkPolicies define allowed traffic flows at Layer 3/4 using pod selectors, namespace selectors, and IP blocks. By default, all pods can communicate freely; NetworkPolicies flip this to deny-all unless explicitly permitted. This is critical because containerized applications often assume unrestricted network access during development.

Creating a Default Deny Policy

Always start with a default-deny baseline per namespace, then add allow rules incrementally:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: production
spec:
  podSelector: {}  # Applies to all pods in namespace
  policyTypes:
  - Ingress
  - Egress

Allowing Specific Traffic Flows

After denying all traffic, create targeted allow policies. This example permits the frontend to reach the backend API on port 8080:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-api
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: api-backend
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    ports:
    - protocol: TCP
      port: 8080

Critical note: NetworkPolicies require a CNI plugin that supports them (Calico, Cilium, Weave Net). AWS VPC CNI, Azure CNI, and GKE dataplane v2 all support NetworkPolicies natively. Flannel does not support them without additional configuration.

NetworkPolicy Traffic ControlFrontend Podapp: frontendAPI Backendapp: api-backendDatabase PodNo External Access✓ Port 8080 Allowed✗ Blocked✓ Port 5432
NetworkPolicy enforcement showing allowed frontend-to-API traffic while blocking unauthorized database access

How Does Pod Security Differ From Network Policies?

These mechanisms address different threat vectors and must be used together. Understanding their distinct roles prevents misconfiguration gaps when implementing Kubernetes Security: Pod Security and Network Policies.

AspectPod Security AdmissionNetwork Policies
ScopeContainer runtime privileges and capabilitiesNetwork traffic between pods and external endpoints
Enforcement PointAdmission controller (pre-scheduling)CNI plugin (runtime packet filtering)
ConfigurationNamespace labelsNetworkPolicy YAML resources
Threat MitigatedContainer escape, privilege escalationLateral movement, data exfiltration
Failure ModePod rejected at creationTraffic silently dropped at runtime
DependenciesBuilt-in (k8s ≥1.25)Requires compatible CNI plugin

In practice, PSA stops a malicious container from mounting the host filesystem, while NetworkPolicies prevent that same container from reaching your database if compromised through an application vulnerability. Neither alone is sufficient.

What Common Mistakes Break Kubernetes Security Controls?

After auditing dozens of production clusters for SOC 2 compliance, I see the same patterns repeatedly. Avoid these pitfalls when hardening environments, whether on managed EKS/AKS/GKE or self-managed infrastructure configured via Terraform infrastructure as code.

Overlooking Egress Restrictions

Most teams configure ingress policies but leave egress wide open. A compromised pod can then exfiltrate data to any external IP or reach internal metadata services (169.254.169.254). Always restrict egress to required destinations:

spec:
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          name: database-ns
    ports:
    - protocol: TCP
      port: 5432
  - to:
    - ipBlock:
        cidr: 10.0.0.0/8
    ports:
    - protocol: TCP
      port: 443  # Internal API gateway only

Mixing PSA Modes Incorrectly

Setting enforce=baseline but audit=restricted creates confusion. Pods pass enforcement but generate audit noise for restricted violations you never intended to fix. Align all three modes to the same level once validated, or use warn for the stricter level during transition periods.

Forgetting DNS Egress

Default-deny egress breaks DNS resolution, causing cascading failures. Always allow UDP port 53 to kube-dns/CoreDNS:

egress:
- to:
  - namespaceSelector:
      matchLabels:
        kubernetes.io/metadata.name: kube-system
    podSelector:
      matchLabels:
        k8s-app: kube-dns
  ports:
  - protocol: UDP
    port: 53
  - protocol: TCP
    port: 53

Testing Without Validation Tools

Never assume policies work as written. Use kubectl-np-viewer or Cilium's policy verdict tool to simulate traffic flows before deploying to production. Manual testing with curl/netshoot pods catches selector typos that YAML linting misses.

Start HardeningApply PSA warn+audit LabelsReview Violations (2 Weeks)Deploy Default-Deny NetworkPolicySwitch PSA to enforce ModeFix Manifests
Recommended implementation sequence for Kubernetes Security: Pod Security and Network Policies with validation checkpoints

Implementing Production-Ready Kubernetes Security

Effective Kubernetes Security: Pod Security and Network Policies requires treating both controls as interdependent components of a single security posture. Start with PSA in warn mode to identify violations without disruption, deploy default-deny NetworkPolicies with explicit allow rules for known traffic flows, validate with connectivity testing tools, then graduate to enforcement. Document every exception with business justification for audit trails—this matters for ISO 27001 and SOC 2 evidence collection.

If your team needs help designing compliant Kubernetes architectures or validating existing security controls against production requirements, reach out to discuss your specific environment. Proper hardening now prevents incident response costs later.

Frequently Asked Questions

Pod Security Standards restrict container runtime behavior like privilege escalation and host access. Network Policies control traffic flow between pods and external endpoints. Both are essential for Kubernetes security but operate at different layers of the stack.

Apply the pod-security.kubernetes.io/enforce: restricted label to your namespace. This blocks non-compliant pods immediately. Use warn and audit labels first to identify violations without breaking deployments during migration.

Yes. Create a default-deny egress policy selecting all pods with empty egress rules. Then explicitly allow required outbound connections. Without this, pods can reach any external endpoint regardless of ingress restrictions.

Yes. PodSecurityPolicy was removed in Kubernetes 1.25. Pod Security Admission is the built-in replacement as of 2026. Migrate existing PSPs using the official psp-to-psa migration tool before upgrading clusters.

Verify a CNI plugin supporting NetworkPolicy is installed. Calico, Cilium, and kube-router support it; Flannel does not. Check policy selectors match pod labels exactly and that namespace selectors are correct.

Set pod-security.kubernetes.io/warn: restricted on the namespace first. Deploy workloads and check API server audit logs or kubectl warnings. Fix violations before switching to enforce mode to avoid deployment failures.

Yes. Use PSA for baseline runtime enforcement and OPA Gatekeeper for custom policies like image registry whitelisting or resource quotas. They complement each other without conflict in Kubernetes 1.30+.

Use kubectl np-viewer or Cilium Hubble to visualize active policies and denied flows. Inspect pod network namespaces with crictl or nsenter. Always validate YAML syntax with kubeval before applying policies.

Namespace-scoped. Apply labels per namespace for granular control. For cluster-wide defaults, configure the Pod Security Admission controller via kube-apiserver flags or use a mutating webhook to auto-label new namespaces.

Add an egress rule permitting UDP port 53 to kube-system namespace where CoreDNS runs. Without this, pods lose name resolution even if HTTP egress is allowed. Always include DNS in default-deny setups.

No. Restricted PSS requires runAsNonRoot, dropping all capabilities, and setting seccompProfile to RuntimeDefault or Localhost. Containers may still run as non-root UID without full rootless mode. Check compliance with kubectl auth can-i.

No. Network Policies only select pods via labels. To target a Service, apply matching labels to its backing pods or use FQDN-based policies with Cilium or Calico enterprise editions in 2026.

The most restrictive level wins. If enforce: baseline and enforce: restricted labels both exist, restricted takes precedence. Avoid conflicting labels; use kubectl get namespace -o yaml to verify effective policy.

Enable audit logging for pod-security admission events. Forward logs to Elasticsearch or Loki. Alert on warn-level events to catch misconfigurations early. Use kubectl describe namespace to see current PSA labels and last violation timestamps.

Minimal. PSA evaluates at admission time only, not runtime. Benchmarks show under 2ms latency per pod creation in Kubernetes 1.32. Network Policies add slight datapath overhead depending on CNI implementation and rule complexity.