整合Open vSwitch與DNSmasq為虛擬機提供DHCP功能


繼上文《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的功能就完成了,兩個虛擬機之間就可以愉快地玩耍了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM