Production: deny-all-ingress, then allow specific app-to-app traffic
Begin by denying all ingress to pods in theproduction namespace. The policy below uses an empty podSelector ({}) to select every pod in production, sets policyTypes to only Ingress, and provides no ingress rules — which means “deny all ingress” to the selected pods.
production namespace):
allow-frontend-to-middleware— protects the middleware pods and permits ingress from pods labeledapp=frontend(inproduction).allow-middleware-to-mysql— protects MySQL pods and permits ingress from pods labeledapp=middleware(inproduction).
podSelector and the from entries are provided without a namespaceSelector, they only match pods in the same namespace as the policy. A QA or dev tester in another namespace with app=frontend will not be matched by these rules.
Apply the policies:
qa and dev namespaces to the middleware service in production — both are blocked):

alpha-prod: allow ingress from a logging namespace (namespaceSelector)
If the source pods you want to allow live in another namespace, usenamespaceSelector. A podSelector inside a policy selects pods only from the policy’s namespace; to select pods from other namespaces, add a namespaceSelector that matches namespace labels.
Example: allow all pods in alpha-prod to accept ingress from any namespace labeled function=logging. This policy is placed in alpha-prod and selects all pods there via podSelector: {}:
from entry aloud: “from a namespace where function equals logging.” Because NetworkPolicies are additive, an existing deny-all policy in alpha-prod remains; this allow simply opens that specific path on top of the deny-all baseline.
NetworkPolicy peers are evaluated relative to the policy’s namespace. A
podSelector without a namespaceSelector matches only pods in the same namespace as the policy. To match pods in other namespaces, include a namespaceSelector.beta-prod: require both namespace label AND pod label on the source (namespaceSelector + podSelector)
When both namespace-level and pod-level constraints must apply to the same source, putnamespaceSelector and podSelector together in the same peer mapping (same list item) under from. That groups selectors with a logical AND.
Start with the top-level policy metadata (this policy lives in beta-prod and selects all pods in that namespace):
function=logging AND has role=logger-1, combine selectors into the same peer mapping (single dash):
namespaceSelector and podSelector are keys of the same mapping (the same list item in the from list), both must match for the traffic to be allowed: the source must be a pod labeled role=logger-1 inside a namespace labeled function=logging.
Contrast: if you place namespaceSelector and podSelector as separate list items (each with its own dash), you create an OR between those peers:
- any pod in a namespace labeled
function=logging, OR - any pod in the same namespace as the policy labeled
role=logger-1.
Final reminder: Ingress vs Egress
All of the policies above control only what may enter selected pods (Ingress). Pods are still free to initiate outbound connections (Egress) unless you define explicit Egress rules. If you need to restrict outbound traffic as well, add corresponding Egress policies.