Network Policies¶
Pod-to-pod network isolation enforced by Kubernetes NetworkPolicy resources.
Current posture¶
The tomoda app defines three NetworkPolicies in k8s/apps/tomoda/base/network-policy.yaml (k3s enforces them with its built-in network policy controller):
| Policy | Target | Allowed ingress |
|---|---|---|
tomoda-api-policy |
Pods with app: tomoda-api |
TCP 8080 from namespaces traefik-system (user traffic) and monitoring (metrics scrape), and from the app: tomoda-smoke-test pod |
tomoda-async-policy |
Pods with app: tomoda-async |
TCP 8080 from namespace monitoring only. Async pods otherwise only initiate connections (Redis, Postgres, NATS); the kubelet's /health probe is not subject to NetworkPolicy. |
frontend-policy |
Pods with app: frontend |
TCP 8081 from namespace traefik-system and from the app: tomoda-smoke-test pod |
# Excerpt
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: traefik-system
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
- podSelector:
matchLabels:
app: tomoda-smoke-test
ports:
- protocol: TCP
port: 8080
Effect: only Traefik, Prometheus and the post-deploy smoke test (a PostSync Job in the same namespace) can open connections to the api and frontend pods, and only Prometheus can reach async. If someone breaches another workload in the cluster, they cannot pivot directly into the backend pools.
Each CNPG cluster also has an ingress policy alongside its manifests in
k8s/envs/{dev,prod}/postgres/manifests/network-policy.yaml:
| Policy | Target | Allowed ingress |
|---|---|---|
postgres-dev-policy |
Pods cnpg.io/cluster: postgres-dev |
TCP 5432 from tomoda-dev + data; all ports from cnpg-system; TCP 9187 from monitoring |
postgres-prod-policy |
Pods cnpg.io/cluster: postgres-prod |
TCP 5432 from tomoda-prod + data; all ports from cnpg-system; TCP 9187 from monitoring |
Effect: only the app namespace, this namespace's own admin tooling (pgAdmin,
backups), the CNPG operator, and the Prometheus scrape can reach Postgres. A
breach in any other namespace cannot even open a socket to :5432.
NATS (k8s/envs/platform/nats/manifests.yaml) has nats-ingress in platform: TCP 4222 only from tomoda-api and tomoda-async pods in tomoda-dev and tomoda-prod, and TCP 7777 (metrics) only from monitoring. Per-env NATS accounts keep dev and prod subjects apart.
What is not restricted¶
Egress¶
No egress NetworkPolicy is defined. Pods can:
- Reach external APIs (Stripe, Google, Apple, DeepSeek, Serper) directly
- Connect to Postgres in the
datanamespace - Connect to Redis in the
datanamespace - Resolve DNS via kube-dns
- Reach R2 and GCP Secret Manager over HTTPS (backups, observability, ESO)
Restricting egress would need CIDR-based policies or an FQDN-aware policy controller.
Adding a NetworkPolicy¶
Add the manifest to the relevant app's directory, commit, push. Argo CD syncs.
The CNPG database policies (above) live in each cluster's manifests/
directory. A minimal ingress policy looks like:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: postgres-prod-policy
namespace: data
spec:
podSelector:
matchLabels:
cnpg.io/cluster: postgres-prod
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tomoda-prod
ports:
- protocol: TCP
port: 5432
Do not forget the operator
A policy that selects the CNPG pods with only an app-namespace rule will
lock out the CloudNativePG operator (cnpg-system) and break
reconciliation. Always allow cnpg-system and the monitoring scrape, as
the shipped postgres-{dev,prod}-policy do.
Test additions in dev first
A misconfigured NetworkPolicy can take an app down hard: default-deny kicks in as soon as any policy selects a pod for the given policyType. Apply to dev, smoke-test, then promote.
Verifying enforcement¶
# Inside a debug pod in a namespace that should be BLOCKED
kubectl run shell --rm -it --image=busybox -n default -- /bin/sh
# Inside:
wget -T 3 -O- http://backend-service.tomoda-dev.svc.cluster.local:8080/health
# Expected: "wget: download timed out"
# From inside Traefik's namespace (should succeed)
kubectl run shell --rm -it --image=busybox -n traefik-system -- /bin/sh
wget -T 3 -O- http://backend-service.tomoda-dev.svc.cluster.local:8080/health
# Expected: 200 OK
If both succeed, NetworkPolicy enforcement isn't running. k3s enforces policies with its embedded controller unless started with --disable-network-policy; check /etc/rancher/k3s/config.yaml on the server and kubectl -n kube-system logs -l k8s-app=kube-router (or the k3s journal) for policy errors.