How to Configure K8 Network Policy
A k8 network policy controls which pods can send or receive traffic inside your cluster. You set it up by matching pod labels and defining allowed incoming and outgoing rules. By default, Kubernetes lets every pod talk to every other pod on the network. This open setup makes it easy for apps to work right out of the box. Still, it also means one compromised pod can reach your core databases and services. Adding network rules limits this blast radius and keeps internal traffic locked down. How
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
A k8 network policy controls which pods can send or receive traffic inside your cluster. You set it up by matching pod labels and defining allowed incoming and outgoing rules.
By default, Kubernetes lets every pod talk to every other pod on the network. This open setup makes it easy for apps to work right out of the box. Still, it also means one compromised pod can reach your core databases and services. Adding network rules limits this blast radius and keeps internal traffic locked down.
How a k8 network policy works in practice
Kubernetes does not enforce network rules on its own. Your cluster needs a container network plugin that supports policy rules, like Calico or Cilium. Without a compatible plugin, the cluster accepts your policy objects but does nothing with them. Always check that your network plugin enforces rules before you rely on them for safety.
Each policy applies to a single namespace and uses labels to pick target pods. When you attach a policy to a pod, that pod becomes isolated for the chosen direction. You can filter traffic based on three main selectors. First, pod selectors match other pods by their labels in the same namespace. Second, namespace selectors allow traffic from all pods inside another labeled namespace. Third, IP block rules let you allow or block external address ranges outside the cluster.
The safest way to start is with a default deny rule for each namespace. A default deny rule selects all pods in the namespace and lists no allowed sources. Once that baseline is in place, no pod can talk to another without an explicit rule. From there, you add small rules for each app pair that must share data. Because rules are additive, Kubernetes combines all matching policies to decide if traffic can pass.
How to check rules and what they leave open
After you apply a rule, you must test it from inside the cluster. Teams often run a test pod in the target namespace and try to reach other services. You should test both allowed paths and blocked paths to confirm the plugin works. Many network plugins also write flow logs that show dropped packets and matched rules. Reviewing these flow logs helps you spot missing rules when an app fails to connect.
End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Keep in mind that label-based rules need strict access controls to stay safe. If a user can edit pod labels, they can bypass your network rules right away. Make sure your role rules stop regular users from changing labels on sensitive namespaces. You should also review your policy files during every code change to prevent drift.
Even a strict k8 network policy only controls packet flow at layers three and four. It checks IP addresses and port numbers, not the human user or the command inside. Once a developer opens an interactive shell or port forward, the network rule cannot help. It cannot see which queries run against a database or mask sensitive fields like emails. Controlling what happens inside an active session requires inspection above the raw network layer.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
Why is my k8 network policy not blocking any traffic?
Your cluster likely uses a basic network plugin that does not enforce policy rules. You must install a plugin like Calico or Cilium for rules to take effect.
Do network policies block traffic between pods on the same node?
Yes, a working network plugin enforces rules across all pods, even on the same node. Local pod traffic still passes through the virtual network interface where rules run.
Can a k8 network policy inspect database queries or API calls?
No, standard network policies only filter traffic by IP addresses, pod labels, and port numbers. They cannot read the payload, check user identity, or mask card numbers in results.
Related guides
In this post