AI Hints
Home›Kubernetes Tutorial›Ingress and Network Policies
Kubernetes · Lesson 24 of 86

Network Policies

Who may talk to whom.

⏱ 2 min read▶ Video at 1:00:39🎓 Free
Watch this lesson in the Kubernetes courseWatch on YouTube ↗Full Kubernetes course

By default, every pod in a cluster can talk to every other pod. That is convenient, but dangerous. If one pod is hacked, the attacker can reach everything. Network policies are firewall rules for pods. In this lesson, you will learn to lock down a database, so only the right apps may reach it.

Network Policies
From the video: Network Policies
  • Default: everything talks to everything
  • Policies act as pod firewalls
  • Allow only what is needed

Open by default

Here is a small system. A front end, a back end, and a database, with a stranger, a pod that should have no access. In the default setup, all of them can talk to everything. The stranger can call the database directly. A network policy changes this. It selects some pods, and says which traffic is allowed to reach them. Everything not listed, is blocked.

Open by default
From the video: Open by default

A NetworkPolicy

Here is a policy. The pod selector says which pods are protected, here, the pods labelled app equals db. The policy types field says that we control incoming traffic, ingress. Then comes the allow list. It permits traffic from pods labelled role equals app, and only on port eighty. Once a pod is selected by a policy, anything that is not allowed is denied. That is the key idea.

policy.yamlyaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-only-from-app
spec:
  podSelector:
    matchLabels:
      app: db
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: app
    ports:
    - port: 80

Locking the database

Let's prove it. We create a database, and two client pods. One is labelled role equals app. The other has no label. Before any policy, both can reach the database. Now we apply the policy. The labelled client still gets the page. The other client waits, and then times out. The packets are dropped. Note that network policies need a network plugin that enforces them, and in our cluster, it does. On some setups, policies are silently ignored, so always test.

kubectl
kubectl create deployment db --image=nginx:1.27
deployment.apps/db created
kubectl expose deployment db --port=80
service/db exposed
kubectl exec client -- wget -qO- -T 3 http://db | head -1
<!DOCTYPE html>
kubectl exec other -- wget -qO- -T 3 http://db | head -1
<!DOCTYPE html>
kubectl apply -f policy.yaml
networkpolicy.networking.k8s.io/db-only-from-app created
kubectl exec client -- wget -qO- -T 3 http://db | head -1
<!DOCTYPE html>
kubectl exec other -- wget -qO- -T 3 http://db 2>&1 | head -1
wget: download timed out
File used: policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-only-from-app
spec:
  podSelector:
    matchLabels:
      app: db
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: app
    ports:
    - port: 80

Summary

Wonderful. Network policies are pod firewalls. Once a pod is selected, only the traffic you allow can reach it. They need a network plugin that supports them. Next, we learn how to give apps their settings and secrets, without baking them into images.

Key takeaways
  • Default: all pods can talk
  • Policy selects pods, allows traffic
  • Everything else is denied
Next up: ConfigMaps and Secrets — settings and passwords for apps