Vagrant is an automation tool to manage virtual environments. It helps spin up several versions, flavors and their mix of operating system environment in no time. KVM has been my choice of virtualization platform for a while now and its integration with vagrant helps me get clean state for developing or testing solutions or snapshot specific states of my testbed configurations and quickly spin them up later for an evaluation. I extensively use the sandboxing provided by plugin sahara to test new configurations and rollback to a cleaner state if things break. There are a hundred other additional plugins if you wish to try.
Setting up vagrant and spinning up your first VM is typically straight forward. I have listed the installation process for CentOS 7 in two sections. Adapting the process to a The first section explains the steps to prepare the host system and the later explains working with the guest VM.
Host Configuration
Host is the physical machine on which we will deploy our VMs
Preparation
Turn off firewall and SELinux.
systemctl stop firewalld setenforce 0
I generally also tend to stop and disable NetworkManager service to prevent it from overwriting the network settings.
systemctl stop NetworkManager systemctl disable NetworkManager
Package Installation
Install KVM and libvirt
yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libvirt-devel ruby-devel gcc libvirt-devel libguestfs libguestfs-tools-c libguestfs-tools
Enable Virtualization using KVM
Validate the host virtualization setup using
virt-host-validate
If /dev/kvm not loaded enable Virtualization at BIOS
When you pass all evalautions with virt-host-validate, start libvirtd service
systemctl start libvirtd systemctl enable libvirtd
Networking
By default KVM allows network traffic from the Guest VM to the outside world through a NAT. If the Guest VM needs to be visible to the outside world a public network should be configured through a bridge on the host.
Creating a network bridge
Create a configuration file for the bridge. The following configuration file creates a bridge by name br0 and assigns a static IP address.
[root@test test]# cat /etc/sysconfig/network-scripts/ifcfg-br0 TYPE=Bridge BOOTPROTO=none # Change this to dhcp if this LAN segment runs a DHCP server DEFROUTE=yes NAME=br0 DEVICE=br0 ONBOOT=yes IPADDR=192.16.71.245 # Replace with your networks IP if you need a static IP PREFIX=24 GATEWAY=192.16.71.1 # Replace with your networks' gateway IPV6_PRIVACY=no NM_CONTROLLED=no
Add the NIC that connects to the LAN as a slave to the bridge.
[root@test test]# cat /etc/sysconfig/network-scripts/ifcfg-eno1 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eno1 # Change this to your ethernet device UUID=ef04c3c5-ca44-4097-99eb-b9663f4043e3 DEVICE=eno1 # Change this to your ethernet device ONBOOT=yes NM_CONTROLLED=no BRIDGE=br0
CAUTION: You may get locked out of the server if the configuration breaks.
Install Vagrant
Make sure to check for the latest version of vagrant from https://www.vagrantup.com/downloads.html
yum install https://releases.hashicorp.com/vagrant/2.0.4/vagrant_2.0.4_x86_64.rpm
Install Vagrant Plugins
The following is the minimum set of plugins I use for my KVM setups with vagrant.
vagrant-libvirt – adds an Libvirt provider to Vagrant
sahara – allows vagrant to operate in sandbox mode
fog – Ruby cloud services library
vagrant plugin install vagrant-libvirt sahara fog
Storage configuration
KVM image location
KVM stores the images in /var/lib/libvirt/images. There are a few times I have run out of disk space in /var and had to relocate the images to a different location. To plan this early in your setup, Check if you already have a default storage pool
virsh pool-list
If you don’t have one create it with your preferred location
virsh pool-create-as --name default --type dir --target /bigdisk/kvmImages/
If the default location exists, you could delete the default location and recreate.
CAUTION: You might loose your VMs if you have already VMs running out of this pool.
Vagrant box location
Vagrant stores the box data in $HOME/.vagrant.d. I typically change this to one with large disk space. You can change the location of boxes by setting environment variable VAGRANT_HOME
Environment Variables
I have these two environment variables in my ~/.bashrc file to set defaults
export VAGRANT_DEFAULT_PROVIDER=libvirt # Set provider type export VAGRANT_HOME=/apps/vms
I generally create a project folder and host the vagrantfile in the folder and I tend to store snapshots as exported boxes within the project folder
Vagrantfile
The following is an example vagrantfile. Vagrant would now spin up a Centos 7 VM and configure two interfaces one connected to the LAN through the bridge and the other a private network that connects the host and guest. There a few more options of OS versions / flavours for KVM provider and a comprehensive list of available boxes is here.
[root@test test]# cat vagrantfile
Vagrant.configure("2") do |config|
config.vm.define :master do |master|
master.vm.box = "centos/7"
#Change the box name you wish to spin up
master.vm.network :public_network,
:auto_config => false,
:mac => "32:54:10:86:a2:00",
:dev => "br0", # Make sure to change the bridge id
:mode => "bridge",
:type => "bridge"
master.vm.provider :libvirt do |domain|
domain.management_network_address = "192.168.122.0/24"
domain.management_network_name = "default"
domain.nic_model_type = "rtl8139"
domain.memory = 2048
domain.cpus = 2
domain.nested = true
domain.volume_cache = 'none'
domain.graphics_port = 5902
domain.graphics_ip = '0.0.0.0'
domain.video_type = 'qxl'
end
end
end
To start the VM, use the following command when you are in the same directory as the one where you created the vagrantfile
vagrant up
You can use the following command to login to the VM
vagrant ssh
1 thought on “Vagrant & KVM – Managing virtual environments”