NetworkingLesson 7 of 8
- The pod network model, not reconciled
- Services, not reconciled
- EndpointSlices and kube-proxy, not reconciled
- Following a packet through the node, not reconciled
- Cluster DNS with CoreDNS, not reconciled
- Gateway API and the frozen Ingress API, not reconciled
- NetworkPolicies, not reconciled
- gRPC, service mesh and mTLS, not reconciled
NetworkPolicies
Pod traffic is open until a NetworkPolicy selects a pod. From then on only the traffic some policy allows gets through, in that direction, and only if the network plugin enforces it.
Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.
Reconcile, in 8 scenes
Default allow
A cluster’s pod network starts flat: every pod can open a connection to every other pod, on any port, in any namespace. A NetworkPolicy changes that for the pods it selects, and only in the directions it names. Kubernetes treats ingress and egress as two separate kinds of isolationThe state of a pod that some NetworkPolicy selects for a direction. Only connections a policy allows pass in that direction.:
- A pod is isolated for ingress once any NetworkPolicy selects it and lists
IngressinpolicyTypes. From then on, the only inbound connections allowed are those some policy’singressrules permit, plus traffic from the pod’s own node. - A pod is isolated for egress once any NetworkPolicy selects it and lists
Egress. Its outbound connections are then limited to what some policy’segressrules permit.
A pod that no policy selects stays fully open, and so does any direction no policy names. That means a single policy is effective on its own: you do not need a namespace-wide default deny before an allow policy means something, because selecting a pod is what isolates it. Policies are stateful: once a connection is allowed, its reply packets flow back without a rule of their own.
For a connection between two pods, both ends get a vote. The source pod’s egress policies must allow it, and the destination pod’s ingress policies must allow it. If either side refuses, the connection fails, which is why a locked-down namespace usually needs policies in pairs.
Policy shape
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow-frontend
namespace: shop
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
spec.podSelector chooses the pods the policy applies to, in the policy’s own namespace; an empty selector (podSelector: {}) selects them all. policyTypes names the directions the policy isolates. Each ingress rule allows traffic that matches both its from peers and its ports; each egress rule does the same with to and ports. A peer is a podSelector, a namespaceSelector, both together, or an ipBlock with a CIDR and optional except ranges. A port can be a number or a named container port, and endPort (stable since Kubernetes 1.25) turns a numeric port into a range.
Policies match the pod port, not the Service port. A client that calls a Service on port 80 reaches a pod on its targetPort, and that pod port (8080 here) is what an ingress rule on the server and an egress rule on the client must name.
Policies are defined for TCP, UDP and SCTP. What happens to ICMP is up to the network plugin, so ping proves little about a policy; test with the protocol the application uses. Rules written with ipBlock should name cluster-external addresses. Pod IPs change, and whether source NAT on a load balancer happens before or after policy evaluation differs between plugins.
Combining rules
Three combination rules decide what a set of policies allows.
- Policies add up. Every policy that selects a pod contributes allows; none can take away what another grants, and order never matters. There is no deny rule in
networking.k8s.io/v1. - Entries in a
fromortolist are ORed. Traffic is allowed if it matches any one peer. - Selectors inside one entry are ANDed. An entry with both
namespaceSelectorandpodSelectormatches only those pods in those namespaces.
The difference between rules 2 and 3 is a single dash in YAML:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: metrics-and
namespace: shop
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheus
ports:
- protocol: TCP
port: 9090
This allows app=prometheus pods in the monitoring namespace to scrape the API pods on port 9090. Put a dash before podSelector and the list holds two peers: every pod in monitoring, or any app=prometheus pod in the policy’s own namespace. A podSelector without a namespaceSelector in its entry never reaches outside the policy’s namespace, and podSelector: {} alone means “every pod in this namespace”, not the whole cluster. For every pod in every namespace, use namespaceSelector: {}.
When the YAML is ambiguous to your eye, ask the API server how it parsed the policy:
kubectl -n shop describe networkpolicy metrics-and
Name: metrics-and
Namespace: shop
Created on: 2026-11-19 09:00:30 +0000 UTC
Labels: <none>
Annotations: <none>
Spec:
PodSelector: app=api
Allowing ingress traffic:
To Port: 9090/TCP
From:
NamespaceSelector: kubernetes.io/metadata.name=monitoring
PodSelector: app=prometheus
Not affecting egress traffic
Policy Types: Ingress
Events: <none>One From: heading with both selectors under it is a single ANDed peer; separate peers each get their own From: heading.
NetworkPolicy cannot name a namespace by name, only by label, so use the immutable label the control plane puts on every namespace: kubernetes.io/metadata.name=<namespace>. Many published examples select namespaces with a name label, which no namespace carries unless someone adds it, so the selector matches nothing. A common case is admitting your edge proxy: select the namespace that runs your Gateway implementation’s proxies by kubernetes.io/metadata.name, plus the proxy pods’ own labels. Examples that admit traffic from the ingress-nginx controller describe a project retired on 2026-03-24, and their app: ingress-nginx pod label did not match that controller’s pods, which were labelled app.kubernetes.io/name: ingress-nginx.
Default-deny patterns
A namespace-wide default deny selects every pod and allows nothing:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
namespace: shop
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
Listing only Ingress gives a deny-all-ingress policy, and only Egress a deny-all-egress policy. After the default deny, add one allow per real flow, named for what it permits. The usual set for a web tier is: DNS egress for everything, ingress to the frontend from the Gateway’s proxies, egress from the frontend to the API and the matching ingress on the API, and egress from the API to its database and the matching ingress there. Policies apply from the first instant a container starts, to init and sidecar containers as well as app containers, once the plugin has processed them.
Two things a default deny cannot block: traffic between a pod and its own node (so kubelet probes keep working), and a pod’s traffic to itself over loopback.
kubectl apply -f default-deny.yaml
networkpolicy.networking.k8s.io/default-deny created
kubectl -n shop exec frontend -- wget -qO- -T 2 http://api
wget: bad address 'api' command terminated with exit code 1
kubectl apply -f allow-dns.yaml
networkpolicy.networking.k8s.io/allow-dns created
kubectl -n shop exec frontend -- wget -qO- -T 2 http://api
wget: download timed out command terminated with exit code 1
kubectl apply -f frontend-api.yaml
networkpolicy.networking.k8s.io/api-allow-frontend created networkpolicy.networking.k8s.io/frontend-to-api created
kubectl -n shop exec frontend -- wget -qO- -T 2 http://api
{"service":"web","version":"1.1.0","pod":"api-866657c86f-tfgs5"}kubectl -n shop get networkpolicy
NAME POD-SELECTOR AGE allow-dns <none> 0s api-allow-frontend app=api 0s default-deny <none> 0s frontend-to-api app=frontend 0s metrics-and app=api 30s
Read the failures in order. bad address means the name never resolved, because egress to CoreDNS was dropped. download timed out means DNS works but the connection to the API pod is dropped. frontend-api.yaml holds the api-allow-frontend policy from the start of this lesson and its egress twin, frontend-to-api, which selects app=frontend and allows TCP 8080 to app=api. A dropped packet looks like a timeout, not a refusal, so a policy problem shows up as slowness before it shows up as an error.
DNS egress
Every default-deny egress policy breaks name resolution first, because a pod’s lookups go to CoreDNS in kube-system. Allow it explicitly, for every pod in the namespace:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: shop
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: 53
Three details matter. The peer selects the CoreDNS pods, not the kube-dns Service address, because policy is evaluated after kube-proxy has translated the Service IP to a pod IP; if your CoreDNS pods listen on a port other than 53, name that port. The namespace is selected by kubernetes.io/metadata.name. And the rule allows TCP as well as UDP: a DNS answer too large for UDP comes back truncated and the client retries over TCP, and RFC 7766 requires DNS implementations to support both. A policy with only UDP works until the first large answer, then fails in a way that looks random. If you run NodeLocal DNSCache, pods send queries to its node-local address instead, and that address needs an allow too.
ExplainerName resolution in a pod
Podtoolbox10.244.3.4
/etc/resolv.conf
- search
- shop.svc.cluster.localsvc.cluster.localcluster.local
- nameserver
- 10.96.0.10
- options
- ndots:5
- ledger.shop.svc.cluster.localnot sent yet
- ledger.svc.cluster.localnot sent yet
- ledger.cluster.localnot sent yet
- ledgernot sent yet
CoreDNSkube-dns10.96.0.10
- kubernetes cluster.local in-addr.arpa ip6.arpaanswers cluster.local names from the API: authoritative
- forward . /etc/resolv.confsends everything else to the node's resolver at 172.18.0.1
No query sent yet.
Answer
Waiting for the resolver.
$ kubectl -n shop exec toolbox -- nslookup ledger Resolve the name to see what nslookup prints.
The name has no dots, fewer than ndots:5, so the resolver tries every search domain before the name as typed.
kubectl get namespace kube-system --show-labels
NAME STATUS AGE LABELS kube-system Active 60s kubernetes.io/metadata.name=kube-system
kubectl -n kube-system get pods -l k8s-app=kube-dns
NAME READY STATUS RESTARTS AGE coredns-7dcd66b979-9xzmq 1/1 Running 0 60s coredns-7dcd66b979-fpcdz 1/1 Running 0 60s
An allowed DNS path lets the pod resolve any name, including external ones. Restricting which domains a pod may reach is outside networking.k8s.io/v1; it needs a plugin with FQDN policies.
Enforcement
A NetworkPolicy is only an API object. The network plugin enforces it, usually through iptables, nftables or eBPF programs on each node, by watching policies, pods and namespaces and translating selectors into IP sets. A plugin that does not implement NetworkPolicy accepts the objects and ignores them: the API server stores your default deny and every packet keeps flowing. Nothing in the API reports whether a policy is enforced or when the plugin finished processing it, so test with real traffic from the real source pod.
Kubernetes ships no network plugin of its own. Some guides call kubenet “the default CNI”; kubenet was a kubelet-internal plugin, never a CNI plugin, it never enforced NetworkPolicy, and the kubelet’s --network-plugin and --cni-* flags were removed with dockershim in Kubernetes 1.24, when the container runtime took over CNI. Calico, Cilium and the Amazon VPC CNI enforce policy, and kind clusters from v0.24.0 on enforce it through kindnet.
Enforcement has edges the API leaves to the implementation. A pod created before the plugin has processed a policy may start unprotected, and isolation applies once the plugin catches up. Whether a policy change cuts existing connections is implementation-defined. Behaviour for hostNetwork pods is undefined; a plugin that cannot tell them apart treats their traffic as traffic from the node’s IP.
kubectl --context kind-kx create namespace shopkubectl --context kind-kx -n shop create deployment api --image=nginx:1.30kubectl --context kind-kx -n shop expose deployment api --port=80kubectl --context kind-kx -n shop run frontend --image=busybox:1.37 --labels=app=frontend -- sleep 3600kubectl --context kind-kx -n shop rollout status deployment apikubectl --context kind-kx -n shop wait --for=condition=Ready pod/frontend --timeout=60skubectl --context kind-kx -n shop exec frontend -- wget -qO- -T 2 http://apikubectl --context kind-kx apply -f default-deny.yaml -f allow-dns.yamlkubectl --context kind-kx -n shop exec frontend -- wget -qO- -T 2 http://api
For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. Save the default-deny and allow-dns policies above first. The first request returns the nginx welcome page; after the policies, the name resolves but the request times out, because nothing allows frontend to reach api yet.
Cluster-wide guardrails
NetworkPolicy is namespaced and allow-only, so it cannot express a rule a namespace owner is unable to undo: anyone who can create policies in a namespace can add an allow. Platform teams use cluster-scoped policies for that. The SIG Network policy API project defines ClusterNetworkPolicy in policy.networking.k8s.io. Its v1alpha2 version (network-policy-api v0.2.0) replaced the earlier AdminNetworkPolicy and BaselineAdminNetworkPolicy, and v0.3.0 serves it as v1beta1. An Admin-tier policy is evaluated before any NetworkPolicy and can Accept, Deny or Pass traffic down to the namespace owners. A Baseline-tier policy is evaluated after them and sets defaults that a NetworkPolicy can override. These are CRDs implemented by some network plugins, not built-in APIs. Plugins also have their own cluster-wide resources, such as Calico’s GlobalNetworkPolicy and Cilium’s CiliumClusterwideNetworkPolicy. Cilium’s policyAuditMode logs what a policy would drop without dropping it, and it is meant for building policies, not for production.
Zero trust
NetworkPolicy limits reachability: which addresses may open connections to which ports. It does not authenticate anything. Selectors become IP addresses, pod IPs are reused, and a compromised pod that keeps its labels keeps its access. Zero-trust designs add workload identity on top, usually mutual TLS from a service mesh, where each connection proves which service account sent it and an authorization policy decides what that identity may call.
The mesh layer has its own traps that published guides often get wrong. Istio issues workload certificates with a default lifetime of 24 hours and rotates them automatically. A RequestAuthentication on its own does not enforce JWTs: a request with no token still passes, so pair it with an AuthorizationPolicy that requires requestPrincipals. An Istio egress gateway needs a Gateway, a DestinationRule and a VirtualService with two hops, one from the sidecar to the gateway and one from the gateway out. And YAML that describes “behavioural analysis” with a made-up kind is pseudo-code: no standard resource defines it, so the API server rejects it unless a matching CRD is installed. The lesson on gRPC, service mesh and mTLS covers how a mesh issues those identities.
Actual state
Check your understanding
Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.
Lesson not yet reconciled
Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met
- Write a default-deny policy and the allow rules a workload needs. (not yet met)
- Predict how multiple policies, and the entries inside one rule, combine. (not yet met)
- Allow DNS egress on UDP and TCP 53 to CoreDNS by namespace and pod labels. (not yet met)
- Say which component enforces policy and what happens on a network plugin that does not. (not yet met)
Pass every check above to reconcile this lesson.
Every check has passed, so the desired state is met. First-try accuracy: 0 of 6. This lesson's review cards are in your queue.
Practice
Put this lesson to work on the simulated cluster. Everything runs in your browser, with nothing to install.
Changed since the notes
This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.
Show 19 corrections
The notes saidAWS VPC CNI now offers native Network Policy support; enable through EKS add-on configuration.
CorrectedEnable with add-on configuration {"enableNetworkPolicy": "true"} on VPC CNI v1.14.0-eksbuild.3 or later (cluster 1.26.7/eks.6 or 1.27.4/eks.5 or later, Linux kernel 5.10+); the network policy agent runs in the aws-node DaemonSet (metrics port 8162, health 8163). VPC CNI 1.21+ also enforces cluster-scoped ClusterNetworkPolicy (networking.k8s.aws/v1alpha1, Admin and Baseline tiers with Deny/Allow/Pass). Policies apply only to EC2 Linux nodes (not Fargate or Windows). In EKS Auto Mode you enable it with the amazon-vpc-cni ConfigMap key enable-network-policy-controller, and NodeClass networkPolicyEventLogs turns on policy event logging.
The notes saidCalico is commonly used in EKS; install only Calico policy components alongside VPC CNI.
CorrectedAWS best practices now position the VPC CNI network policy engine as the default and recommend running only one policy engine per cluster; they provide a migration tool (k8s-network-policy-migrator) from Calico/Cilium CRDs. Calico (or Cilium) is recommended only for requirements beyond native support, such as global policies, L7 rules, service-account-based rules or explicit deny/log actions.
Sourcedocs.aws.amazon.com/eks/latest/best-practices/network-security.html
The notes saidNo explicit deny rules; you can only allow traffic.
CorrectedTrue for networking.k8s.io/v1 NetworkPolicy (still allow-only and without logging in Kubernetes 1.37). Cluster-scoped policies with Deny exist outside core: upstream SIG Network's ClusterNetworkPolicy (policy.networking.k8s.io/v1alpha2, network-policy-api v0.2.0) replaced AdminNetworkPolicy/BaselineAdminNetworkPolicy, with Admin and Baseline tiers and Accept/Deny/Pass actions; EKS VPC CNI 1.21+ implements its own ClusterNetworkPolicy (networking.k8s.aws/v1alpha1) with Deny.
The notes saidAllow from ingress controller example uses the ingress-nginx namespace.
CorrectedIngress NGINX was retired: best-effort maintenance ended in March 2026 and the project receives no further releases, bug fixes or security patches (its repositories are read-only). Use a Gateway API implementation (or another maintained ingress controller) as the canonical example, selecting its namespace by kubernetes.io/metadata.name.
Sourcekubernetes.io/blog/2025/11/11/ingress-nginx-retirement
The notes saidLimiting DNS resolution requires additional tools; Cilium provides DNS-based policies.
CorrectedEKS Auto Mode supports DNS-based (FQDN and wildcard domain) egress policies natively through the ApplicationNetworkPolicy CRD and cluster-scoped DNS rules; this applies only to workloads on Auto Mode-launched EC2 instances. On non-Auto Mode clusters, standard VPC CNI policies remain IP/port-based, so FQDN rules still need Cilium or Calico.
Sourcedocs.aws.amazon.com/eks/latest/userguide/auto-net-pol.html
The notes saidCalls kubenet 'the default kubenet CNI'.
Correctedkubenet was a kubelet-internal network plugin, not a CNI plugin, and Kubernetes has never shipped a default CNI. The kubelet's --network-plugin and --cni-* flags were removed in Kubernetes 1.24 with dockershim; CNI management is the container runtime's job. kubenet also never enforced NetworkPolicy.
Sourcekubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins
The notes saidIntro says 'Traffic not explicitly allowed by a policy is denied'.
CorrectedPods are non-isolated by default. A pod becomes isolated for ingress (or egress) only when some NetworkPolicy selects it and lists Ingress (or Egress) in policyTypes; only then is traffic not allowed by some policy dropped, and only in that direction. Pods no policy selects stay fully open.
Sourcekubernetes.io/docs/concepts/services-networking/network-policies
The notes saidDNS egress example allows only UDP 53.
CorrectedDNS uses TCP as well as UDP (truncated responses, large records, DNSSEC); RFC 7766 requires general-purpose DNS implementations to support both. Egress rules to cluster DNS must allow both UDP 53 and TCP 53 (and CoreDNS's target port if it differs).
The notes saidSelects namespace label name: ingress-nginx and controller pods app: ingress-nginx.
CorrectedNamespaces do not get a 'name' label; the control plane sets the immutable label kubernetes.io/metadata.name=<namespace>. The ingress-nginx manifests label the namespace and controller pods app.kubernetes.io/name: ingress-nginx (pods also app.kubernetes.io/component: controller), not app: ingress-nginx, so the example selects nothing.
The notes saidIRSA as workload identity; IRSA gives per-pod credentials
CorrectedIRSA gives every pod of a service account the same role. Pod Identity is preferred, and its session tags (kubernetes-pod-name, kubernetes-pod-uid and others) identify the pod.
Sourcedocs.aws.amazon.com/eks/latest/userguide/pod-id-abac.html
The notes saidNetwork policies are enforced on EKS
CorrectedEnforcement requires enableNetworkPolicy in the VPC CNI. EKS also supports ClusterNetworkPolicy with Admin and Baseline tiers.
Sourcedocs.aws.amazon.com/eks/latest/userguide/cni-network-policy.html
The notes saidCilium monitoring mode for policies
CorrectedThe setting is policyAuditMode and is not meant for production. Upstream, ClusterNetworkPolicy v1alpha2 replaces AdminNetworkPolicy and BaselineAdminNetworkPolicy.
The notes saidIstio mTLS certificates rotate frequently
CorrectedThe default certificate TTL is 24h.
The notes saidWithout a default deny, a NetworkPolicy is ineffective
CorrectedSelecting a pod isolates it for the listed policyTypes.
Sourcekubernetes.io/docs/concepts/services-networking/network-policies
The notes saidpodSelector: {} allows traffic cluster-wide
CorrectedpodSelector: {} covers only the same namespace. Add namespaceSelector: {} for cluster-wide.
Sourcekubernetes.io/docs/concepts/services-networking/network-policies
The notes saidDNS egress allows UDP 53 only
CorrectedAlso allow TCP 53, and the NodeLocal DNSCache IP if you use it.
Sourcekubernetes.io/docs/concepts/services-networking/network-policies
The notes saidIstio egress gateway configuration
CorrectedAn egress gateway needs a Gateway, a DestinationRule and a two-hop VirtualService.
Sourceistio.io/latest/docs/tasks/traffic-management/egress/egress-gateway
The notes saidBehavioral analysis YAML presented as configuration
CorrectedIt is pseudo-code. No standard resource defines that kind, so the API server rejects it unless a matching CRD is installed.
Sourcekubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources
The notes saidRequestAuthentication alone enforces JWT
CorrectedRequests without a token pass. Enforce with an AuthorizationPolicy using requestPrincipals.
Sourceistio.io/latest/docs/reference/config/security/request_authentication
Every correction across the guide, by note and topic.
Sources
Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .
- kubernetes.io/docs/concepts/services-networking/network-policies
- kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins
- rfc-editor.org/rfc/rfc7766
- github.com/kubernetes-sigs/kind/releases/tag/v0.24.0
- network-policy-api.sigs.k8s.io/blog/2025/10/09/api-update-for-v1alpha2-clusternetworkpolicy-replaces-adminnetworkpolicy-and-baselineadminnetworkpolicy
- github.com/kubernetes-sigs/network-policy-api/releases/tag/v0.3.0
- kubernetes.io/blog/2025/11/11/ingress-nginx-retirement
- istio.io/latest/about/faq/security
- istio.io/latest/docs/reference/config/security/request_authentication
- istio.io/latest/docs/tasks/traffic-management/egress/egress-gateway
- docs.cilium.io/en/stable/security/policy-creation
- docs.aws.amazon.com/eks/latest/userguide/cni-network-policy.html
- docs.aws.amazon.com/eks/latest/userguide/cni-network-policy-configure.html
- docs.aws.amazon.com/eks/latest/userguide/auto-net-pol.html
- docs.aws.amazon.com/eks/latest/best-practices/network-security.html
- docs.aws.amazon.com/eks/latest/userguide/pod-id-abac.html
NetworkPolicies
↓ ↑ step, 1 to 8 scene, Esc leave