OpsCanary
securitynetwork securityPractitioner

Mastering Network Policies in Kubernetes: Security Without Compromise

5 min read Cilium DocsAug 2, 2026Reviewed for accuracy
Share
PractitionerHands-on experience recommended

Network policies exist to control the flow of traffic to and from your Kubernetes pods, providing a vital layer of security in a microservices architecture. By defining rules at Layers 3 and 4, you can restrict access based on IP addresses and ports, ensuring that only authorized services can communicate with each other. This is especially important in multi-tenant environments where isolation is key to preventing data leaks and unauthorized access.

When you run Cilium on Kubernetes, you can leverage its ability to distribute policies across all nodes automatically. This means you can define a Kubernetes NetworkPolicy for basic L3 and L4 controls, or opt for the more advanced CiliumNetworkPolicy, which allows for Layer 3-7 specifications. To enable Kubernetes ClusterNetworkPolicy support, you must set the --enable-k8s-cluster-network-policy flag to true. Additionally, be cautious with ipBlock rules, as they do not match intra-cluster IPs by default, which could lead to unexpected behavior if not configured correctly.

In production, managing multiple policy types can become complex. You need to pay close attention to how these policies interact, as overlapping rules might create unintended traffic allowances. Starting with Cilium 1.20, you can use Kubernetes ClusterNetworkPolicy, but migrating from CiliumClusterwideNetworkPolicy requires careful planning. Always install the Custom Resource Definition (CRD) from sigs.k8s.io/network-policy-api to get started.

Key takeaways

  • Implement NetworkPolicies to control traffic flow to and from your pods.
  • Use CiliumNetworkPolicy for advanced Layer 3-7 security specifications.
  • Enable Kubernetes ClusterNetworkPolicy by setting the appropriate flag.
  • Be cautious with multiple policy types to avoid unintended traffic allowances.
  • Install the Custom Resource Definition (CRD) for network policies before implementation.

Why it matters

In a production environment, effective network policies can significantly reduce the attack surface of your applications, preventing unauthorized access and potential data breaches. This is critical for maintaining compliance and trust in your services.

Code examples

Bash
$kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/network-policy-api/release-0.2/config/crd/standard/policy.networking.k8s.io_clusternetworkpolicies.yaml
Go
type CiliumNetworkPolicy struct { // +deepequal-gen=false metav1.TypeMeta `json:",inline"` // +deepequal-gen=false metav1.ObjectMeta `json:"metadata"` // Spec is the desired Cilium specific rule specification. Spec *api.Rule `json:"spec,omitempty"` // Specs is a list of desired Cilium specific rule specification. Specs api.Rules `json:"specs,omitempty"` // Status is the status of the Cilium policy rule // +deepequal-gen=false // +kubebuilder:validation:Optional Status CiliumNetworkPolicyStatus `json:"status"` }
Go
type CiliumClusterwideNetworkPolicy struct { // Spec is the desired Cilium specific rule specification. Spec *api.Rule // Specs is a list of desired Cilium specific rule specification. Specs api.Rules // Status is the status of the Cilium policy rule. // The reason this field exists in this structure is due a bug in the k8s // code-generator that doesn't create a `UpdateStatus` method because the // field does not exist in the structure. // +kubebuilder:validation:Optional Status CiliumNetworkPolicyStatus }

When NOT to use this

Keep this in mind when migrating from CiliumClusterwideNetworkPolicy to Kubernetes ClusterNetworkPolicy. The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →
DigitalOceanSponsor

Simple, affordable cloud — VMs, Kubernetes, and managed databases in minutes. Trusted by 600,000+ developers. Spin up a Droplet in 60 seconds.

Try DigitalOcean →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.