LoginSignup
0
0

More than 3 years have passed since last update.

サブネットの違う2つのサーバーをVXLANでトンネリングしてみた

Last updated at Posted at 2020-11-19

目標

VirtualBoxで立ち上げたサブネットの違う2つのサーバーをVXLANでL2トンネリングする

環境

VirtualBox : 6.0.20
Vagrant : 2.2.9
CentOS : 7.8.2003 (Core)

1. まずは、サーバーを立ち上げる

VagrantとかVirtualBoxとかは使ったことあるからわかるって人は、2から読んでください。

まずは、Vagrantで使うCentOSのBoxをインストール

$ vagrant box add centos/7 
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3

ちゃんと追加されたか確認します

$ vagrant box list
centos/7           (virtualbox, 2004.01)

次に、

$ vagrant init

これでVagrantfileが出来ます。
そして、出来たVagrantfileを以下のように全部書き換えてください。

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"

  # DC1のGW
  config.vm.define :vm1 do |vm1|
    vm1.vm.network :private_network, ip:"192.168.60.10"
  end

  # ルーター?
  config.vm.define :vm2 do |vm2|
    vm2.vm.network :private_network, ip:"192.168.60.11"
    vm2.vm.network :private_network, ip:"192.168.61.11"
  end

  # DC2のGW
  config.vm.define :vm3 do |vm3|
    vm3.vm.network :private_network, ip:"192.168.61.10"
  end

end

はい、設定が完了しました。

次に、Virtualboxを立ち上げます。

$ vagrant up

はい、なかなか時間かかったと思います。

じゃあ、無事に立ち上がったか確認します。runningになってたらオッケー

$ vagrant status
Current machine states:

vm1                       running (virtualbox)
vm2                       running (virtualbox)
vm3                       running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

2. 次に、サブネットの違うVM1とVM3でPingが通るようにします

ちなみに、以下の設定をしなくてもPingは通ります。ただ、tracerouteを使うとわかるんですけど、

vm3
$ sudo yum install -y traceroute
$ traceroute 192.168.60.10
traceroute to 192.168.60.10 (192.168.60.10), 30 hops max, 60 byte packets
 1  gateway (10.0.2.2)  0.214 ms  0.149 ms  0.160 ms
 2  192.168.60.10 (192.168.60.10)  0.819 ms  0.733 ms  0.662 ms

勝手に設定されたgatewayを通って、vm1とつながっています。
ちゃんと vm1--vm2--vm3って感じでパケットが届いて欲しいので、以下の設定をします。


$ vagrant sshでそれぞれのサーバーにログインします。
vm1にログインしたければ、下記のようにしてください。
vm2,vm3にログインしたい場合も同様に。

$ vagrant ssh vm1

はい、vm1にログインできました。

vm1,vm2,vm3での作業は以下の通りです。

vm1
## ルーティングの追加
$ sudo ip r add 192.168.61.0/24 via 192.168.60.11
vm2
## パケット転送設定
$ sudo echo 1 >/proc/sys/net/ipv4/ip_forward
vm3
## ルーティングの追加
$ sudo ip r add 192.168.60.0/24 via 192.168.61.11

これだけ!簡単ですね。

vm3
$ traceroute 192.168.60.10
traceroute to 192.168.60.10 (192.168.60.10), 30 hops max, 60 byte packets
 1  192.168.61.11 (192.168.61.11)  0.625 ms  0.485 ms  1.378 ms
 2  192.168.60.10 (192.168.60.10)  3.306 ms  3.305 ms  3.293 ms

ただし、これらは一時的な設定でVirtualBoxを再起動すると、せっかくやったものが全て消えます!!
再起動してもそのままが良いなって場合は、nmcliとかで永続的に設定しましょう。(書くのめんどくさかったです...w)

3. 次に、VM1とVM3をVXLANで繋げます。

vm1
$ ip l add vxlan0 type vxlan id 77 remote 192.168.61.10 dstport 4789 dev eth1
$ ip l set up vxlan0
$ ip a add 172.24.77.101/24 dev vxlan0
vm3
$ ip l add vxlan0 type vxlan id 77 remote 192.168.61.10 dstport 4789 dev eth1
$ ip l set up vxlan0
$ ip a add 172.24.77.101/24 dev vxlan0
vm1
$ traceroute 172.24.77.102
traceroute to 172.24.77.102 (172.24.77.102), 30 hops max, 60 byte packets
 1  172.24.77.102 (172.24.77.102)  0.682 ms  0.803 ms  0.791 ms

はい、できました。これも再起動したら消える一時的な設定です。
永続的な設定は、まあ...とりあえずこれでお願いします!

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