- Application layer: what applications use (e.g., Ingress controllers).
- Presentation layer: formats and encryption.
- Session layer: connection/session control (ports/sessions).
- Transport layer: TCP/UDP — where NetworkPolicy ports apply.
- Network layer: routing and IP — where NetworkPolicy ipBlock/CIDR applies.
- Data link layer: framing on the network.
- Physical layer: raw bit transmission (switches, NICs).


- Namespaced and applied to selected pods via
podSelector. - Additive: multiple NetworkPolicies can select the same pod; the effective allowed traffic is the union of all rules.
- Default behavior:
- If no NetworkPolicy selects a pod, that pod is open for both ingress and egress.
- If a NetworkPolicy selects a pod and includes
Ingressrules, ingress is restricted to those rules; similarly forEgress. - To lock both directions, ensure policies cover both
IngressandEgress.
- Enforcement depends on the cluster CNI: your plugin must support NetworkPolicy (examples: Calico, Cilium, Canal). Without CNI support the rules are not enforced.
- Targets pods labeled
app: myappin thedefaultnamespace. - Restricts both ingress and egress (
policyTypesincludes both). - Ingress allows TCP traffic on port 6379 from:
- IPs in
172.17.0.0/16excluding172.17.1.0/24. - Pods in namespaces labeled
project: myproject. - Pods labeled
role: frontend(in the same namespace unless anamespaceSelectoris used).
- IPs in
- Egress allows outbound traffic to
10.0.0.0/24.
Behavior summary
- If a pod is selected by one or more policies that include
Ingress, only traffic matching thoseingressrules is allowed in. - If a pod is selected by one or more policies that include
Egress, only traffic matching thoseegressrules is allowed out. - Traffic in directions not covered by any selecting policy remains unrestricted.
NetworkPolicies operate at layers 3 and 4 (IP and port). They do not filter by HTTP paths or application-layer data — use an application-aware proxy, Ingress controller, or a WAF for L7 filtering.
NetworkPolicies are enforced only if your cluster’s CNI plugin implements them. Verify your CNI (e.g., Calico, Cilium) supports NetworkPolicy before relying on these rules for security.
- Kubernetes NetworkPolicy docs: https://kubernetes.io/docs/concepts/services-networking/network-policies/
- Calico: https://projectcalico.org/
- Cilium: https://cilium.io/
- Redis (port reference): https://redis.io/