Showing posts with label container-security. Show all posts
Showing posts with label container-security. Show all posts

Friday, 24 October 2025

Implementing Zero-Trust Networking in Kubernetes with Cilium - Complete 2025 Guide

October 24, 2025 0

Implementing Zero-Trust Networking within a Kubernetes Cluster with Cilium: The Complete 2025 Guide

Zero-Trust Networking in Kubernetes Cluster with Cilium eBPF Security Implementation - showing secure microservice communication with network policies and encrypted connections

In today's rapidly evolving cloud-native landscape, traditional perimeter-based security models are no longer sufficient. As Kubernetes becomes the de facto standard for container orchestration, implementing Zero-Trust networking has become crucial for enterprise security. In this comprehensive guide, we'll explore how to implement Zero-Trust principles within your Kubernetes clusters using Cilium—the eBPF-powered CNI that's revolutionizing container networking and security. Whether you're securing microservices in production or building a new cloud-native application, this deep dive will provide you with practical strategies and advanced techniques for achieving true Zero-Trust architecture in your Kubernetes environment.

🚀 Why Zero-Trust is Non-Negotiable in Modern Kubernetes

Zero-Trust networking operates on the fundamental principle of "never trust, always verify." Unlike traditional security models that assume everything inside the network perimeter is safe, Zero-Trust requires continuous verification of every request, regardless of its origin. In Kubernetes environments, where containers are ephemeral and workloads are highly dynamic, this approach is particularly critical.

The 2025 cloud-native landscape presents unique challenges that make Zero-Trust essential:

  • Multi-cloud deployments blur traditional network boundaries
  • Ephemeral workloads make IP-based security policies obsolete
  • API-driven infrastructure increases attack surface
  • Regulatory requirements demand granular security controls
  • Supply chain attacks require runtime protection

According to recent studies, organizations implementing Zero-Trust architectures in their Kubernetes environments have seen a 67% reduction in security incidents and a 45% improvement in compliance audit outcomes. Cilium, with its eBPF-powered data plane, provides the perfect foundation for implementing these principles effectively.

🔍 Understanding Cilium's eBPF-Powered Security Model

Cilium leverages eBPF (extended Berkeley Packet Filter) to enforce security policies at the kernel level, providing unprecedented visibility and control over network traffic. Unlike traditional iptables-based solutions, Cilium's eBPF implementation offers:

  • Kernel-level enforcement without proxying traffic
  • Application-aware security policies based on Kubernetes identities
  • Real-time observability with minimal performance overhead
  • L3/L4 and L7 network policies for granular control
  • Service mesh capabilities without sidecar complexity

Cilium's security model aligns perfectly with Zero-Trust principles by enabling identity-aware networking, where policies are based on workload identity rather than network topology. This approach ensures that security policies remain effective even as workloads scale, migrate, or change network locations.

🛠️ Installing and Configuring Cilium for Zero-Trust

Let's start by installing Cilium in your Kubernetes cluster. The following steps assume you have a running Kubernetes cluster (version 1.25 or newer) and helm installed.

💻 Cilium Installation with Helm


# Add the Cilium Helm repository
helm repo add cilium https://helm.cilium.io/
helm repo update

# Install Cilium with Zero-Trust features enabled
helm install cilium cilium/cilium --version 1.15.0 \
  --namespace kube-system \
  --set egressGateway.enabled=true \
  --set hubble.enabled=true \
  --set hubble.relay.enabled=true \
  --set hubble.ui.enabled=true \
  --set policyEnforcementMode=default-deny \
  --set securityContext.capabilities.ciliumAgent="{CHOWN,KILL,NET_ADMIN,NET_RAW,IPC_LOCK, SYS_ADMIN,SYS_RESOURCE}" \
  --set securityContext.capabilities.cleanCiliumState="{NET_ADMIN,SYS_ADMIN,SYS_RESOURCE}" \
  --set kubeProxyReplacement=strict \
  --set k8sServiceHost=API_SERVER_IP \
  --set k8sServicePort=API_SERVER_PORT

# Verify installation
kubectl -n kube-system get pods -l k8s-app=cilium
kubectl get ciliumnodes -A

  

The key configuration parameters for Zero-Trust include policyEnforcementMode=default-deny, which implements the fundamental Zero-Trust principle of denying all traffic by default. This ensures that no communication can occur without explicit policy approval.

