2. Connecting two network namespaces (ns1, ns2) - with a veth pair
The Slovak original of this document: 2. Prepojenie dvoch sieťových menných priestorov (ns1, ns2) - pomocou páru veth adaptérov (slovensky).
2016 - Linux NET Namespace - Connecting two network namespaces (ns1, ns2) - with a veth pair
1 Schéma
+--------------------+ +--------------------+
(PID = 12112) | ns1 veth1 |=========kabel=========| veth2 ns2 | (PID = 12130)
+--------------------+ +--------------------+
namespace "ns1" namespace "ns2"2 Creating network namespaces
Create two network (NET) namespaces, "ns1" and "ns2".
# ip netns add ns1 # ip netns add ns2
3 Running processes inside namespaces
- [1]TERM1 - In network namespace "ns1", exec the command "bash". PID = 12112
- [2]TERM1 - Find the PID of the BASH process.
- [3]TERM2 - In network namespace "ns2", exec the command "bash". PID = 12130
- [4]TERM2 - Find the PID of the BASH process.
[1]TERM1# ip netns exec ns1 bash [2]TERM1# echo $$ ---------------------------------------------------------------------------------------------------------------- 12112 ---------------------------------------------------------------------------------------------------------------- [3]TERM2# ip netns exec ns2 bash [4]TERM2# echo $$ ---------------------------------------------------------------------------------------------------------------- 12130
4 Creating a pair of virtual Ethernet devices
- [1] - Create a pair of virtual Ethernet devices, which stand for a network cable with two RJ45 plugs
koncovkami, pricom nasledne jednu stranu (veth1) umiestnime do menneho priestoru "ns1" a druhu stranu (veth2) umiestnime do menneho priestoru "ns2".
- [2] - Virtualny ethernet adapter "veth1" umiestnime do sietoveho menneho "ns1".
- [3] - Virtualny ethernet adapter "veth1" umiestnime do sietoveho menneho "ns2".
[1]# ip link add veth1 type veth peer name veth2 [2]# ip link set veth1 netns ns1 [3]# ip link set veth2 netns ns2
5 Bringing the virtual Ethernet devices up in the namespaces and testing that they talk
- [1] - Bring the network adapter "veth1" up in namespace "ns1" and give it the address "10.0.0.1".
- [2] - Bring the network adapter "veth2" up in namespace "ns2" and give it the address "10.0.0.2".
- [3] - From network namespace "ns1", test that namespace "ns2" answers.
- [4] - From network namespace "ns2", test that namespace "ns1" answers.
[1] # ip netns exec ns1 ifconfig veth1 10.0.0.1/24 up [2]# ip netns exec ns2 ifconfig veth2 10.0.0.2/24 up [3]# ip netns exec ns1 ping 10.0.0.2 ---------------------------------------------------------------------------------------------------------------- 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.022 ms 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.036 ms ... ---------------------------------------------------------------------------------------------------------------- [4]# ip netns exec ns2 ping 10.0.0.1 ---------------------------------------------------------------------------------------------------------------- 64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.026 ms 64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.069 ms ...