IT技术互动交流平台

整合Open vSwitch与DNSmasq为虚拟机提供DHCP功能

作者:CasonChan  来源:IT165收集  发布日期:2015-06-30 20:58:30

继上文《Ubuntu14.04安装配置Open vSwitch》安装好Open vSwitch后,这里我们将要创建两个KVM虚拟机,并通过DNSmasq来为这两个虚拟机自动分配私网IP地址。

虚拟机与宿主机网络结构图大致如下所示:

测试环境说明:

1)测试私网段:172.17.0.0/24

2)测试 vlan tag:100

下面简要介绍下整个配置流程:

1、安装dnsmasq:

??  sudo apt-get install dnsmasq
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  dnsmasq
0 upgraded, 1 newly installed, 0 to remove and 37 not upgraded.
Need to get 14.9 kB of archives.
After this operation, 114 kB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/universe dnsmasq all 2.68-1ubuntu0.1 [14.9 kB]
Fetched 14.9 kB in 0s (138 kB/s)   
Selecting previously unselected package dnsmasq.
(Reading database ... 231198 files and directories currently installed.)
Preparing to unpack .../dnsmasq_2.68-1ubuntu0.1_all.deb ...
Unpacking dnsmasq (2.68-1ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Setting up dnsmasq (2.68-1ubuntu0.1) ...
 * Starting DNS forwarder and DHCP server dnsmasq                                                                                               [ OK ] 
Processing triggers for ureadahead (0.100.0-16) ...
??  sudo ps -ea | grep dns
 3307 ?        00:00:00 dnsmasq

2、通过 ovs 创建一个 internal port,这里取名为 qdhcp,并设置 vlan id 为100,供下文的 dnsmasq 进程使用:

??  sudo ovs-vsctl add-port br0 qdhcp tag=100
??  sudo ovs-vsctl set Interface qdhcp type=internal

3、查看已经创建的 qdhcp port:

??  sudo ovs-vsctl show
1e6548a9-956e-4b86-b743-f8da0aa2b922
    Bridge "br0"
        Port "br0"
            Interface "br0"
                type: internal
        Port qdhcp
            tag: 100
            Interface qdhcp
                type: internal
        Port "eth0"
            Interface "eth0"
    ovs_version: "2.0.2"
??  ifconfig 

qdhcp     Link encap:Ethernet  HWaddr 72:6c:a8:c2:48:68  
          inet6 addr: fe80::706c:a8ff:fec2:4868/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:578 (578.0 B)

4、给 qdhcp 虚拟网卡配置 IP 地址,这里IP地址必须为测试私网内的,这里我们就分配 172.17.0.1 给qdhcp:

??  sudo ifconfig qdhcp 172.17.0.1 netmask 255.255.255.0 up
??  ifconfig

qdhcp     Link encap:Ethernet  HWaddr 72:6c:a8:c2:48:68  
          inet addr:172.17.0.1  Bcast:172.17.0.255  Mask:255.255.255.0
          inet6 addr: fe80::706c:a8ff:fec2:4868/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:648 (648.0 B)

5、启动一个 dnsmasq 进程,监听 qdhcp 虚拟网卡:

??  sudo /usr/sbin/dnsmasq --strict-order --bind-interfaces --interface=qdhcp --except-interface=lo --pid-file=/var/run/dnsmasq/qdhcp.pid --leasefile-ro --dhcp-range=172.17.0.2,172.17.0.254,255.255.255.0,12h --conf-file=

6、查看已经启动的 dnsmasq 进程:

??  ps aux | grep dnsmasq
nobody    3471  0.0  0.0  35228  2412 ?        S    12:26   0:00 /usr/sbin/dnsmasq --strict-order --bind-interfaces --interface=qdhcp --except-interface=lo --pid-file=/var/run/dnsmasq/qdhcp.pid --leasefile-ro --dhcp-range=172.17.0.2,172.17.0.254,255.255.255.0,12h --conf-file=

7、下面就是整理两个虚拟机VM1、VM2的配置文件,这里仅展示 interface 这块配置项:

VM1:
    <interface type='bridge'>
      <source bridge='br0'/>
      <vlan>
        <tag id='100'/>
      </vlan>
      <virtualport type='openvswitch'/>
      <target dev='tap0'/>
      <model type='virtio'/>
    </interface>

VM2:
    <interface type='bridge'>
      <source bridge='br0'/>
      <vlan>
        <tag id='100'/>
      </vlan>
      <virtualport type='openvswitch'/>
      <target dev='tap0'/>
      <model type='virtio'/>
    </interface>

这里将两个虚拟机的虚拟网卡的 vlan id 都设为 100,即必须保证和 qdhcp port在同一个vlan中。

8、启动VM1、VM2,并查看 ovs 虚拟网卡 tap0、tap1 的情况:

??  sudo ovs-vsctl show
1e6548a9-956e-4b86-b743-f8da0aa2b922
    Bridge "br0"
        Port "tap1"
            tag: 100
            Interface "tap1"
        Port "br0"
            Interface "br0"
                type: internal
        Port qdhcp
            tag: 100
            Interface qdhcp
                type: internal
        Port "eth0"
            Interface "eth0"
        Port "tap0"
            tag: 100
            Interface "tap0"
    ovs_version: "2.0.2"

由此,我们已经将VM1、VM2和qdhcp port分在了同一个VLAN里了。

10、通过VNC登陆到VM1、VM2中,查看其是否获取到指定私网段IP地址:

VM1:

VM2:

 

11、从VM1 ping VM2:

至此,整合Open vSwitch与DNSmasq的功能就完成了,两个虚拟机之间就可以愉快地玩耍了。

延伸阅读:

Tag标签: 功能  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规