Sub topic 16
Common patterns: tiered apps, namespace isolation, monitoring access
This chapter assumes you know how a NetworkPolicy selects pods, how ingress and egress rules work, and how AND and OR selector semantics differ. It also assumes your cluster's network plugin enforces policies. The patterns below build on a default-deny baseline in the demo namespace.
Start from default deny
Each pattern adds allow rules on top of the default-deny-all policy from the previous chapter. Policies are additive, so the order you apply them in doesn't matter. The final allowed traffic is the union of all rules that select a pod.
Pattern 1: tiered application
A typical app has three tiers: frontend, backend and database. Each tier accepts traffic only from the tier directly above it.
The allow-frontend-to-backend policy covers the middle link:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080For the database, repeat the same shape with app: db as the target, app: backend as the source and port 5432. A frontend pod cannot reach the database directly, even if an attacker compromises it.
Pattern 2: namespace isolation
To stop other namespaces from reaching pods in demo, while still letting pods inside demo talk to each other, allow ingress from an empty podSelector. An empty selector in a from entry matches all pods in the policy's own namespace.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-same-namespace
namespace: demo
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- podSelector: {}Pods in other namespaces match no rule, so their traffic is dropped. If you want tier-level control inside the namespace, use this policy instead of the tier policies only when you accept that every pod can reach every other pod.
Pattern 3: monitoring access
Prometheus usually runs in its own namespace, so the backend needs an exception for it. The rule must match both the namespace and the pod label.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-prometheus-scrape
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheusThis policy opens all ports for Prometheus. Add a ports list with your metrics port if you want to restrict it further.
Common errors
- Forgetting that a default-deny egress policy also blocks DNS. Add the DNS egress policy from the earlier chapter.
- Selecting a namespace by name without a label. Use the automatic
kubernetes.io/metadata.namelabel, or label the namespace yourself. - Writing a policy for the source pod's egress but not the destination pod's ingress. When both sides are restricted, both policies must allow the connection.
Exercise
A policy in demo selects app: backend and allows ingress from podSelector: {matchLabels: {app: prometheus}} and, as a separate list item, namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: monitoring}}. Which pods can now reach the backend?
Show answer
Two groups can reach it. The first is every pod in demo labeled app: prometheus. The second is every pod in the monitoring namespace, whatever its labels. Separate list items combine with OR. To require both conditions, put the two selectors in the same list item.