In this lesson, you will learn how to decouple the lifecycle of a network namespace from the processes running inside it to ensure your network state survives application restarts.
The Problem of Transient Namespaces
By default, a Linux network namespace (netns) is destroyed as soon as the last process inside it exits or the last interface associated with it is deleted. In high-availability environments, this behavior is a bottleneck; if a container or service crashes, you lose your routing tables, interface configurations, and connection tracking states. To maintain a stable network environment for your applications, you must move from process-bound namespaces to persistent namespaces.
Binding to the Filesystem
The core mechanism for persistence is the bind mount. A network namespace is represented as a file under /proc/[pid]/ns/net. If you map this file to a location in your filesystem, the kernel keeps the namespace alive even if all processes associated with it terminate.
You can create a persistent namespace by using the ip netns utility, which automates the mounting process:
# 1. Create a persistent namespace directory
sudo mkdir -p /etc/netns/my_app_ns
# 2. Create the namespace (this automatically creates a bind mount in /var/run/netns)
sudo ip netns add persistent_ns
# 3. Verify the bind mount exists
ls -l /var/run/netns/persistent_ns
Once the namespace is mounted at /var/run/netns/persistent_ns, it acts as a "container" for your network configuration. You can now start, stop, or crash processes within this namespace using ip netns exec persistent_ns <command> without losing your interface assignments or IP tables.
Why This Matters for Services
When you design your infrastructure this way, you decouple your networking stack from your application stack. If your application process dies, the kernel does not garbage-collect the interface because the namespace is held "open" by the bind mount. When your orchestration layer (like a supervisor or init system) restarts the application, it simply re-attaches to the existing persistent_ns. This ensures that your iptables rules, routes, and veth pairs remain intact across the entire application restart lifecycle.
Key Takeaway: By bind-mounting a network namespace to the filesystem, you break the dependency between process lifecycle and network state, enabling seamless recovery and persistence for critical network stacks.