Skip to content

Vagrant 🛠️

Vagrant is an open-source tool that helps us to automate the creation and management of Virtual Machines. In a nutshell, we can specify the configuration of a virtual machine in a simple configuration file, and Vagrant creates the same Virtual machine using just one simple command. It provides command-line interfaces to automate such tasks.

Requirements

Virtualbox 📦

Vagrant 📦

Installing Virtualbox in the Host Machine

Download link

https://download.virtualbox.org/virtualbox/6.1.30/VirtualBox-6.1.30-148432-Win.exe

Through Command line

sudo apt update
sudo apt install virtualbox

Note

To Run Virtual Machine You should need to Disable the Secure-Boot in BIOS Options of your Machine 🚫

To Verify that your Virtual Box is Working fine or not in Linux, Please hit the below command.

sudo systemctl status virtualbox.service

Installing Vagrant in the Host Machine

Download link

https://releases.hashicorp.com/vagrant/2.3.4/vagrant_2.3.4_windows_i686.msi

Through Command line

sudo apt update
sudo apt install vagrant -y

Note

You Should Need to Restart Your Machine After Installation of Vagrant

Creating a Common Directory for all VMs

Create a Directory with name vagrantvms 📂

mkdir vagrantvms
Change Directory to vagrantvms
cd vagrantvms

To Bring up VM 🚀

Create a Directory name ubuntu

mkdir ubuntu 
Change Directory to ubuntu
cd ubuntu
Initialize Ubuntu VM
vagrant init ubuntu/bionic64 
Now Bring up your Ubuntu VM
vagrant up
Once your VM is up then login to your vm
vagrant ssh
Now you can see the prompt of the ubuntu machine, If you wanna exit type exit

Create a Directory name centos

mkdir centos 
Change Directory to centos
cd centos
Initialize centos VM
vagrant init geerlingguy/centos7 
Now Bring up your centos VM
vagrant up
Once your VM is up then login to your VM
vagrant ssh
Now you can see the prompt of the centos machine, If you wanna exit type 👉 exit

To Init Specific Vagrant box

https://app.vagrantup.com/boxes/search

Multi VM Vagrantfile 📦

mkdir -p vagrantvms && cd vagrantvms
vim Vagrantfile

Vagrant.configure("2") do |config|
  config.hostmanager.enabled = true 
  config.hostmanager.manage_host = true

### Nginx VM ###
  config.vm.define "web01" do |web01|
    web01.vm.box = "ubuntu/bionic64"
    web01.vm.hostname = "web01"
  web01.vm.network "private_network", ip: "192.168.33.11"
  web01.vm.synced_folder "../vprofile-code", "/vprofile-vm-data"
  end

### tomcat vm ###
  config.vm.define "app01" do |app01|
    app01.vm.box = "geerlingguy/centos7"
    app01.vm.hostname = "app01"
    app01.vm.network "private_network", ip: "192.168.33.12"
    app01.vm.synced_folder "../vprofile-code", "/vprofile-vm-data"
  app01.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
  app01.vm.provision "shell", inline: <<-SHELL
#     yum update -y
  yum install epel-release -y
  SHELL
  end

### RabbitMQ vm  ####
  config.vm.define "rmq01" do |rmq01|
    rmq01.vm.box = "geerlingguy/centos7"
  rmq01.vm.hostname = "rmq01"
    rmq01.vm.network "private_network", ip: "192.168.33.16"
  rmq01.vm.synced_folder "../vprofile-code", "/vprofile-vm-data"
  rmq01.vm.provision "shell", inline: <<-SHELL
  # yum update -y
  #yum install epel-release -y
  # yum install wget -y
    SHELL
  end ; f
  ., v/
### Memcache vm  #### 
  config.vm.define "mc01" do |mc01|
    mc01.vm.box = "geerlingguy/centos7"
  mc01.vm.hostname = "mc01"
    mc01.vm.network "private_network", ip: "192.168.33.14"
  mc01.vm.synced_folder "../vprofile-code", "/vprofile-vm-data"
  mc01.vm.provision "shell", inline: <<-SHELL
    yum install epel-release -y
    SHELL
  end

### DB vm  ####
  config.vm.define "db01" do |db01|
    db01.vm.box = "geerlingguy/centos7"
  db01.vm.hostname = "db01"
    db01.vm.network "private_network", ip: "192.168.33.15"
  db01.vm.synced_folder "../vprofile-code", "/vprofile-vm-data"
  db01.vm.provision "shell", inline: <<-SHELL
#   yum update -y
    yum install epel-release -y
    SHELL
  end


end
vagrant up
vagrant ssh