Skip to content
Microsegmentation.si

Microsegmentation implementation

Kubernetes Microsegmentation: Network Policies, Limits and Plugins

How to segment a cluster with Kubernetes NetworkPolicy, and when you need more.

Jorge HorstUpdated Based on public sources, not lab tested

Kubernetes microsegmentation usually starts with the NetworkPolicy API. By default, pods accept traffic from anywhere. Once a policy selects a pod, that pod accepts only what a policy allows, for each direction the policy covers. Your network plugin must support enforcement, or the policy does nothing. Start with default deny, then add narrow allow rules.

Need YAML now? Use the Kubernetes network policy builder, which runs in your browser.

How do Kubernetes network policies work?

Network policies are additive allow lists that apply per direction. These rules from the Kubernetes documentation explain most surprises:

  • Isolation: a pod is unrestricted until a policy selects it for that direction.
  • Additive: policies never conflict. The allowed traffic is the union of all policies that apply.
  • Both ends count: a connection needs egress allowed at the source and ingress allowed at the destination.
  • Scope: policies are namespaced, and there is no built-in default for all namespaces.

How do you create a default-deny policy?

A default-deny policy selects every pod in a namespace and lists no allow rules. This example from the Kubernetes documentation covers both directions:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress

Listing Egress matters here. If you omit policyTypes, Kubernetes always sets Ingress, but it sets Egress only when the policy has an egress section. A deny-all egress policy without that line silently becomes ingress-only.

What are the most common NetworkPolicy mistakes?

The most common mistake is a single misplaced dash, which changes AND into OR. In this first example, one list item holds both selectors, so a pod must match both:

ingress:
- from:
  - namespaceSelector:
      matchLabels:
        user: alice
    podSelector:
      matchLabels:
        role: client

In this second example, the extra dash creates two list items, so either one matches. This allows any pod in user=alice namespaces, plus role=client pods in the policy’s own namespace:

ingress:
- from:
  - namespaceSelector:
      matchLabels:
        user: alice
  - podSelector:
      matchLabels:
        role: client

Other mistakes to check for:

MistakeWhat happens
ingress: [{}] used as a denyIt allows all ingress. Deny means an empty or missing list
Assuming ipBlock works for podsPod IPs are short-lived, and source NAT can change the address the policy sees
Selecting a namespace by nameThere is no name field. Use the kubernetes.io/metadata.name label
Using endPort on an unsupported pluginThe policy applies only to the first port
Expecting rules for ICMPBehaviour for protocols other than TCP, UDP and SCTP is undefined

What can’t Kubernetes NetworkPolicy do?

The documentation lists several things that the API can’t do. It has no explicit deny rules, no logging of allowed or blocked traffic, no TLS features and no targeting of services by name. It also can’t set defaults across all namespaces, force traffic through a gateway or block traffic from a pod’s own node. Existing connections and hostNetwork pods can also behave differently, depending on the implementation.

When do you need Cilium or Calico policies?

You need plugin-specific policies when standard NetworkPolicy lacks a feature you require. Both projects also accept standard NetworkPolicy, so you can start simple.

NeedCiliumCalico
ResourcesCiliumNetworkPolicy, CiliumClusterwideNetworkPolicyNetworkPolicy and GlobalNetworkPolicy (projectcalico.org/v3)
Cluster-wide policyYesYes
Explicit deny and rule orderSee Cilium’s documentationYes, using order
Layer 7 rulesHTTP and DNSWith Istio
VMs and host interfacesCheck your versionYes

See Cilium vs Calico for a fuller comparison, and the best microsegmentation tools for commercial options. NIST SP 800-207A also notes that network-tier policies typically lack per-request decisions in application context, which is where a service mesh may help.

How should you roll out Kubernetes microsegmentation?

Roll it out one namespace at a time, starting with a low-risk application.

  1. Confirm that your network plugin enforces NetworkPolicy, using a test namespace.
  2. Map real traffic. Hubble and Calico’s flow logs can both show it, and the flow log policy drafter can help if you export flows to CSV.
  3. Add allow rules, including DNS, before you add default deny.
  4. Apply default deny and watch for failures. Pods may start before a new policy is handled, so don’t rely on policies at startup.
  5. Store policies in version control with the application.

The same principles appear in our network segmentation best practices.

Sources

  • Kubernetes, Network Policies: kubernetes.io/docs/concepts/services-networking/network-policies
  • Kubernetes, NetworkPolicy API reference: kubernetes.io/docs/reference/kubernetes-api/policy-resources/network-policy-v1
  • Kubernetes, Declare Network Policy: kubernetes.io/docs/tasks/administer-cluster/declare-network-policy
  • Cilium, Layer 7 policy: docs.cilium.io/en/stable/security/policy/layer7
  • Cilium, Kubernetes policy resources: docs.cilium.io/en/stable/network/kubernetes/policy
  • Calico, network policy: docs.tigera.io/calico/latest/network-policy/get-started/calico-policy/calico-network-policy
  • NIST SP 800-207A, September 2023: doi.org/10.6028/NIST.SP.800-207A

Frequently asked questions

Does Kubernetes have microsegmentation built in?

Kubernetes provides the NetworkPolicy API, but it has no effect unless your network plugin implements it. The Kubernetes documentation names Antrea, Calico, Cilium and Kube-router as supporting providers.

Can Kubernetes network policies deny traffic explicitly?

No. Standard NetworkPolicy has no deny rules. Policies are additive allow lists, and a pod becomes isolated once a policy selects it. Calico and Cilium add their own policy resources with more features, such as Calico's deny rules.

Why did DNS stop working after I added an egress policy?

Default-deny egress also blocks DNS. Pods that need name resolution need a rule allowing egress to the cluster DNS service, usually on UDP and TCP port 53. Check the DNS pod labels in your cluster, because they vary.

Can Kubernetes network policies filter by URL or HTTP path?

No. NetworkPolicy works at layer 3 and 4. Cilium's CiliumNetworkPolicy supports layer 7 rules such as HTTP paths and DNS names, and Calico can add layer 5 to 7 matching with Istio.

Related articles