SecurityKubernetes
Kubernetes network policies: start with default deny
By default, all inbound and outbound connections of a pod are allowed. A default-deny policy isolates every pod in a namespace, and from then on you add an allow rule for each flow, DNS included.

What happens with no policy
A namespace with no policies allows every ingress and egress connection of its pods.
A pod is isolated for ingress when a NetworkPolicy selects it and has Ingress in its policyTypes. Egress works the same way with Egress. Once a pod is isolated in a direction, the only connections allowed in that direction are the ones some policy allows. Ingress from the pod's own node is the documented exception.
Policies do not conflict. They are additive, and the order of evaluation does not affect the result. A connection between two pods needs two approvals: the source pod's egress policy and the destination pod's ingress policy must each allow it.
The plugin that enforces it
Network policies are implemented by the network plugin. Creating a NetworkPolicy without a controller that implements it has no effect.
The Kubernetes walkthrough names Antrea, Calico, Cilium and Kube-router among the network providers that support NetworkPolicy.
Default deny for a namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressThis is the documented policy that prevents all ingress and egress traffic in the namespace where you create it. An empty podSelector selects all pods in the namespace. Pods that no other NetworkPolicy selects are then allowed no ingress or egress traffic.
The API has no default policies that apply to all namespaces. Create this object in each namespace you want to isolate.
Allow rules for DNS and each flow
A default-deny egress policy also blocks DNS traffic. The documentation's answer is one more NetworkPolicy, which permits egress to the DNS service of your cluster. This one is built from documented fields:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53Its values describe the example cluster in the Kubernetes DNS debugging page. There, the CoreDNS pods run in kube-system with the label k8s-app=kube-dns, and their endpoints listen on port 53. The control plane sets the label kubernetes.io/metadata.name on every namespace, with the namespace name as its value. Run kubectl get endpointslice -l kubernetes.io/service-name=kube-dns --namespace=kube-system to see the ports in your cluster, and change the selectors and ports to match your DNS setup.
Then add a policy for each application flow. The Kubernetes walkthrough uses this one. It lets pods with the label access: "true" reach the nginx pods:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: access-nginx
spec:
podSelector:
matchLabels:
app: nginx
ingress:
- from:
- podSelector:
matchLabels:
access: "true"The rule for both ends applies here too: the egress policy on the source pod must allow the connection, as well as the ingress policy on the destination.
Mind the list syntax under from and to. A namespaceSelector and a podSelector in one list element select pods with that label inside namespaces with that label. Written as two elements, they select pods in the policy's own namespace by the pod label, or all pods in the namespaces that carry the namespace label. Run kubectl describe on the policy to see how Kubernetes has interpreted it.
How to test a policy
The walkthrough creates an nginx Deployment and Service, applies access-nginx, and tests from two throwaway pods:
kubectl run busybox --rm -ti --image=busybox -- /bin/sh
kubectl run busybox --rm -ti --labels="access=true" --image=busybox -- /bin/shIn each shell it runs wget --spider --timeout=1 nginx. From the pod with no label, the documented output ends with wget: download timed out. From the pod with access=true, it ends with remote file exists.
The documentation states that a network plugin handles every new policy eventually, and that the Kubernetes API offers no way to tell when that has happened.
API limits and cluster-wide policies
The documentation lists what the NetworkPolicy API cannot do. Among the items:
- It is defined for layer 4 connections: TCP, UDP and optionally SCTP. For other protocols, such as ICMP, the behavior may vary across network plugins.
- The model is deny by default with allow rules only. Explicit deny rules do not exist.
- It cannot target Services by name, and it handles nothing related to TLS.
- It has no default policies that apply to all namespaces or pods.
- It cannot log network security events, such as blocked or accepted connections.
- A pod cannot block access from the node it runs on, or from localhost.
The Network Policy API project, a subgroup of SIG Network, works on network security beyond NetworkPolicy v1. Its active API is ClusterNetworkPolicy. The project lists AdminNetworkPolicy and BaselineAdminNetworkPolicy as previous APIs that are no longer actively developed. Release v0.3.0 graduated ClusterNetworkPolicy to v1beta1.
A ClusterNetworkPolicy sets cluster-wide rules that are evaluated before or after the NetworkPolicies of namespace owners. The tiers are evaluated in this order: Admin, NetworkPolicy, Baseline. Each rule has one action: Accept, Deny or Pass. The resources are CRDs in the policy.networking.k8s.io API group. The project's implementations page lists kube-network-policies, Kube-OVN and Calico for ClusterNetworkPolicy.
First steps
- Confirm that your network plugin implements NetworkPolicy. A policy has no effect otherwise.
- Apply the default-deny policy to one namespace, then the DNS rule, then one policy for each flow.
- Label namespaces and pods on purpose. A policy cannot name a namespace directly. It selects namespaces and pods by their labels.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Pod Security Standards: baseline, restricted and how to enforce them
Security · 4 min read
Three levels, six namespace labels and a rollout that warns before it blocks.

Kubernetes Secrets: encryption at rest and who can read them
Security · 4 min read
How Secrets are stored, how to encrypt them in etcd, and who can still read them.

Kyverno policies: block what should never deploy
Security · 4 min read
Five policy types, one documented example, and what happened to ClusterPolicy in 1.20.