Kubernetes Network Policies

Rüdiger Küpper KubernetesNetworkPolicyInternet

German Version

Github Actions


In the world of container orchestration, Kubernetes plays a leading role in managing and automating containerized applications. One of the key components in Kubernetes is the NetworkPolicy, an essential tool for controlling communication between Pods. In this article, we will explore the basics of NetworkPolicies and how they can be used to secure Kubernetes clusters.

Basics of NetworkPolicies

NetworkPolicies are specific objects in Kubernetes that define how groups of Pods may communicate with each other and with other network endpoints. They use labels to identify Pods and define rules that govern the traffic between these Pods.

Types of NetworkPolicies

  1. Ingress Rules:
    • These rules control incoming traffic to Pods.
    • They can specify which sources (IP addresses or Pods) are allowed to access a Pod.
  2. Egress Rules:
    • These rules control outgoing traffic from Pods.
    • They can define which destinations (IP addresses or Pods) a Pod is allowed to communicate with.

Creating a NetworkPolicy

To create a NetworkPolicy, you define a YAML file with the desired rules and apply it to the Kubernetes cluster. Here is a simple example of an Ingress NetworkPolicy:

kind: NetworkPolicy
  name: example-networkpolicy
  namespace: default
      app: myapp
  - Ingress
  - from:
    - ipBlock:

This policy allows incoming traffic to Pods with the label app: myapp only from the IP range

Best Practices

  1. Principle of Least Privilege:
    • Grant only the minimal necessary permissions.
    • By default, deny all connections and only allow specific ones.
  2. Explicit Egress Rules:
    • Define clear Egress rules to control outgoing traffic.
  3. Use of Labels:
    • Use labels to identify Pods, making the management of NetworkPolicies more straightforward.


NetworkPolicies are an indispensable tool for securing Kubernetes clusters. They enable fine-grained control over the communication between Pods and help enforce the principle of least privilege. Through the effective use of NetworkPolicies, organizations can operate their applications in Kubernetes securely and reliably.

Rüdiger Küpper
DevOps Engineer & CISO @ Mogenius