8.04 Network Namespaces
Abstract
Network namespaces provide isolated network stacks on a Linux host.
Containers use network namespaces to get their own interfaces, IP addresses, routing tables, and ARP tables, separate from the host and other containers.
These concepts are important for understanding Docker networking, Kubernetes Pod networking, and CNI plugins.
What is a Namespace?
A namespace isolates resources inside Linux.
For containers, namespaces provide separation for:
- processes
- networking
- mounts
- users
- hostnames
Note
A container appears to have its own system view, but the host can still see everything.
Process Namespace
Inside a container, a process may appear as PID 1.
Example inside container:
Output may show:
On the host, the same process appears with a different PID:
Example:
Tip
The same process can have different process IDs inside and outside the container because of process namespaces.
Network Namespace
A network namespace gives an isolated network environment.
Each network namespace can have its own:
- network interfaces
- IP addresses
- routing table
- ARP table
- firewall rules
Success
Network namespaces are the foundation of container network isolation.
Host vs Network Namespace
The host has its own interfaces:
Example:
A new network namespace initially sees only loopback:
or:
Note
The namespace cannot see the host eth0 unless it is explicitly connected.
Create Network Namespaces
Create two namespaces:
List namespaces:
Example output:
Execute Commands Inside a Namespace
Use ip netns exec:
Alternative short form:
Tip
ip -n <namespace> works only for ip commands.
For other commands, use ip netns exec.
Example:
ARP and Routing Isolation
On the host:
Inside a namespace:
Initially, the namespace has:
- no ARP entries
- no external routes
- no network connectivity
Note
Each namespace maintains its own ARP and routing tables.
Connect Two Namespaces with veth Pair
A veth pair acts like a virtual network cable.
Create a veth pair:
Attach one end to each namespace:
Assign IP addresses:
Bring interfaces up:
Test connectivity:
Success
The red and blue namespaces can now communicate directly.
Check ARP Tables
Inside red namespace:
Inside blue namespace:
The host ARP table does not know about these namespace interfaces:
Note
Namespace networking is isolated from the host unless connected through a bridge or routing.
Why Linux Bridge Is Needed
A veth pair works for connecting two namespaces.
For multiple namespaces, create a virtual switch using a Linux bridge.
Options include:
- Linux Bridge
- Open vSwitch
This guide uses Linux Bridge.
Create Linux Bridge
Create bridge interface:
View it:
Bring it up:
Tip
A Linux bridge acts like a switch for namespaces and like an interface for the host.
Delete Old Direct veth Pair
If red and blue were directly connected, delete the old veth pair:
Note
Deleting one end of a veth pair automatically deletes the other end.
Connect Namespaces to Linux Bridge
Create veth pair for red:
Attach one end to namespace:
Attach bridge-side end to Linux bridge:
Create veth pair for blue:
Attach one end to namespace:
Attach bridge-side end to Linux bridge:
Configure Namespace IPs
Assign IP addresses:
Bring namespace interfaces up:
Bring bridge-side interfaces up:
Test:
Success
Both namespaces now communicate through the Linux bridge.
Connect Host to Namespace Network
The bridge is also an interface on the host.
Assign an IP to the bridge:
Now the host can reach namespaces:
Note
This creates a private network inside the host: 192.168.15.0/24.
Enable Namespace Access to LAN
Assume the host has:
| Interface | IP |
|---|---|
Bridge v-net-0 |
192.168.15.5 |
Host LAN eth0 |
192.168.1.2 |
If namespace blue wants to reach LAN network 192.168.1.0/24, add a route inside blue:
Warning
The gateway must be reachable from inside the namespace.
Use 192.168.15.5, not the host LAN IP 192.168.1.2.
Enable NAT with iptables
Even with routes, external hosts may not know how to return traffic to the namespace network.
Enable NAT masquerading on the host:
Success
NAT allows namespace traffic to reach external networks using the host’s IP address.
Enable Internet Access from Namespace
Add a default route inside the namespace:
Test internet connectivity:
Tip
A default route sends all unknown traffic through the specified gateway.
Port Forwarding to Namespace
If a namespace hosts a web app on port 80, outside clients cannot directly reach the private namespace IP.
Forward traffic from the host to the namespace:
Note
This forwards traffic arriving at the host on port 80 to namespace blue on port 80.
Network Namespace Workflow
Create namespace
↓
Create veth pair
↓
Attach veth to namespace
↓
Attach other end to bridge
↓
Assign IP address
↓
Bring interfaces up
↓
Add routes
↓
Enable NAT if external access is needed
Relation to Docker and Kubernetes
Containers use network namespaces for isolation.
Docker and Kubernetes automate the same concepts:
- create namespace
- create veth pair
- connect to bridge or CNI network
- assign IP
- configure routes
- apply NAT or policies
Example
Kubernetes CNI plugins automate these Linux networking steps for every Pod.
Production Best Practices
Recommended
- Understand namespaces before troubleshooting CNI
- Use CNI plugins instead of manual namespace networking in Kubernetes
- Keep Pod CIDRs and node CIDRs non-overlapping
- Use network policies for traffic control
- Monitor iptables and routing rules carefully
- Avoid manual changes on production nodes unless required for troubleshooting
Do's
- Use
ip netnsto inspect namespaces - Use
ip -n <namespace>for quick namespace network checks - Use Linux bridges for multi-namespace connectivity
- Use NAT when private namespaces need external access
- Validate routes and ARP tables during troubleshooting
Don'ts
- Don't assume namespaces can reach the host automatically
- Don't forget to bring interfaces up
- Don't use unreachable gateways in routes
- Don't modify iptables blindly in production
- Don't use overlapping IP ranges
Danger
Incorrect routes, NAT rules, or overlapping CIDRs can break Pod connectivity and cause hard-to-debug Kubernetes networking issues.
Troubleshooting Checklist
When namespace networking fails, check:
- Does the namespace exist?
- Are veth interfaces created?
- Is the correct veth end attached to the namespace?
- Are interfaces up?
- Are IP addresses assigned?
- Is the bridge up?
- Is routing configured?
- Is IP forwarding enabled if routing is required?
- Are iptables NAT rules correct?
- Are CIDR ranges overlapping?
Quick Commands
ip netns add red
ip netns add blue
ip netns
ip netns exec red ip link
ip -n red link
ip link add veth-red type veth peer name veth-blue
ip link set veth-red netns red
ip link set veth-blue netns blue
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 netns exec red ping 192.168.15.2
Linux bridge:
NAT:
Summary
Quote
- Network namespaces isolate interfaces, routes, and ARP tables
- veth pairs connect namespaces like virtual cables
- Linux bridges connect multiple namespaces like virtual switches
- Routes and gateways allow namespace traffic to leave its network
- NAT enables private namespaces to access external networks
- Kubernetes CNI automates these concepts for Pods