项目作者: abbbi

项目描述 :
How to manage DNS in your vagrant environment the libvirt way
高级语言:
项目地址: git://github.com/abbbi/vagrant-libvirt-dns.git
创建时间: 2018-09-13T14:53:24Z
项目社区:https://github.com/abbbi/vagrant-libvirt-dns

开源协议:

下载


About

There are various ways to manage your vagrant machines hostnames and IP
addresses. Most of them require you to install additional vagrant plugins (
landrush, hostmanager, vagrant-dns)

One of them is vagrant-landrush, which works nicely but has problems in multi
user environments (spinning up an DNS Service for each vagrant vm)

This short manual should give you an easier way to setup DNS and DHCP
management of your Vagrant environment with libvirt backend.

Goal

The goal is to have a round robin style DNS and DHCP assignment for your
vagrant virtual machines. All machines should get an hostname from the dhcp
service and shall be resolvable forward and reverse from the host system aswell
as the other vagrant machines running.

As example, lets say we have an dhcp range that goes from:

10.1.0.3 to 10.1.0.5

and want those ips to be associated with hostnames like:

  1. sep003.mycloud.local -> 10.1.0.3
  2. sep004.mycloud.local -> 10.1.0.4
  3. sep005.mycloud.local -> 10.1.0.5

These should match the ip given by the dhcp lease. The lease disappears as the
vagrant machine halts and can be re-used likewise by other machines.

It should be possible doing this without the need of third party plugins just
by modifying our libvirt network configuration.

Prerequisites

Your vagrant boxes must honor DHCP’s “hostname” option and allow to set the
systems hostname via DHCP client for better integration.

Configuring Libvirt

Define a new libvirt network from the provided example using:

  1. virsh net-define examplenet.xml

Note: you may have to assign a special bridge name in the config file to
avaoid libvirt re-using an existing one.

  1. <bridge name='virbrXX' stp='on' delay='0'/>

And start the network:

  1. virsh net-start my_cloud

This should spin up a new libvirt network with its according dnsmasq process:

  1. $ ps axw | grep my_cloud
  2. 11714 ? S 0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/my_cloud.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper

The example provides a lease range for three possible IP’s and hostnames.

Spinning up vagrant box

Now initialize a new Vagrant box (this example uses debian9):

  1. vagrant init generic/debian9

And edit your Vagantfile to contain the following section:

  1. config.vm.provider :libvirt do |domain|
  2. domain.management_network_name = "my_cloud"
  3. domain.management_network_address = "10.1.0.0/24"
  4. end

Followed by:

  1. vagrant up

As the VM starts, it receives its dhcp lease and hostname from the dnsmasq
service:

  1. ~$ virsh net-dhcp-leases my_cloud
  2. Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
  3. -------------------------------------------------------------------------------------------------------------------
  4. 2018-09-13 18:10:58 52:54:00:02:7b:cb ipv4 10.1.0.4/24 sep004

Getting into the box the hostname should have been set too:

  1. ~/mynet$ vagrant ssh
  2. [..]
  3. vagrant@sep004:~$

As all boxes use the local dnsmasq dns server, other boxes can happily
forward and reverse lookup each other:

  1. ~/mynet-2$ vagrant ssh
  2. [..]
  3. vagrant@sep003:~$ host sep004
  4. sep004.mycloud.local has address 10.1.0.4
  5. vagrant@sep003:~$ host 10.1.0.4
  6. 4.0.1.10.in-addr.arpa domain name pointer sep004.mycloud.local.

Configuring the Hostsystem

In order to make the host system aware of the hostnames and domain you have to make
your system wide dnsmasq aware of the libvirt dnsmasq server. Place the provided example
file in:

  1. /etc/dnsmasq.d/my-cloud.local

And reload your dnsmasq service. Now the hostsystem should be able to resolve your
vagrant boxes aswell:

  1. vagrantuser@hostsystem:~$ host sep003.mycloud.local
  2. sep003.mycloud.local has address 10.1.0.3

Freeing leases

Leases are automatically freed as the vagrant box is halted:

  1. :~/mynet$ vagrant halt
  2. ==> default: Halting domain...
  3. ~/mynet$ virsh net-dhcp-leases my_cloud
  4. Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
  5. -------------------------------------------------------------------------------------------------------------------
  6. 2018-09-13 18:14:17 52:54:00:16:ee:ab ipv4 10.1.0.3/24 sep003 -