Intro
One of the ironies of working a lot with Docker, Kubernetes, and OpenShift is that I’ve had to learn a lot about Vagrant and Virtualbox. Mostly I use it to spin up OpenShift clusters on my local machine, but I’ve had to poke into lots of corners to get various other things working too.
Here’s a list of things I wish I’d known about before I started.
1) Control CPU and Memory
If you do a vagrant init
then this is buried in the comments of the resulting Vagrantfile.
[...] config.vm.provider "virtualbox" do |vb| vb.memory = "1024" end [...]
You can also set the CPUs used by the VM:
[...] config.vm.provider "virtualbox" do |vb| vb.memory = "1024 vb.cpus = "2" end [...]
2) Pattern for multiple machines
It’s not immediately obvious how you create multiple machines on a host in Vagrant.
You can use this as an example:
Vagrant.configure("2") do |config| config.vm.define "chefserver" do |chefserver| chefserver.vm.box = "centos/7" chefserver.vm.hostname = "chefserver.vagrant.test" chefserver.vm.provider :virtualbox do |v| v.customize ["modifyvm", :id, "--memory", "1024"] end end config.vm.define "chefworkstation1" do |chefworkstation1| chefworkstation1.vm.box = "ubuntu/xenial" chefworkstation1.vm.hostname = "chefworkstation1.vagrant.test" chefworkstation1.vm.provider :virtualbox do |v| v.customize ["modifyvm", :id, "--memory", "512"] end end end
Each VM is defined with a name (eg chefworksation1 above) whose characteristics can then be accessed or modified with the dot notation (chefworkstation.vm.hostname). Characteristics of the ‘provider’ (which, by the way, just means: the thing under the hood that runs the VMs – typically VirtualBox or libvirt, but could even be not-VMs like Docker).
3) Increase Swap Space
This one was a leap forward for my Vagrant usage. By adding swap space to my VMs, I could increase the memory available to them without overcommitting memory on the host.
A short-cut script is available here.
4) Landrush
This is the single most important plugin for simulating clusters. It was a game-changer for me.
I can’t say it’s been without its challenges, especially as my networking skills (like many developers’) isn’t my ‘A’ game.
One big challenge I faced (for example) was how to make Docker play nice with it (contact me if you want details).
But the work this plugin does for me is invaluable.
5) vagrant global-status
When I run a lot of vagrant machines I often find myself reaching for this command.
$ vagrant global-status id name provider state directory -------------------------------------------------------------------------------------------------------------------- df8bccb master1 virtualbox running /space/git/shutit-openshift-cluster/vagrant_run/shutit_openshift_cluster_v6lepQ 4074674 etcd2 virtualbox running /space/git/shutit-openshift-cluster/vagrant_run/shutit_openshift_cluster_v6lepQ
I also use it as the basis of a bunch of cleanup scripts (which occasionally cause me to swear as I remove important VMs by accident).
6) Get a GUI
OK, maybe I’m a dunce but it took me a good while before I realised I could get a GUI out of a vagrant up command
[...] config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = true # Customize the amount of memory on the VM: vb.memory = "1024" end [...]
7) Persistent Storage
Adding virtual disks to Vagrant machines is a complex affair for the uninitiated.
Fortunately there’s a plugin for that.
8) Vagrant snapshot
Re-building a lot of vagrant machines with the same commands?
Cache your builds with vagrant snapshot
.
9) Syncing folders with host
If you find you run out of space on your Vagrant machines, you may want to just mount folders from the host:
[...] config.vm.synced_folder "/space", "/space", owner: "imiell", group: "imiell" [...]
Be aware that file permission mapping can be important in this context, so if things don’t work, read up on uids etc..
10) Clipboard control
You can control the clipboard using the modifyvm snippet.
[...] config.vm.provider "virtualbox" do |vb| vb.gui = true vb.memory = "4096" vb.customize ['modifyvm', :id, '--clipboard', 'bidirectional'] end [...]
This is based on work in progress from the second edition of Docker in Practice
Get 39% off with the code: 39miell2
8 thoughts on “Ten Things I Wish I’d Known Before Using Vagrant”