0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux Network Namespace(その3:Routing)

Posted at

Network Namespaceで基本ルーティング

Network Namespaceシリーズその3では、基本的なルーティングを実現してみる。

ネットワーク

対象ネットワークは下記。

image.png

ほぼ自明。3つのネットワークが存在し、両端のPCどうしが疎通を行えるようにするもの。

Network Namespace

上記ネットワークを、Network Namespaceで表現すると下記となる。

image.png

sudo ip link add name veth11 type veth peer name veth22
sudo ip link set veth11 up
sudo ip link set veth22 up
sudo ip addr add 10.1.1.1/24 dev veth11
sudo ip addr add 10.1.1.2/24 dev veth22
  • 仮想ネットワークインターフェースPairであるveth11およびveth22作成
  • それらをUpし、IPアドレスアサイン
sudo ip netns add ns1
sudo ip link add name veth1 type veth peer name ns1-veth
sudo ip link set ns1-veth netns ns1
sudo ip link set veth1 up
sudo ip addr add 172.16.1.1/24 dev veth1
sudo ip netns exec ns1 ip link set lo up
sudo ip netns exec ns1 ip link set ns1-veth up
sudo ip netns exec ns1 ip addr add 172.16.1.11/24 dev ns1-veth
  • Namespace ns1作成
  • 仮想ネットワークインターフェースPairであるveth1およびns1-veth作成
  • ns1-vethをns1に配置
  • veth1をUpし、IPアドレスアサイン
  • ns1のループバックおよびns1-vethをUp
  • ns1のns1-vethにIPアドレスアサイン
sudo ip netns add ns2
sudo ip link add name veth2 type veth peer name ns2-veth
sudo ip link set ns2-veth netns ns2
sudo ip link set veth2 up
sudo ip addr add 172.16.2.1/24 dev veth2
sudo ip netns exec ns2 ip link set lo up
sudo ip netns exec ns2 ip link set ns2-veth up
sudo ip netns exec ns2 ip addr add 172.16.2.22/24 dev ns2-veth

ns2向けに、ns1と同様の内容を実施。

sudo sysctl -w net.ipv4.ip_forward=1

sudo ip netns exec ns1 ip route add 10.1.1.0/24 via 172.16.1.1
sudo ip netns exec ns1 ip route add 172.16.2.0/24 via 172.16.1.1
sudo ip netns exec ns2 ip route add 10.1.1.0/24 via 172.16.2.1
sudo ip netns exec ns2 ip route add 172.16.1.0/24 via 172.16.2.1
  • ルーティングON(net.ipv4.ip_forward=1)
  • ns1およびns2にてスタティックルート設定

検証

ルーティングテーブルは下記となる。まずは、オリジナル(1st)Spaceの状況。

$ ip route

10.1.1.0/24 dev veth11 proto kernel scope link src 10.1.1.1 
10.1.1.0/24 dev veth22 proto kernel scope link src 10.1.1.2 
172.16.1.0/24 dev veth1 proto kernel scope link src 172.16.1.1 
172.16.2.0/24 dev veth2 proto kernel scope link src 172.16.2.1 

ns1のルーティングテーブルは下記。(ns2もほぼ同様。)

$ sudo ip netns exec ns1 ip route
10.1.1.0/24 via 172.16.1.1 dev ns1-veth 
172.16.1.0/24 dev ns1-veth proto kernel scope link src 172.16.1.11 
172.16.2.0/24 via 172.16.1.1 dev ns1-veth 

左のPCから右のPCへping実行。

$ sudo ip netns exec ns1 ping 172.16.2.22 -c 1
PING 172.16.2.22 (172.16.2.22) 56(84) bytes of data.
64 bytes from 172.16.2.22: icmp_seq=1 ttl=63 time=0.042 ms

--- 172.16.2.22 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.042/0.042/0.042/0.000 ms

成功。

EOF

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?