Hi Readers, In previous blog we learned about the WHAT and WHY of Virtual Machines and Vagrant. We understood the Virtual Machine life cycle, problems while manual setup of Virtual Machine. Apart from this we also saw benefits of Vagrant and vagrant architecture.
Now in this blog we will try to setup virtual machines automatically using Vagrant.
Prerequisite
- Vitualization Technology Enabled in BIOS
- Vagrant tool
- Hypervisor like Oracle Virtualbox
- CLI (GIT bash/CMD prompt/Terminal)
Script to Install Virtualbox
First of all we will need a Virtualization tool which allows users and administrators to easily run multiple guest operating systems on a single host. We are going to use virtualbox. You can install it from here or can follow below script.
#!/bin/bash
sudo apt-get -y update
wget https://download.virtualbox.org/virtualbox/6.1.8/Oracle_VM_VirtualBox_Extension_Pack-6.1.8.vbox-extpack
sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-6.1.8.vbox-extpack
echo "============= Virtual box installed ==============="
sudo apt-get -y update
Directory Structure
We will create following directory structure for setting up various virtual machine. In this blog we are going to spin up virtual machine using Ubuntu box from vagrant cloud.
Script to Setup workspace
Let us setup workspace for our work. In below script we are going to,
- Create two directories, one for ubuntu and another for centos inside workspace folder.
- Move to ubuntu directory and initialize ubuntu virtual machine using ubuntu box.
- Start the ubuntu virtual machine.
# Create a workspace, and inside that create two sub directories(it could be n as well)
for i in centOS ubuntu; do mkdir -p workspace/$i; done
echo "============= workspace created =============="
cd workspace/ubuntu
#initialize your VM by mentioning box name
echo "============= Initializing vm using boxName ==============="
# replace boxname with box from vagrant-cloud
vagrant init ubuntu/trusty64
#run the initialized box.
echo "============= start and provision the vagrant environment ==============="
vagrant up
echo "=========== Current Vagrant version =============="
vagrant status --version
You can get various boxes from vagrant cloud.
Script to setup application
Now we need to create index.html which will contain a small HTML script and bootstrap.sh which will install apache2 in our ubuntu virtual machine and run index.html.
mkdir html
cat > ./html/index.html << EOF
<!DOCTYPE html>
<html>
<body>
<h1>Hello Knolders!</h1>
</body>
</html>
EOF
echo "============= index.html created =============="
#Create bootstrap.sh
cat > ./bootstrap.sh << EOF
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi
EOF
echo "============= bootstrap.sh created =============="
Provisioning Virtual Machine in Vagrantfile
After that, we need to provision our ubuntu VM by providing reference to bootstrap.sh inside our Vagrantfile. And then we need to reload our VM.
#Provision the VM by adding script to Vagrantfile
sed -i '62 i config.vm.provision :shell, path: "bootstrap.sh"' Vagrantfile > sed_out
echo "============= VM Provisioned with bootstrap.sh =============="
# Reload Provisioning
vagrant reload --provision
echo "============= reloaded VM =============="
SSH to Virtual machine
Now we can enter to our VM and hit the localhost URL to see index.html loaded.
# ssh to your vm
echo "============= connect to your VM =================="
vagrant ssh -c 'wget -qO- 127.0.0.1'
Destroying Virtual machine
After that, when we are done with our work we can destroy our virtual machine.
#destroy VM
vagrant destroy
That’s all for this blog. Hope you found some information from this. You can find the source code and other informations related to VM and Vagrant from this github repository. In case of any queries you can contact me over my email id nitin.mishra@knoldus.com