"refused" means nobody is listening. "timed out" means the packets vanish. That smells like a NetworkPolicy. As soon as a policy selects a pod, only what a policy explicitly allows is permitted for it in the direction named.
Symptom
k exec intruder -- wget -qO- -T2 http://api.tag09/hostname; echo wget: download timed out command terminated with exit code 1
NetworkPolicies are enforced by the CNI plugin, Calico in the course. Without a suitable CNI they have no effect.
Diagnosis
List the policies in the namespace. The POD-SELECTOR column shows whom they select – <none> or map[] means every pod.
k get networkpolicy NAME POD-SELECTOR AGE api-from-frontend app=api 68s api-from-other app=api 51s default-deny <none> 75s frontend-egress role=frontend 34s k get netpol -o custom-columns=NAME:.metadata.name,SELECTOR:.spec.podSelector NAME SELECTOR api-from-frontend map[matchLabels:map[app:api]] default-deny map[] frontend-egress map[matchLabels:map[role:frontend]]
Causes
podSelector {}Default deny without a matching exception
An empty podSelector selects every pod in the namespace. Without an ingress rule nothing is allowed. The client needs a policy that explicitly admits it.
fromThe selector doesn't match – or only applies in its own namespace
A podSelector in from only applies in the policy's own namespace. A pod from another namespace stays out, even with the same label. And the port in the policy is the container port, not the service port.
AND / OROne dash too many or too few
Two list entries under from mean OR, one entry with namespaceSelector and podSelector means AND. describe separates alternatives with ----.
bad addressEgress locked down, DNS forgotten
An egress policy without a DNS rule also blocks name resolution. The pod can't find the service even though the path to it is allowed. DNS needs UDP and TCP 53.
k exec frontend -- wget -qO- -T3 http://api.tag09/hostname; echo wget: bad address 'api.tag09' command terminated with exit code 1
Fix
cat <<'EOF' | k apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-from-frontend
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- port: 8080
EOF
CKA hands-on, day 9: default deny, targeted exceptions, the AND/OR trap and DNS in egress.
Go to day 9 →
FAQ
How do I know a NetworkPolicy is to blame?
By the timeout. With connection refused nobody is listening, with timed out the packets vanish.
Which policies apply to a pod?
Every policy whose podSelector selects it. Policies add up, and an empty selector selects every pod in the namespace.