Lesson 106 · Section 9: Networking
Lesson 106: Network namespaces: how a container gets its own network
The Linux feature under every Pod's network: what a network namespace isolates, how to create one, connect two with a veth pair, join many with a bridge, and reach the outside with routing and NAT. These are the same steps a CNI plugin runs for each Pod. · 16 min read · 3 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why containers need this
A container is a process on a Linux host. Without any isolation, it would see the host's network interfaces, could listen on the host's ports, and would share the host's IP address. Two web servers that both want port 80 could never run on the same machine.
Linux solves this with namespaces: each kind hides one part of the system from a process. The process ID namespace hides other processes. The mount namespace hides the host's file systems. And the network namespace hides the host's network and gives the process a fresh, empty one.
This lesson is about that last kind. You will build, by hand and with nothing but the ip command, the same
network that a container runtime and a CNI plugin build for every Pod. Nothing in it is specific to Kubernetes,
and that is exactly why it is useful. When you later see a veth interface or a bridge named cni0 on a node, you
will know what it is, who made it and how to inspect it.
What a network namespace isolates
Parts:
- Host namespace (eth0, routes, rules), in One Linux host
- Namespace red (lo only), in One Linux host
- Namespace blue (lo only), in One Linux host
- LAN (192.168.1.0/24)
Connections:
- Host namespace to LAN: eth0
- Host namespace to Namespace red: isolated
- Namespace red to Namespace blue: isolated
| Each namespace has its own | Seen with |
|---|---|
| Network interfaces | ip link |
| IP addresses | ip addr |
| Routing table | ip route |
| Neighbour (ARP) table | ip neigh |
| Firewall and NAT rules | iptables -L, nft list ruleset |
| Port numbers (who listens on port 80) | ss -tlnp |
The host itself lives in a namespace too, the one the system booted with. It is called the host, root or default network namespace.
A new namespace contains exactly one interface, the loopback lo, and it is switched off. There is no route, no
address, and no connection to anything.
Creating and entering namespaces
ip netns add red
ip netns add blue
ip netns
blue
red
To run a command inside a namespace, prefix it with ip netns exec <name>:
ip netns exec red ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
The host's eth0 is not in the list. The same is true for routes and ARP entries: ip netns exec red ip route
prints nothing.
For ip commands there is a shorter form, the -n option:
ip -n red link # the same as: ip netns exec red ip link
ip netns exec works with any program, for example ip netns exec red ping ... or ip netns exec red bash for a
shell that lives in the namespace.
Connecting two namespaces: a veth pair
Parts:
- Namespace red (192.168.15.1/24)
- veth pair (veth-red, veth-blue)
- Namespace blue (192.168.15.2/24)
Connections:
- Namespace red and veth pair: end
- veth pair and Namespace blue: end
Two machines are connected with a cable. Two namespaces are connected with a veth pair (virtual Ethernet): two virtual interfaces that are joined back to back. A packet sent into one comes out of the other.
# 1. Create the pair: two interfaces, veth-red and veth-blue
ip link add veth-red type veth peer name veth-blue
# 2. Move each end into its namespace
ip link set veth-red netns red
ip link set veth-blue netns blue
# 3. Give each end an address in the same network
ip -n red addr add 192.168.15.1/24 dev veth-red
ip -n blue addr add 192.168.15.2/24 dev veth-blue
# 4. Switch both ends on
ip -n red link set veth-red up
ip -n blue link set veth-blue up
ip netns exec red ping -c 1 192.168.15.2
64 bytes from 192.168.15.2: icmp_seq=1 ttl=64 time=0.045 ms
This is the lesson Switching, Routing and Gateways again, in miniature: two interfaces in the same network, joined
by a cable. The host knows nothing about 192.168.15.0/24. Its own ip neigh and ip route do not show these
addresses, because the two ends now live in other namespaces.
Connecting many: a bridge
With three namespaces you would need three cables, with ten you would need forty-five. Real networks solve this with a switch, and Linux has a virtual one built in: the bridge.
Parts:
- Namespace red (192.168.15.1), in One Linux host
- Namespace blue (192.168.15.2), in One Linux host
- Bridge v-net-0 (192.168.15.5/24), in One Linux host
- Host eth0 (192.168.1.2), in One Linux host
- LAN, internet (192.168.1.0/24)
- ip_forward + NAT (MASQUERADE), in One Linux host
Connections:
- Namespace red and Bridge v-net-0: veth pair
- Namespace blue and Bridge v-net-0: veth pair
- Bridge v-net-0 to Host eth0: host routes
- Host eth0 to LAN, internet: out
- ip_forward + NAT to Host eth0: needed
A bridge is created in the host namespace. To the host it looks like one more interface. To the namespaces it behaves like a switch they are all plugged into.
# Remove the direct cable from the last step (deleting one end removes both)
ip -n red link del veth-red
# The switch
ip link add v-net-0 type bridge
ip link set dev v-net-0 up
# One cable per namespace: one end inside, the other end on the bridge
ip link add veth-red type veth peer name veth-red-br
ip link set veth-red netns red
ip link set veth-red-br master v-net-0
ip link add veth-blue type veth peer name veth-blue-br
ip link set veth-blue netns blue
ip link set veth-blue-br master v-net-0
# Addresses and state
ip -n red addr add 192.168.15.1/24 dev veth-red
ip -n blue addr add 192.168.15.2/24 dev veth-blue
ip -n red link set veth-red up
ip -n blue link set veth-blue up
ip link set veth-red-br up
ip link set veth-blue-br up
master v-net-0 is the step that plugs the host end of a cable into the bridge. Every namespace attached this way
can reach every other one.
Letting the host in
The host still cannot ping 192.168.15.1. It owns the bridge, and yet it has no address in that network. Give the
bridge interface one:
ip addr add 192.168.15.5/24 dev v-net-0
ping -c 1 192.168.15.1
The host is now a member of the namespace network, on 192.168.15.5. That address will be the gateway of the
namespaces.
Reaching the outside world
The host has a real interface eth0 with 192.168.1.2 on the LAN. A namespace knows only 192.168.15.0/24:
ip netns exec blue ping 192.168.1.3
ping: connect: Network is unreachable
Three things are missing, and each one is from the last two lessons.
1. A route. The namespace needs a gateway. The host, on the bridge address, is the only machine with a foot in both networks:
ip -n blue route add default via 192.168.15.5
2. Forwarding. The host must be willing to pass packets from the bridge to eth0:
sysctl -w net.ipv4.ip_forward=1
3. NAT. The packet now leaves with the source address 192.168.15.2. The machines on the LAN have never heard
of that network and cannot send a reply. The host must replace the source address with its own on the way out:
iptables -t nat -A POSTROUTING -s 192.168.15.0/24 -j MASQUERADE
Now a namespace reaches the LAN and, through the host's own default gateway, the internet.
Letting the outside in
Traffic in the other direction has the mirror problem: nobody outside can address 192.168.15.2. The usual answer
is port forwarding: a rule on the host that sends one of its own ports to a namespace.
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.15.2:80
A request to 192.168.1.2:8080 now arrives at port 80 in the blue namespace. This is what docker run -p 8080:80
does, and a NodePort Service is the same idea applied to a whole cluster.
Under the hood · Why NAT here, when Kubernetes says 'no NAT'?
The network model says that Pods reach each other without NAT. That rule is about traffic inside the cluster, between Pod addresses. A network plugin makes Pod addresses routable between nodes, with real routes or with tunnels, so that no translation is needed there.
Traffic that leaves the cluster is a different case. The outside world has no route to 10.244.x.x, exactly as
the LAN above had none to 192.168.15.x. So nodes masquerade outgoing Pod traffic behind the node's address,
with the same MASQUERADE rule you just wrote. The model is not broken: it never promised that Pod addresses are
visible outside the cluster.
The whole recipe
| Step | Command |
|---|---|
| Create a namespace | ip netns add red |
| Create a bridge and switch it on | ip link add v-net-0 type bridge then ip link set v-net-0 up |
| Create a veth pair | ip link add veth-red type veth peer name veth-red-br |
| Put one end in the namespace | ip link set veth-red netns red |
| Plug the other end into the bridge | ip link set veth-red-br master v-net-0 |
| Assign an address | ip -n red addr add 192.168.15.1/24 dev veth-red |
| Switch both ends on | ip -n red link set veth-red up and ip link set veth-red-br up |
| Give the bridge an address | ip addr add 192.168.15.5/24 dev v-net-0 |
| Default route in the namespace | ip -n red route add default via 192.168.15.5 |
| Forwarding and NAT on the host | sysctl -w net.ipv4.ip_forward=1 and the MASQUERADE rule |
Read the list once more and replace three words: "namespace" with Pod, "v-net-0" with cni0, and "you" with the CNI plugin. That is Pod networking on a single node.
Where this meets Kubernetes
| What you built by hand | In a cluster |
|---|---|
| A network namespace | One per Pod. The container runtime creates it together with the Pod's sandbox (the pause container), and all containers of the Pod join it. That is why they share one IP address and talk over localhost |
| A veth pair | One per Pod: eth0 inside the Pod, a veth… or cali… interface on the node |
The bridge v-net-0 | cni0 with bridge-based plugins such as Flannel. Routed plugins such as Calico use no bridge, only routes |
The address 192.168.15.x | A Pod IP from the node's slice of the Pod range, handed out by the plugin's IPAM |
The gateway 192.168.15.5 | The bridge address on the node, for example 10.244.1.1 |
| You, typing commands | The CNI plugin, called by the container runtime each time a Pod is created or deleted |
Containers started with hostNetwork: true skip all of this. They run in the host namespace, see the node's
interfaces and use the node's IP address. The control plane's static Pods and most network plugins themselves run
that way.
Using it
Example · Find the namespace and interfaces of a Pod, from the node
On a node that uses containerd, the Pods' namespaces are visible to ip netns:
ip netns
cni-4f2a9c1e-77b0-41a2-9d53-0c1be6a7d2f1 (id: 1)
cni-b81d3e52-1c9a-4f6e-8a40-52e7f3c9a016 (id: 0)
Look inside one, with the node's own tools:
ip -n cni-4f2a9c1e-77b0-41a2-9d53-0c1be6a7d2f1 -br addr
ip -n cni-4f2a9c1e-77b0-41a2-9d53-0c1be6a7d2f1 route
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if7 UP 10.244.1.7/24
default via 10.244.1.1 dev eth0
10.244.0.0/16 via 10.244.1.1 dev eth0
10.244.1.0/24 dev eth0 proto kernel scope link src 10.244.1.7
A Pod address, an eth0 that is one end of a veth pair, and a default route to the bridge. It is the hand-built
setup with other numbers.
Example · Match a Pod's eth0 to its interface on the node
The name eth0@if7 holds a clue: @if7 means "my other end is interface number 7 in another namespace". Look
for number 7 on the node:
ip link | grep '^7:'
7: veth1a2b3c4d@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP mode DEFAULT
veth1a2b3c4d is the node end of that Pod's cable, and master cni0 shows that it is plugged into the bridge.
bridge link or ip link show master cni0 lists every interface attached to the bridge: one per Pod on this node.
Example · Debug a Pod's network when its image has no tools
Many images contain no ip, ping or ss. You do not need them inside the image. A network namespace is separate
from the file system, so you can enter only the Pod's network namespace and keep using the node's programs.
Find the process ID of one of the Pod's containers, then use nsenter:
crictl ps --name web -q
crictl inspect --output go-template --template '{{.info.pid}}' 3f9d2c81a7b4
3f9d2c81a7b4e0c6...
24817
nsenter -t 24817 -n ip -br addr
nsenter -t 24817 -n ss -tlnp
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if7 UP 10.244.1.7/24
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:8080 0.0.0.0:* users:(("nginx",pid=24817,fd=6))
-t names the process whose namespace to enter and -n says "its network namespace only". The second command
answers a very common question: on which port does the application in this Pod really listen?
Example · Clean up after an experiment
ip netns del red
ip netns del blue
ip link del v-net-0
iptables -t nat -D POSTROUTING -s 192.168.15.0/24 -j MASQUERADE
Deleting a namespace removes the interfaces inside it, and deleting one end of a veth pair removes the other end as well. Only the bridge and the firewall rule on the host need their own commands.
Tricks
Common mistakes
- Forgetting to switch interfaces on. Both ends of every veth pair, and the bridge, must be
UP. - Adding an address without a prefix.
ip addr add 192.168.15.1 dev veth-redcreates a/32, a network of one, and the namespace cannot reach its neighbours. - Attaching only one side. One end of the pair goes into the namespace, the other must be given
masteron the bridge. An end left alone in the host namespace connects to nothing. - Expecting the host to reach the namespaces without an address on the bridge. The bridge switches traffic between namespaces, but the host joins that network only when the bridge interface has an IP address.
- Forgetting forwarding or NAT. With a default route and no
ip_forward, packets stop at the host. With forwarding and noMASQUERADE, they leave and the replies never return. - Looking for Docker's namespaces with
ip netns. That command lists only namespaces linked under/var/run/netns. Docker does not link its namespaces there, so the list is empty.lsns -t netshows them all. - Expecting two containers of one Pod to use the same port. They share one network namespace, so port 80 can be taken only once per Pod.
Recap
- A network namespace is a private network stack: interfaces, addresses, routes, firewall rules and ports. A
new one holds only a
lothat is down. ip netns add,ip netns exec <name> <command>andip -n <name> ...create and inspect namespaces.- A veth pair is a virtual cable. One end goes into the namespace, the other into a second namespace or onto a bridge, the host's virtual switch.
- An address on the bridge makes the host the gateway.
ip_forwardand aMASQUERADErule take traffic out, and aDNATrule lets it in. - A Pod is one network namespace, shared by its containers. The CNI plugin runs these same steps for each Pod.
- On a node,
ip netns,ip -n <namespace> ..., the@ifindex andnsenter -t <pid> -nlet you inspect any Pod's network with the node's tools.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Build what a network plugin builds for pods, with nothing but ip: two network namespaces, a bridge on the host, a veth pair for each, addresses and a way out.
Build · Level 3 · challenge · target 25 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.