本节主要讲解如何让包通过openvswitch,连宿主机和虚拟机。
模块 | 版本 |
---|---|
内核版本 | 4.0.0-2-amd64 |
debian | Debian 4.0.8-2 (2015-07-22) x86_64 |
openvswitch-common | 2.4.0-1 |
openvswitch-datapath-dkms | 2.4.0-1 |
openvswitch-datapath-source | 2.4.0-1 |
openvswitch-dbg | 2.4.0-1 |
openvswitch-pki | 2.4.0-1 |
openvswitch-switch | 2.4.0-1 |
openvswitch-test | 2.4.0-1 |
openvswitch-vtep | 2.4.0-1 |
python-openvswitch | 2.4.0-1 |
注: 这些包目前没有源提供,本人是在本地自己打包编译的,
其实编译也很简单:
qemu的版本不是本文的重点。再此就不再列出具体版本了
准备启动虚拟机的镜像,也不在此叙述,我这边有个副本,直接root登陆,密码是:1,链接地址:http://pan.baidu.com/s/1sjmOZyl 文件名字forqemu.qcow
qemu的命令不再本文叙述之列,请参照帮助文档:
本人用的启动虚拟机的命令:(仅供参考,参照本文最后一节的备注)
qemu-system-x86_64 forqemu.qcow2 -m 1024 -net nic,model=virtio,macaddr=00:16:3e:22:22:22 -net tap,ifname=tap124
物理机(注,物理机该处的命令在虚拟机启动之后才会有tap124设备)
root@www /home/abaobo 2015-08-14 20:54:23
# ip a a 10.120.0.2/24 dev tap124
虚拟机中命令执行
qemu-system-x86_64 forqemu.qcow2 -m 1024 -net nic,model=virtio,macaddr=00:16:3e:22:22:22 -net tap,ifname=tap124 -display curses
-------------------------------------
root@cnsdev-for-test:~# ip a a 10.120.0.1/24 dev eth0
root@cnsdev-for-test:~# ip r
10.120.0.0/24 dev eth0 proto kernel scope link src 10.120.0.1
root@cnsdev-for-test:~# ping 10.241.21.84 -I eth0 -c 2
PING 10.241.21.84 (10.241.21.84) from 10.120.0.1 eth0: 56(84) bytes of data.
64 bytes from 10.241.21.84: icmp_req=1 ttl=64 time=10.0 ms
64 bytes from 10.241.21.84: icmp_req=2 ttl=64 time=13.0 ms
--- 10.241.21.84 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 10.076/11.558/13.040/1.482 ms
root@cnsdev-for-test:~#
上面可以看出,虚拟机与物理机之间已经互通,下面我们来展示一下如何通过openvswich创建的bridge连通物理机与虚拟机。
物理机上运行命令:
root@www /home/abaobo 2015-08-14 21:53:11
# ovs-vsctl add-br br-int
root@www /home/abaobo 2015-08-14 21:53:40
# ovs-vsctl add-port br-int tap124
root@www /home/abaobo 2015-08-14 21:54:02
# ovs-vsctl show
69581e33-310b-4971-84f3-2c06c4f8a75c
Bridge br-int
Port "tap124"
Interface "tap124"
Port br-int
Interface br-int
type: internal
ovs_version: "2.4.0"
root@www /home/abaobo 2015-08-14 21:55:04
# ifconfig br-int up
root@www /home/abaobo 2015-08-14 21:55:12
# ip r a 10.120.0.0/24 dev br-int
然后在虚拟机中ping物理机:
root@cnsdev-for-test:~# ping 10.241.21.84 -I eth0 -c 2
PING 10.241.21.84 (10.241.21.84) from 10.120.0.1 eth0: 56(84) bytes of data.
64 bytes from 10.241.21.84: icmp_req=1 ttl=64 time=12.7 ms
64 bytes from 10.241.21.84: icmp_req=2 ttl=64 time=2.09 ms
--- 10.241.21.84 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 2.099/7.401/12.703/5.302 ms
root@cnsdev-for-test:~#
使用虚拟机来做这些讲解释很复杂的,接下来的章节我们将会使用internal的port模拟虚拟机网络,具体的办法是。
命令如下:
root@www /home/abaobo/tmp 2015-08-15 11:22:29
# ovs-vsctl add-port br-int br_fake_vm_A_1 -- set interface br_fake_vm_A_1 type=internal
root@www /home/abaobo/tmp 2015-08-15 11:17:43
# ip netns add br_fake_vm_A_1
root@www /home/abaobo/tmp 2015-08-15 11:23:06
# ip link set br_fake_vm_A_1 netns br_fake_vm_A_1
root@www /home/abaobo/tmp 2015-08-15 11:23:35
# ip netns exec br_fake_vm_A_1 bash
root@www:/home/abaobo/tmp# ifconfig
root@www:/home/abaobo/tmp# ifconfig br_fake_vm_A_1 up
root@www:/home/abaobo/tmp# ifconfig
br_fake_vm_A_1 Link encap:Ethernet HWaddr 4a:dc:1f:9f:f8:ae
inet6 addr: fe80::48dc:1fff:fe9f:f8ae/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5 errors:0 dropped:8 overruns:0 frame:0
TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:418 (418.0 B) TX bytes:418 (418.0 B)
root@www:/home/abaobo/tmp# ip a a 10.120.0.8/24 dev br_fake_vm_A_1
root@www:/home/abaobo/tmp# exit
下一节将讲解openvswitch中的TAG的知识。