Kubernetes Network Policies 101

Stav Sitnikov

TL;DR

Kubernetes network policies are crucial for managing and securing network traffic within Kubernetes clusters. These policies help you improve security and compliance through network segmentation and control. Learn about common Kubernetes network policies including:

  • Allow All IngressTraffic Within Namespace
  • Deny All IngressTraffic
  • Allow SpecificEgress Traffic
  • Restrict Access to Specific Ports
  • Isolate Sensitive Workloads
  • Restrict Egress Traffic to a Specific Namespace
  • Combine Ingress and Egress Policies

Kubernetes network policies are crucial for managing and securing network traffic within Kubernetes clusters. In this blog post, we will explore why policies are essential, walk through some practical examples, and discuss the challenges you might face when implementing them.

Understanding these concepts will enable you to effectively secure your Kubernetes clusters and maintain a robust infrastructure.

Why Kubernetes Network Policies?

Kubernetes network policies provide a way to control the network traffic within a cluster. They are essential for several reasons:

  • Security: Network policies help prevent unauthorized access to your applications and services, reducing the risk of breaches and cyber attacks.
  • Compliance: By controlling network traffic, you can ensure compliance with industry-specific regulations and internal policies.
  • Network Segmentation: Network policies allow you to isolate and segment applications, reducing the blast radius in case of a security incident.

Kubernetes Network Policy Examples

Let's go through some practical examples of Kubernetes network policies to better understand their use cases:

a. Allow All IngressTraffic Within Namespace:

This policy allows all ingress traffic within the same namespace, while denying traffic from other namespaces.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: allow-same-namespace
spec:
 podSelector: {}
 ingress:
 - from:
   - namespaceSelector:
       matchLabels:
         app: my-app
 policyTypes:
 - Ingress

b. Deny All IngressTraffic:

This policy denies all ingress traffic to the pods it is applied to, effectively isolating them from other resources within the cluster.

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

c. Allow SpecificEgress Traffic:

This policy allows egress traffic from selected pods to a specific external service, identified by its IP address range.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: allow-egress-to-external-service
spec:
 podSelector:
   matchLabels:
     app: my-app
 egress:
 - to:
   - ipBlock:
       cidr: 203.0.113.0/24
 policyTypes:
 - Egress

d. Restrict Access to Specific Ports:

This policy allows ingress traffic only to a specific port on the pods it's applied to, there by restricting access to just the necessary services.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: allow-specific-port
spec:
 podSelector:
   matchLabels:
     app: my-app
 ingress:
 - from:
   - namespaceSelector:
       matchLabels:
         app: my-app
   ports:
   - protocol: TCP
     port: 8080
 policyTypes:
 - Ingress

e. Isolate Sensitive Workloads: This policy denies ingress traffic to a sensitive application (e.g., a database)except from trusted application pods labeled with app=my-trusted-app.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: isolate-sensitive-workload
spec:
 podSelector:
   matchLabels:
     app: sensitive-app
 ingress:
 - from:
   - podSelector:
       matchLabels:
         app: my-trusted-app
 policyTypes:
 - Ingress

f. Restrict Egress Traffic to a Specific Namespace:

This policy allows egress traffic only to pods within the same namespace or a specified namespace, while denying egress traffic to other namespaces or external resources.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: restrict-egress-to-namespace
spec:
 podSelector:
   matchLabels:
     app: my-app
 egress:
 - to:
   - namespaceSelector:
       matchLabels:
         app: my-app
 policyTypes:
 - Egress

g. Combine Ingress and Egress Policies:

This policy restricts ingress traffic to a specific application and allows egress traffic only to a particular external service.

 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: combined-ingress-egress
spec:
 podSelector:
   matchLabels:
     app: my-app
 ingress:
 - from:
   - namespaceSelector:
       matchLabels:
         app: my-app
 egress:
 - to:
   - ipBlock:
       cidr: 203.0.113.0/24
 policyTypes:
 - Ingress
 - Egress 

Challenges with Kubernetes Network Policies and How to Fix Them

While Kubernetes network policies are powerful, they come with challenges. The most common challenges include:

  • Complexity: Network policies can become complex as your cluster and application requirements grow, making it difficult to manage and troubleshoot policies.
  • Compatibility: Not all network plugins support Kubernetes network policies, and some may have their custom implementation, leading to inconsistencies in policy enforcement.
  • Monitoring: It can be challenging to monitor and visualize network policies in a cluster, making it harder to identify and resolve issues using native tools or legacy tools.
By using Lightlytics, you can solve these Complexity and Monitoring challenges:
See how Kubernetes network policies interact with your overall AWS posture
Review reachable allowed ports for each path, across ACLs, Security Groups and K8S network policies.

Troubleshoot Kubernetes network policies easily and understand their impact
Track all Kubernetes network policy changes

Start a free trail now.
Read the GigaOM CXO Decision Brief:
Cloud Change Intelligence
What's new
Deploy cloud infrastructure changes with confidence. Troubleshoot faster with the complete context of your cloud environment.
GET STARTED