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?

More than 1 year has passed since last update.

仮想ネットワークを構築するためのスクリプト

Last updated at Posted at 2022-11-03

0. はじめに

Open vSwitch(OVS)ip-netnsは、ネットワークの構築方法を学ぶための便利な道具としても利用できます。これらを用いて仮想ネットワークを構築する際には、以下の2点がハマりやすい点になると思います。

本記事では、読者がネットワークの構築方法を学ぶ際の参考情報として、下記のネットワークを構築するためのスクリプトを共有します。

 -----------------------      -----------------------     -----------------------      -----------------------
 |         h00         |      |        h01          |     |        h10          |      |        h11          | 
 |                     |      |                     |     |                     |      |                     |   
 |                     |      |                     |     |                     |      |                     | 
 |  192.168.10.102/24  |      |  192.168.10.103/24  |     |  192.168.20.102/24  |      |  192.168.20.103/24  |    
 -----------------------      -----------------------     -----------------------      -----------------------
           | vport_h00                   | vport_h01                  | vport_h10                  | vport_h11 
           |                             |                            |                            | 
           | vport_br00                  | vport_br01                 | vport_br10                 | vport_br11
     ----------------------------------------------         ----------------------------------------------
     |                  br0                       |         |                     br1                    |
     ----------------------------------------------         ----------------------------------------------
                         | vport_br02                                              | vport_br12
                         |                                                         |
                         | vport_n00                                               | vport_n10
               ---------------------                                     ----------------------
               | 192.168.10.101/24 |                                     | 192.168.20.101/24  |
               |                   |                                     |                    |
               |        n0         |                                     |         n1         |
               |                   |                                     |                    |
               |  192.168.30.1/24  |                                     |   192.168.30.2/24  |
               ---------------------                                     ----------------------
                         | vport_n01                                               | vport_n11
                         |                                                         |
                         | vport_br20                                              | vport_br21
               ---------------------------------------------------------------------------------
               |                                      br2                                      |
               ---------------------------------------------------------------------------------

前提条件:OSやソフトウェアのバージョン

Ubuntu 20.04
Open vSwitch 2.13.5

1. スクリプト一覧

1.1 ネットワーク構築用スクリプト

下記のスクリプトを順番に実行することで、ネットワークを構築できます。
尚、ルーティング設定用のスクリプトとip forwardを有効化するスクリプトは、4_iproute.sh5_ipfoward.shに対応します。

0_net.sh
ip netns add h00
ip netns add h01
ip netns add h10
ip netns add h11
ip netns add n0
ip netns add n1
ip l add vport_h00 type veth peer name vport_br00
ip l add vport_h01 type veth peer name vport_br01
ip l add vport_h10 type veth peer name vport_br10
ip l add vport_h11 type veth peer name vport_br11
ip l add vport_n00 type veth peer name vport_br02
ip l add vport_n01 type veth peer name vport_br20
ip l add vport_n10 type veth peer name vport_br12
ip l add vport_n11 type veth peer name vport_br21
ip l set up vport_h00
ip l set up vport_h01
ip l set up vport_h10
ip l set up vport_h11
ip l set up vport_n00
ip l set up vport_n01
ip l set up vport_n10
ip l set up vport_n11
ip l set up vport_br00
ip l set up vport_br01
ip l set up vport_br02
ip l set up vport_br10
ip l set up vport_br11
ip l set up vport_br12
ip l set up vport_br20
ip l set up vport_br21
ip l set vport_h00 netns h00
ip l set vport_h01 netns h01
ip l set vport_h10 netns h10
ip l set vport_h11 netns h11
ip l set vport_n00 netns n0
ip l set vport_n01 netns n0
ip l set vport_n10 netns n1
ip l set vport_n11 netns n1
1_ovs-br.sh
ovs-vsctl add-br br0
ovs-vsctl add-br br1
ovs-vsctl add-br br2
ovs-vsctl add-port br0 vport_br00
ovs-vsctl add-port br0 vport_br01
ovs-vsctl add-port br0 vport_br02
ovs-vsctl add-port br1 vport_br10
ovs-vsctl add-port br1 vport_br11
ovs-vsctl add-port br1 vport_br12
ovs-vsctl add-port br2 vport_br20
ovs-vsctl add-port br2 vport_br21
2_ipaddr.sh
ip netns exec h00 ip a add 192.168.10.102/24 dev vport_h00
ip netns exec h01 ip a add 192.168.10.103/24 dev vport_h01
ip netns exec h10 ip a add 192.168.20.102/24 dev vport_h10
ip netns exec h11 ip a add 192.168.20.103/24 dev vport_h11
ip netns exec n0  ip a add 192.168.10.101/24 dev vport_n00
ip netns exec n0  ip a add 192.168.30.1/24   dev vport_n01
ip netns exec n1  ip a add 192.168.20.101/24 dev vport_n10
ip netns exec n1  ip a add 192.168.30.2/24   dev vport_n11
3_linkup.sh
ip netns exec h00 ip l set up vport_h00
ip netns exec h01 ip l set up vport_h01
ip netns exec h10 ip l set up vport_h10
ip netns exec h11 ip l set up vport_h11
ip netns exec n0  ip l set up vport_n00
ip netns exec n0  ip l set up vport_n01
ip netns exec n1  ip l set up vport_n10
ip netns exec n1  ip l set up vport_n11
4_iproute.sh
ip netns exec h00 ip r add default via 192.168.10.101 dev vport_h00
ip netns exec h01 ip r add default via 192.168.10.101 dev vport_h01
ip netns exec n0  ip r add 192.168.20.0/24 via 192.168.30.2 dev vport_n01
ip netns exec h10 ip r add default via 192.168.20.101 dev vport_h10
ip netns exec h11 ip r add default via 192.168.20.101 dev vport_h11
ip netns exec n1  ip r add 192.168.10.0/24 via 192.168.30.1 dev vport_n11
5_ipfoward.sh
ip netns exec n0 sysctl -w net.ipv4.ip_forward=1
ip netns exec n1 sysctl -w net.ipv4.ip_forward=1
ip netns exec n0 sysctl -p
ip netns exec n1 sysctl -p

1.2 ネットワーク検証用スクリプト

pingを用いた疎通確認用のスクリプトです。

6_ping_test0.sh
ip netns exec h00 ping 192.168.10.103 -c 2
ip netns exec h00 ping 192.168.10.101 -c 2
ip netns exec h00 ping 192.168.30.1 -c 2
ip netns exec n0  ping 192.168.10.103 -c 2
ip netns exec h10 ping 192.168.20.103 -c 2
ip netns exec h10 ping 192.168.20.101 -c 2
ip netns exec h10 ping 192.168.30.2 -c 2
ip netns exec n1  ping 192.168.20.103 -c 2
7_ping_test1.sh
ip netns exec n0  ping 192.168.30.2 -c 2
ip netns exec n0  ping 192.168.20.101 -c 2
ip netns exec h00 ping 192.168.30.2 -c 2
ip netns exec h00 ping 192.168.20.101 -c 2
ip netns exec h00 ping 192.168.20.102 -c 2
ip netns exec h00 ping 192.168.20.103 -c 2

1.3 ネットワーク削除用スクリプト

構築したネットワークを削除するためのスクリプトです。

8_del_net.sh
ip netns del h00
ip netns del h01
ip netns del h10
ip netns del h11
ip netns del n0
ip netns del n1
ovs-vsctl del-br br0
ovs-vsctl del-br br1
ovs-vsctl del-br br2

2. 参考文献

3. おまけ

MACアドレステーブルを表示するコマンド

ovs-appctl fdb/show br0

OpenFlowに関する情報を表示するコマンド

ovs-ofctl show br0
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?