🔐 Implementing L3/L4 Network Policies

Layer 3 and 4 network policies control traffic based on IP addresses, ports, and protocols. In a Zero-Trust model, these policies should be granular and application-specific. Let's create a comprehensive network policy for a microservices application.

💻 Advanced CiliumNetworkPolicy Example


apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: zero-trust-frontend-backend
  namespace: production
spec:
  description: "Zero-Trust policy for frontend to backend communication"
  endpointSelector:
    matchLabels:
      app: backend-api
      env: production
  ingress:
  - fromEndpoints:
    - matchLabels:
        app: frontend
        env: production
    toPorts:
    - ports:
      - port: "8080"
        protocol: TCP
      rules:
        http:
        - method: "GET"
          path: "/api/v1/*"
        - method: "POST"
          path: "/api/v1/orders"
        - method: "PUT"
          path: "/api/v1/orders/*"
  - fromEndpoints:
    - matchLabels:
        app: monitoring
        env: production
    toPorts:
    - ports:
      - port: "9090"
        protocol: TCP
  egress:
  - toEndpoints:
    - matchLabels:
        app: database
        env: production
    toPorts:
    - ports:
      - port: "5432"
        protocol: TCP
  - toEndpoints:
    - matchLabels:
        app: redis-cache
        env: production
    toPorts:
    - ports:
      - port: "6379"
        protocol: TCP
---
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: default-deny-all
  namespace: production
spec:
  description: "Default deny all traffic in production namespace"
  endpointSelector: {}
  ingress:
  - {}
  egress:
  - {}

  

This policy demonstrates several Zero-Trust principles: explicit allow-listing of communication paths, identity-based authentication (using Kubernetes labels), and default-deny posture. The policy ensures that only specific services can communicate with each other on designated ports and protocols.

🌐 Layer 7 Application Security with Cilium

While L3/L4 policies provide foundational security, L7 policies offer application-aware protection that's essential for modern microservices. Cilium's L7 policies can inspect HTTP, gRPC, and other application protocols to enforce security at the application layer.

💻 L7 HTTP-aware Security Policy


apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: l7-api-security
  namespace: production
spec:
  description: "L7 API security with method and path restrictions"
  endpointSelector:
    matchLabels:
      app: user-service
  ingress:
  - fromEndpoints:
    - matchLabels:
        app: api-gateway
    toPorts:
    - ports:
      - port: "8080"
        protocol: TCP
      rules:
        http:
        - method: "GET"
          path: "/api/v1/users"
          headers:
          - "X-API-Key: .*"
        - method: "GET"
          path: "/api/v1/users/[0-9]+"
          headers:
          - "Authorization: Bearer .*"
        - method: "POST"
          path: "/api/v1/users"
          headers:
          - "Content-Type: application/json"
          - "Authorization: Bearer .*"
        - method: "PUT"
          path: "/api/v1/users/[0-9]+"
          headers:
          - "Content-Type: application/json"
          - "Authorization: Bearer .*"
        - method: "DELETE"
          path: "/api/v1/users/[0-9]+"
          headers:
          - "Authorization: Bearer .*"
  egressDeny:
  - toPorts:
    - ports:
      - port: "443"
        protocol: TCP
      rules:
        http:
        - method: ".*"
          path: "/admin/.*"
        - method: "DELETE|PUT|POST"
          path: "/api/internal/.*"

  

This L7 policy demonstrates advanced Zero-Trust capabilities: header validation, HTTP method restrictions, and path-based authorization. By enforcing these rules at the kernel level, Cilium prevents unauthorized API access without the performance overhead of application-level proxies.

🔍 Service Mesh and mTLS Integration

Cilium's service mesh capabilities provide mutual TLS (mTLS) for service-to-service communication, a critical component of Zero-Trust architecture. Unlike traditional service meshes that use sidecars, Cilium implements mTLS at the kernel level using eBPF.

💻 Enforcing mTLS with Cilium


apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
  name: enforce-mtls-clusterwide
