
Key Entity Types
Network Policies match traffic based on three entities:
Defining a NetworkPolicy
ANetworkPolicy is a namespaced resource that applies to pods matching podSelector. You must also declare policyTypes (Ingress, Egress, or both) and the corresponding rules:
Entity Selectors and IP Blocks
Use label selectors for pods and namespaces:Layer 4 Ports and Protocols
Control ports and protocols (requires Kubernetes v1.25+ for port ranges):port: Single port or starting port of a rangeprotocol: TCP or UDP (defaults to TCP)endPort: End of port range (optional)
Default Policies
By default, all ingress and egress traffic is allowed. You can enforce a “deny all” or “allow all” baseline by using an emptypodSelector: {}.
An empty
podSelector: {} matches every pod in the namespace.Ingress Defaults
Egress Defaults
Benefits of Network Policies
- Granular security controls by workload
- Isolation in multi-tenant clusters
- Compliance with GDPR, HIPAA, PCI DSS
- Reduced attack surface by blocking unused paths
- Consistent enforcement across applications

Limitations
Network Policies operate at layers 3 & 4 and have some constraints:- Cannot enforce a common gateway (service mesh can)
- No built-in TLS termination or deep packet inspection
- Cannot restrict host-level traffic or localhost loops
- No native allow/deny event logging
- Label-based only—cannot target Service objects
- No Layer 7 (HTTP/gRPC) filtering

CNI-Specific Enhancements
Several CNI providers extend Kubernetes Network Policies with advanced capabilities: