1. Default “Allow-All” Behavior
By default, Kubernetes does not restrict pod-to-pod traffic. Any pod in the cluster can communicate with any other pod on any port. To secure your DB pod:- Deny all incoming traffic.
- Explicitly allow only the API pod to connect on port 3306.
2. Deny All Ingress to the DB Pod
First, create a policy that selects pods with labelrole=db and blocks all ingress:
This policy ensures no traffic can reach the DB pod until you add explicit
ingress rules.3. Allow Ingress from the API Pod on Port 3306
Next, extenddb-policy to permit traffic from the API pod:
Responses from the DB pod back to the API pod are automatically allowed—no
egress rule is required for reply traffic.4. Restrict API Access by Namespace
If you have multiple namespaces (dev, test, prod), the preceding policy allows API pods from all namespaces. To limit to the prod namespace, add a namespaceSelector:
The target namespace must have the label
name=prod before this selector will match.5. Allow Traffic from an External IP Range
To permit a backup server (e.g.,192.168.5.10/32) outside your cluster to read from the DB, use an ipBlock:
prod OR external IP) grants access.
Selector Logic
| Combination | Semantics |
|---|---|
podSelector + namespaceSelector (same) | AND (both must match) |
Multiple entries under from or to | OR (any one entry may match) |
6. Adding Egress Rules
If your DB pod must initiate outbound connections (e.g., pushing backups), includeEgress in policyTypes and define an egress rule:
192.168.5.10.
Summary of Policy Types
| Policy Type | Controls |
|---|---|
| Ingress | Incoming traffic to selected pods |
| Egress | Outgoing traffic from selected pods |