spec:
  description: "Enforce mTLS for all service-to-service communication"
  endpointSelector: {}
  ingress:
  - fromEndpoints:
    - {}
    toPorts:
    - ports:
      - port: "443"
        protocol: TCP
      - port: "8080"
        protocol: TCP
      - port: "9090"
        protocol: TCP
    termination:
      mode: "TLS"
      certificates:
        - secret:
            name: cilium-clusterwide-ca
        - secret:
            name: cilium-workload-ca
  egress:
  - toEndpoints:
    - {}
    toPorts:
    - ports:
      - port: "443"
        protocol: TCP
      - port: "8080"
        protocol: TCP
      - port: "9090"
        protocol: TCP
    termination:
      mode: "TLS"
      certificates:
        - secret:
            name: cilium-clusterwide-ca
        - secret:
            name: cilium-workload-ca
---
# Certificate management with cert-manager integration
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: cilium-ca-issuer
spec:
  ca:
    secretName: cilium-ca-secret
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: cilium-workload-cert
  namespace: kube-system
spec:
  secretName: cilium-workload-ca
  issuerRef:
    name: cilium-ca-issuer
    kind: ClusterIssuer
  commonName: "*.cluster.local"
  dnsNames:
  - "*.cluster.local"
  - "*.svc.cluster.local"
  - "*.pod.cluster.local"

  

This configuration ensures that all service-to-service communication within the cluster is encrypted and authenticated using mTLS. The policy applies cluster-wide and automatically handles certificate rotation and validation.

📊 Monitoring and Observability with Hubble

Zero-Trust requires comprehensive visibility into network traffic and policy enforcement. Cilium's Hubble provides real-time observability, allowing you to monitor policy violations, traffic flows, and security events.

💻 Hubble Observability Configuration


# Enable Hubble with security observability features
helm upgrade cilium cilium/cilium --version 1.15.0 \
  --namespace kube-system \
  --reuse-values \
  --set hubble.metrics.enabled="{dns,drop,tcp,flow,port-distribution,icmp,http}" \
  --set hubble.relay.enabled=true \
  --set hubble.ui.enabled=true \
  --set prometheus.enabled=true \
  --set operator.prometheus.enabled=true

# Access Hubble UI
kubectl port-forward -n kube-system svc/hubble-ui 12000:80 &

# Query security events with Hubble CLI
hubble observe --since=1h --type=drop --verdict=DROPPED
hubble observe --since=1h --verdict=DENIED
hubble observe --since=1h --label app=frontend

# Generate security reports
hubble metrics --server hubble-relay:4245 --flows bytes,count,packets

  

Hubble provides critical insights for maintaining Zero-Trust compliance, including real-time policy violation alerts, traffic flow analysis, and security incident investigation capabilities.

🛡️ Advanced Zero-Trust Patterns

Beyond basic network policies, Cilium supports advanced Zero-Trust patterns that address complex security requirements in enterprise environments.

  • Egress Gateway Control: Route all external traffic through dedicated egress gateways for inspection and logging
  • DNS-based Security: Enforce DNS policies and prevent DNS tunneling attacks
  • FQDN-based Policies: Control egress traffic based on domain names rather than IP addresses
  • Cluster Mesh Security: Extend Zero-Trust policies across multiple Kubernetes clusters
  • Runtime Security: Detect and prevent suspicious process behavior using eBPF

💻 Egress Gateway and FQDN Policies


apiVersion: cilium.io/v2
kind: CiliumEgressGatewayPolicy
metadata:
  name: controlled-egress
spec:
  selectors:
  - podSelector:
      matchLabels:
        app: external-api-consumer
  destinationCIDRs:
  - "0.0.0.0/0"
  egressGateway:
    nodeSelector:
      matchLabels:
        node-role.kubernetes.io/egress: "true"
    egressIP: "192.168.100.100"
---
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: fqdn-egress-control
spec:
  endpointSelector:
    matchLabels:
      app: external-api-consumer
  egress:
  - toFQDNs:
    - matchName: "api.stripe.com"
    - matchPattern: "*.github.com"
    toPorts:
    - ports:
      - port: "443"
        protocol: TCP
  - toFQDNs:
    - matchName: "s3.amazonaws.com"
    toPorts:
    - ports:
      - port: "443"
        protocol: TCP
      - port: "80"
        protocol: TCP
  egressDeny:
  - toFQDNs:
    - matchPattern: "*"

  

