Back to Blog

Implementing Micro-segmentation in Kubernetes via Cilium Network Policies

Implementing Micro-segmentation in Kubernetes via Cilium Network Policies

The "crunchy shell, soft center" security model-where a hardened perimeter protects a permissive internal network-is fundamentally incompatible with the dynamic, ephemeral nature of Kubernetes. In a standard Kubernetes cluster, the default networking behavior is "flat": any pod can communicate with any other pod across the entire cluster. While this facilitates ease of development, it creates a massive blast radius. If an attacker compromises a single, internet-facing microservice, the lack of internal boundaries allows for unrestricted lateral movement, reconnaissance, and data exfiltration.

To mitigate this, we must implement micro-segmentation. While standard Kubernetes `NetworkPolicies` provide a baseline for L3/L4 isolation, they lack the granularity required for modern, identity-aware security. This is where Cilium, powered by eBPF (Extended Berkeley Packet Filter), redefines the security boundary by providing deep L7 visibility and identity-based enforcement.

The Shift from IP-Based to Identity-Based Security

Traditional firewalls and standard Kubernetes NetworkPolicies rely heavily on IP addresses and port numbers. In a containerized environment, IPs are ephemeral; pods are created and destroyed constantly, making IP-based ACLs (Access Control Lists) brittle and difficult to manage at scale.

Cilium fundamentally changes this paradigm by utilizing Security Identities. When a pod is created, Cilium assigns it a numeric identity based on its labels. When a packet travels from Pod A to Pod B, the Cilium agent (running as an eBPel program in the kernel) doesn't just look at the source IP; it looks at the embedded security identity.

Because the policy enforcement happens in the kernel via eBPF, the lookup is $O(1)$ rather than the $O(n)$ complexity associated with traversing large `iptables` rule sets. This allows for highly granular, label-based policies that remain performant even as the number of services and rules grows.

Moving Beyond L4: Deep Packet Inspection with L7 Policies

Standard Kubernetes NetworkPolicies are limited to Layer 3 (IP/CIDR) and Layer 4 (TCP/UDP/SCTP). They can stop a pod from reaching a specific port, but they cannot prevent a pod from sending a `DELETE` request to a sensitive API endpoint if the port is open.

Cilium extends this to Layer 7. By integrating an embedded Envoy proxy, Cilium can perform Deep Packet Inspection (DPI) to enforce policies based on application-layer protocols, including HTTP, Kafka, gRPC, and DNS.

Practical Example: L7 HTTP Enforcement

Consider a scenario where a `frontend` service needs to communicate with a `backend` service. We want to allow `GET` requests to retrieve data, but strictly prohibit `POST` or `DELETE` operations to prevent unauthorized state changes.

```yaml

apiVersion: "cilium.io"

kind: "CiliumNetworkPolicy"

metadata:

name: "restrict-backend-api"

namespace: "production"

spec:

endpointSelector:

matchLabels:

app: backend

ingress:

  • fromEndpoints:
  • matchLabels:

app: frontend

toPorts:

  • ports:
  • port: "8080"

protocol: TCP

rules:

http:

  • method: "GET"

path: "/api/v1/products.*"

  • method: "GET"

path: "/api/v1/inventory.*"

```

In this policy, Cilium intercepts the traffic at the socket or `tc` (traffic control) layer. If the `frontend` attempts an `HTTP POST` to `/api/v1/products`, the eBPF program identifies the mismatch against the Envoy-processed rules and drops the packet at the source or destination, preventing the malicious payload from ever reaching the application logic.

Practical Example: DNS-Based Egress Control

One of the most common vectors for data exfiltration is a compromised pod making outbound calls to a malicious Command and Control (C2) server. Using Cilium, we can implement egress filtering based on DNS names rather than unstable IP ranges.

```yaml

apiVersion: "cilium.io"

kind: "CiliumNetworkPolicy"

metadata:

name: "allow-stripe-api-only"

spec:

endpointSelector:

matchLabels:

app: payment-processor

egress:

  • toFQDNs:
  • matchPattern: "*.stripe.com"

toPorts:

  • ports:
  • port: "443"

protocol: TCP

rules:

dns:

  • pattern: "*"

mode: allow

```

This policy ensures that the `payment-processor` can only initiate outbound connections to Stripe's API. Even if an attacker gains execution capabilities within the pod, they cannot reach any other external domain.

Operationalizing Micro-segmentation

Implementing micro-segmentation is not a "set and forget" operation. It requires a robust observability strategy. You cannot secure what you cannot see.

Hubble, the observability layer for Cilium, is indispensable here. Hubble provides a real-time view of service dependencies and, crucially, a stream of dropped packets due to policy enforcement. When a new deployment fails because of a network policy, Hubble allows operators to see exactly which `CiliumNetworkPolicy` caused the drop, which identity was involved, and the specific L7 rule that was violated.

Conclusion

As shown across "The Shift from IP-Based to Identity-Based Security", "Moving Beyond L4: Deep Packet Inspection with L7 Policies", "Operationalizing Micro-segmentation", a secure implementation for implementing micro-segmentation in kubernetes via cilium network policies depends on execution discipline as much as design.

The practical hardening path is to enforce admission-policy enforcement plus workload isolation and network policy controls, host hardening baselines with tamper-resistant telemetry, and protocol-aware normalization, rate controls, and malformed-traffic handling. This combination reduces both exploitability and attacker dwell time by forcing failures across multiple independent control layers.

Operational confidence should be measured, not assumed: track mean time to detect and remediate configuration drift and detection precision under peak traffic and adversarial packet patterns, then use those results to tune preventive policy, detection fidelity, and response runbooks on a fixed review cadence.

Related Articles

Explore related cybersecurity topics:

Recommended Next Steps

If this topic is relevant to your organisation, use one of these paths: