👉 Overview
👀 What ?
A Network Namespace is a feature of the Linux kernel that isolates and virtualizes network interfaces and stack for processes running on the same host. It is a fundamental concept in Linux networking and containerization technologies.
🧐 Why ?
Network Namespaces are crucial in modern cloud-native architectures, as they enable each container to have its own network stack, making it appear as though it is running on its own machine with its own network interfaces. This isolation improves security by limiting the potential attack surface and allows for network configuration flexibility.
⛏️ How ?
To use a Network Namespace, you can use the `ip netns` command in Linux. First, you create a new namespace using `ip netns add`. Then, you can assign network interfaces to this namespace, and configure it as if it was a separate host. It's important to note that each namespace has its own routing tables and firewall rules.
⏳ When ?
The Network Namespace feature was introduced in the Linux kernel version 2.6.24, which was released in 2008. It has since become a fundamental part of container technologies such as Docker and Kubernetes.
⚙️ Technical Explanations
Network Namespaces are a powerful feature of the Linux kernel that allow for the isolation of network resources. This essentially means that each namespace can have its own unique set of network interfaces, routing tables, and firewall rules. This isolation is achieved through the Linux kernel's capabilities for virtualization and isolation.
When a Network Namespace is created using the ip netns add
command, a new network stack is instantiated. This stack is entirely separate from the default or any other network namespaces. The new namespace can be configured with its own network devices, either by moving an existing device from another namespace or by creating a new virtual device.
The routing tables inside a Network Namespace define how packets are forwarded. Each namespace can have its own set of routes, allowing for complex network topologies to be built. Firewall rules, governed by tools such as iptables
, are also namespaced. This means that each namespace can have its own set of rules for packet filtering and NAT.
When a process is running inside a namespace, it only has access to the network devices, routes, and firewall rules within that namespace. This encapsulation provides a level of security and isolation, as processes cannot interfere with the network settings of other namespaces.
In the context of containerization technologies like Docker and Kubernetes, Network Namespaces play a vital role. Each container is assigned its own network namespace, giving it a private network stack. This makes each container appear as though it is running on its own isolated network, improving security and allowing for network configuration flexibility.
It's important to note that while Network Namespaces provide isolation at the network level, other resources such as CPU and memory are not isolated. Other Linux features, such as cgroups, are used for this purpose.
Here's a detailed example of how to create and configure a new network namespace:
- Create a New Network Namespace:
- Check the Created Namespace:
- Create a New Virtual Ethernet Pair:
- Move
veth2
tomyNamespace
: - Configure the Interfaces:
- Test the Connectivity:
First, we'll create a new network namespace called myNamespace
using the ip netns add
command.
sudo ip netns add myNamespace
With this command, a new network namespace is created and it's completely isolated from the default namespace.
You can list out all the network namespaces using the ip netns list
command.
sudo ip netns list
You should see myNamespace
listed in the output.
Now, we'll create a new virtual ethernet (veth) pair. This pair of network interfaces can be used to create a sort of "pipe" for network communication between namespaces.
sudo ip link add veth1 type veth peer name veth2
This command creates two connected interfaces, veth1
and veth2
.
We assign veth2
to myNamespace
. This allows communication between myNamespace
and the default namespace.
sudo ip link set veth2 netns myNamespace
Now, we configure the interfaces with IP addresses.
In the default namespace:
sudo ip addr add 192.168.1.1/24 dev veth1
sudo ip link set veth1 up
And in myNamespace
:
sudo ip netns exec myNamespace ip addr add 192.168.1.2/24 dev veth2
sudo ip netns exec myNamespace ip link set veth2 up
sudo ip netns exec myNamespace ip link set lo up
Now, veth1
in the default namespace has IP address 192.168.1.1
, and veth2
in myNamespace
has IP address 192.168.1.2
.
You can test the connectivity by pinging 192.168.1.2
from the default namespace.
ping 192.168.1.2
If everything is set up correctly, you should see successful ping responses. This means that the default namespace can communicate with myNamespace
through the veth1
-veth2
pair.
This example demonstrates the creation and configuration of a network namespace with network interfaces. Each step represents a fundamental part of working with network namespaces in Linux.