⚡ Key Takeaways

  1. Zero-Trust in Kubernetes requires identity-aware networking, not just IP-based policies
  2. Cilium's eBPF-powered data plane provides kernel-level enforcement with minimal overhead
  3. Start with default-deny policies and explicitly allow required communication paths
  4. Implement L7 policies for application-aware security beyond basic network controls
  5. Use Hubble for comprehensive observability and policy validation
  6. Combine network policies with mTLS for defense-in-depth security
  7. Monitor policy violations and adjust policies based on actual traffic patterns

❓ Frequently Asked Questions

How does Cilium's Zero-Trust approach differ from traditional firewalls?
Traditional firewalls operate at the network perimeter and use IP-based rules. Cilium implements identity-aware security policies based on Kubernetes labels and can enforce L7 application rules at the kernel level using eBPF, providing more granular and dynamic security that adapts to container orchestration.
What performance impact does Cilium have compared to other CNI plugins?
Cilium typically has lower performance overhead than traditional CNI plugins because eBPF programs run directly in the kernel, avoiding context switches and system calls. In benchmarks, Cilium shows 5-15% better performance for network-intensive workloads compared to iptables-based solutions.
Can Cilium replace service mesh solutions like Istio?
Cilium can handle many service mesh use cases including mTLS, traffic management, and observability without sidecar proxies. However, for advanced application-level routing, canary deployments, and complex traffic splitting, you might still need Istio or Linkerd alongside Cilium for a complete solution.
How do I migrate from Calico/Flannel to Cilium for Zero-Trust?
Start by installing Cilium in chaining mode alongside your existing CNI. Gradually implement CiliumNetworkPolicies while monitoring with Hubble. Once policies are validated, switch to Cilium as the primary CNI. Always test in non-production environments first and have rollback plans.
What are the monitoring best practices for Zero-Trust with Cilium?
Enable Hubble with metrics for drops, DNS, and HTTP flows. Set up alerts for policy violations and unexpected traffic patterns. Use Cilium's integration with Prometheus and Grafana for long-term trend analysis. Regularly review policy effectiveness and adjust based on observed traffic patterns.

💬 Found this article helpful? Please leave a comment below or share it with your network to help others learn! Have you implemented Zero-Trust in your Kubernetes clusters? Share your experiences and challenges in the comments!

About LK-TECH Academy — Practical tutorials & explainers on software engineering, AI, and infrastructure. Follow for concise, hands-on guides.

Monday, 20 October 2025

Mastering Pod Security Contexts and OPA Gatekeeper for Kubernetes Security 2025

October 20, 2025 0

Mastering Pod Security Contexts and OPA Gatekeeper for Kubernetes Security

Kubernetes Pod Security Context and OPA Gatekeeper security implementation diagram showing container isolation, policy enforcement, and security layers for enterprise Kubernetes clusters

In today's cloud-native landscape, Kubernetes security has become paramount as organizations scale their containerized applications. With the rise of sophisticated cyber threats targeting container environments, understanding and implementing robust security measures is no longer optional—it's essential. This comprehensive guide dives deep into two critical Kubernetes security components: Pod Security Contexts and OPA Gatekeeper. Whether you're a DevOps engineer, platform architect, or security specialist, mastering these tools will transform your Kubernetes security posture from vulnerable to enterprise-ready.

🚀 Understanding Pod Security Contexts: The Foundation of Container Security

Pod Security Contexts define privilege and access control settings for pods and containers. They're your first line of defense against container escape attacks and privilege escalation. Let's break down the key components:

  • RunAsUser/RunAsGroup: Controls which user and group IDs containers run as
  • FSGroup: Defines the special supplemental group for volume ownership
  • RunAsNonRoot: Ensures containers don't run as root user
  • AllowPrivilegeEscalation: Prevents child processes from gaining more privileges
  • Capabilities: Manages Linux capabilities granted to containers
  • Seccomp/SELinux: Advanced security profiles and context types

💻 Secure Pod Security Context Configuration


apiVersion: v1
kind: Pod
metadata:
  name: secure-app-pod
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: secure-app
    image: nginx:1.25
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop:
        - ALL
        add:
        - NET_BIND_SERVICE
      readOnlyRootFilesystem: true
    volumeMounts:
    - name: tmp-volume
      mountPath: /tmp
  volumes:
  - name: tmp-volume
    emptyDir: {}

  

🔒 Advanced Security Context Patterns

For enterprise-grade security, consider these advanced patterns that go beyond basic configurations:

  • AppArmor Profiles: Application-level access control policies
  • Seccomp Custom Profiles: Fine-grained system call filtering
  • Pod Security Standards: Implementing baseline and restricted policies
  • Service Account Token Projection: Secure service account token management

💻 Advanced Seccomp Profile Implementation


# custom-seccomp-profile.yaml
apiVersion: v1
kind: SeccompProfile
metadata:
  name: custom-restricted
annotations:
  seccomp.security.alpha.kubernetes.io/allowedProfileNames: custom-restricted
spec:
  defaultAction: SCMP_ACT_ERRNO
  architectures:
  - SCMP_ARCH_X86_64
  - SCMP_ARCH_X86
  - SCMP_ARCH_X32
  syscalls:
  - names:
    - accept
    - access
    - arch_prctl
    - bind
    - brk
    # ... additional allowed syscalls
    action: SCMP_ACT_ALLOW

# Pod using custom seccomp profile
apiVersion: v1
kind: Pod
metadata:
  name: seccomp-demo
spec:
  securityContext:
    seccompProfile:
      type: Localhost
      localhostProfile: profiles/custom-restricted.yaml
  containers:
  - name: test-container
    image: nginx:1.25
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: ["ALL"]

  

🚀 Introduction to OPA Gatekeeper: Policy-as-Code for Kubernetes

OPA (Open Policy Agent) Gatekeeper brings policy-as-code to Kubernetes, enabling you to define, enforce, and audit policies across your cluster. Unlike traditional admission controllers, Gatekeeper provides:

  • Declarative Policy Language: Use Rego for complex policy logic
  • Audit Capabilities: Continuous compliance monitoring
  • Dry-run Mode: Test policies before enforcement
  • Custom Resources: Native Kubernetes API for policies
  • Mutation Support: Automatically fix policy violations

💻 Installing OPA Gatekeeper


# Install latest Gatekeeper version
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper.yaml

# Verify installation
kubectl get pods -n gatekeeper-system

# Check webhook configuration
kubectl get validatingwebhookconfigurations gatekeeper-validating-webhook-configuration

# Install mutation webhook (optional)
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper-mutation.yaml

  

🔐 Creating Custom Constraints with Rego

Rego is OPA's purpose-built policy language that enables sophisticated policy decisions. Let's create policies that enforce security best practices:

💻 Require Non-Root User Policy


# ConstraintTemplate
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
  name: k8srequiredprobes
spec:
  crd:
    spec:
      names:
        kind: K8sRequiredProbes
      validation:
        openAPIV3Schema:
          type: object
          properties:
            probes:
              type: array
              items:
                type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8srequiredprobes

        violation[{"msg": msg}] {
            container := input.review.object.spec.containers[_]
            expected_probes := input.parameters.probes
            missing_probes := expected_probes - get_probes(container)
            count(missing_probes) > 0
            msg := sprintf("Container %v is missing required probes: %v", [container.name, missing_probes])
        }

        get_probes(container) = probes {
            probes := [p | p := container.livenessProbe; p != null]
            probes := probes + [p | p := container.readinessProbe; p != null]
            probes := probes + [p | p := container.startupProbe; p != null]
        }

# Constraint Instance
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredProbes
metadata:
  name: require-liveness-readiness
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    probes: ["livenessProbe", "readinessProbe"]

  

⚡ Advanced Gatekeeper Policies for Security

Let's implement comprehensive security policies that cover multiple aspects of Kubernetes security:

💻 Comprehensive Security Policy Suite


# Block privileged containers
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPPrivilegedContainer
metadata:
  name: psp-privileged-container
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    privileged: false

# Require specific security contexts
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPSecurityContext
metadata:
  name: require-security-context
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    runAsNonRoot: true
    runAsUser:
      min: 1000
      max: 65535
    allowPrivilegeEscalation: false

# Block host namespace sharing
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPHostNamespace
metadata:
  name: block-host-namespace
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    hostPID: false
    hostIPC: false
    hostNetwork: false

# Image registry whitelist
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sAllowedRepos
metadata:
  name: allowed-repositories
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    repos:
    - "docker.io/library/"
    - "gcr.io/my-project/"
    - "registry.k8s.io/"

  

🔍 Monitoring and Auditing with Gatekeeper

Gatekeeper's audit functionality provides continuous compliance monitoring. Here's how to leverage it effectively:

  • Constraint Status: Monitor policy violations across the cluster
  • Audit Results: Historical data on policy compliance
  • Metrics Integration: Export metrics to Prometheus
  • Alerting: Set up alerts for critical violations

💻 Audit Configuration and Monitoring


# Check constraint status
kubectl get constraints

# View detailed constraint violations
kubectl describe K8sPSPPrivilegedContainer psp-privileged-container

# Get audit results
kubectl get constrainttemplates -o yaml

# Set up audit frequency (in Gatekeeper deployment)
kubectl patch deployment gatekeeper-controller-manager \
  -n gatekeeper-system \
  --type='json' \
  -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/args", "value": ["--audit-interval=60", "--log-level=INFO", "--operation=webhook"]}]'

# Export metrics to Prometheus
kubectl port-forward -n gatekeeper-system deployment/gatekeeper-controller-manager 8888:8888
curl localhost:8888/metrics

  

🛠️ Real-World Implementation Strategy

Implementing these security measures requires a phased approach to avoid breaking existing applications:

  1. Assessment Phase: Audit current pod security contexts and identify gaps
  2. Policy Development: Create Gatekeeper policies in dry-run mode
  3. Testing: Deploy policies to non-production environments
  4. Enforcement: Gradually enable enforcement with proper monitoring
  5. Optimization: Continuously refine policies based on audit results

💻 Gradual Policy Enforcement Strategy


# Phase 1: Dry-run mode
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPPrivilegedContainer
metadata:
  name: psp-privileged-container-dry-run
  annotations:
    mode.gatekeeper.sh: dry-run
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    privileged: false

# Phase 2: Warn mode (after 2 weeks of dry-run)
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPPrivilegedContainer
metadata:
  name: psp-privileged-container-warn
  annotations:
    mode.gatekeeper.sh: warn
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    privileged: false

# Phase 3: Enforcement (after addressing violations)
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPPrivilegedContainer
metadata:
  name: psp-privileged-container-enforce
spec:
  match:
    kinds:
    - apiGroups: [""]
      kinds: ["Pod"]
  parameters:
    privileged: false

  

⚡ Key Takeaways

  1. Pod Security Contexts provide fundamental container isolation and privilege control
  2. OPA Gatekeeper enables policy-as-code with comprehensive audit capabilities
  3. Implement security policies gradually using dry-run and warn modes
  4. Combine multiple security layers for defense-in-depth approach
  5. Continuous monitoring and auditing are essential for maintaining security posture

❓ Frequently Asked Questions

What's the difference between Pod Security Context and Pod Security Standards?
Pod Security Context is a Kubernetes native specification that defines security settings for individual pods, while Pod Security Standards are policy frameworks that define baseline, restricted, and privileged security levels. OPA Gatekeeper can enforce these standards across your cluster.
Can OPA Gatekeeper replace Kubernetes Pod Security Policies?
Yes, OPA Gatekeeper is the recommended replacement for the deprecated Pod Security Policies (PSP). It provides more flexibility, better audit capabilities, and supports policy-as-code through Rego language.
How do I handle legacy applications that require privileged access?
Use Gatekeeper's exemption features to create namespaces or labels that bypass certain policies for specific applications. Gradually refactor these applications to remove privileged requirements while maintaining business continuity.
What performance impact does OPA Gatekeeper have on cluster operations?
Gatekeeper adds minimal latency (typically 10-50ms) to admission requests. The impact depends on policy complexity and cluster size. Use constraint templates efficiently and avoid overly complex Rego policies for optimal performance.
How can I test Gatekeeper policies before applying them to production?
Use the dry-run mode to test policies without enforcement, set up a dedicated testing cluster, or use tools like Conftest to test policies locally against your Kubernetes manifests before deployment.

💬 Found this article helpful? Please leave a comment below or share it with your network to help others learn! Have you implemented Pod Security Contexts or OPA Gatekeeper in your environment? Share your experiences and challenges!

About LK-TECH Academy — Practical tutorials & explainers on software engineering, AI, and infrastructure. Follow for concise, hands-on guides.