Network policy and discovery
Evaluate both directions of policy
Kubernetes NetworkPolicy describes permitted Pod traffic at the IP and transport-port level. It works only when the networking implementation enforces it. By default a Pod is not isolated for a direction until selected by a policy applying to that direction. After isolation, matching allow rules from applicable policies are combined additively. For a new Pod-to-Pod connection, the source egress and destination ingress must both permit it. Return traffic for permitted connections is implicitly allowed. A namespace selector matches labels on namespaces; a Pod selector matches labels on Pods. An empty podSelector in a policy selects all Pods in its namespace. An empty rule list for an isolated direction permits no new traffic in that direction. These examples exclude node traffic, host networking, NAT-order differences and implementation-specific features.
Build an explicit flow matrix
The shop namespace contains front, orders and db Pods. Assume all three are isolated for ingress and egress. Permit front egress to orders TCP 8080, orders ingress from front TCP 8080, orders egress to db TCP 5432, and db ingress from orders TCP 5432. Also permit DNS from front and orders to a fictional DNS Pod in namespace infra on UDP and TCP 53; that DNS Pod is not ingress-isolated. Everything else in our simplified model is denied. Front cannot open a direct database connection. Orders cannot initiate a new connection to front just because front is allowed to call orders. Adding a second policy that allows front to db only on egress still does not permit the flow because db ingress remains closed. Policy does not provide application user authorization or TLS.
front -> orders TCP/8080 ALLOWorders -> db TCP/5432 ALLOWfront -> db TCP/5432 DENYfront -> dns UDP/53 ALLOWfront -> dns TCP/53 ALLOWorders -> front TCP/8080 DENYSeparate DNS from application reachability
Assume cluster domain cluster.local. A normal Service named orders in namespace shop has DNS name orders.shop.svc.cluster.local and resolves to its ClusterIP in this IPv4 model. A client in shop may use the short name orders through its configured DNS search list; another namespace should use a qualified name. Headless Services instead expose endpoint addresses through DNS when applicable. In the fictional incident, front reaches the Service IP but name lookup times out. Review the resolver configuration, DNS Service and endpoints, permitted UDP and TCP 53 flows, and DNS logs before changing the order Service. A timeout is not the same as NXDOMAIN, which indicates a negative name response. If the name resolves but TCP fails, investigate Service endpoints, target port, data plane and both policy directions.
Observe and verify without touching a cluster
For every authorized flow record source namespace and label, destination, protocol, port, expected verdict and application outcome. Keep at least one negative test for each sensitive service. A policy object accepted by the API is only control-plane evidence; enforcement requires a capable plugin and data-plane tests. In a real approved lab, collect sanitized EndpointSlice state, DNS response codes, connection metrics and policy verdicts with timestamps. Correlate service-mesh metrics if present, but distinguish proxy errors from underlying packet loss. Use mock records in this lesson; never apply policies to a shared cluster or disable protections to make a test pass. Before an authorized change save the prior configuration and verify recovery criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Network policy and discovery Quiz
Reason about namespaces, Service forwarding and network policy.
Front egress permits db TCP 5432, but db ingress permits only orders. What is the verdict?
What happens to matching allow rules from multiple applicable NetworkPolicies?
Why allow both UDP and TCP port 53 for the example DNS resolver?
Front can reach ClusterIP but DNS lookup times out. What should you inspect first?
A NetworkPolicy is accepted by the API. What remains necessary?