Tuesday, 21 January 2025

Understanding Vagrant - Day 1

I know I am late but finally I got the opportunity to see what VAGRANT is all about. In this article, I am going to cover everything I'll go as part of my 1st learning experience with VAGRANT so if you are also new to this, let's see it together.

Installing Vagrant:

You can download the latest Vagrant software here. For me, it's Version: 2.4.3 and I have downloaded AMD64 for my Windows machine.

The installation is quite easy so if you have every installed any software, it should not be difficult for you.

Configuring Boxes:

A BOX in Vagrant terminology is pre-built VM image. It can be simple OS installation or more than that, depending upon the configuration options in VAGRANTFILE and what you exactly want it to do. For example, you can setup Vagrant to configure Oracle RAC environment in 1 go and that's where we intend to go with this learning.

A VAGRANTFILE is normally a collection of different variables and their assignments which helps with Installation/Configurations and finding components. To permanently fix your lookup path of Vagrantfile, you can use the Environment Variable VAGRANT_CWD.

Right now, since it's fresh installation, I am not having any boxes in my list,

PS C:\Users\parvi> vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.


For this tutorial, let's add 1 box. You can discover any pre-built boxes here.

PS C:\Users\parvi> vagrant box add bento/ubuntu-16.04
==> box: Loading metadata for box 'bento/ubuntu-16.04'
    box: URL: https://vagrantcloud.com/api/v2/vagrant/bento/ubuntu-16.04
==> box: Adding box 'bento/ubuntu-16.04' (v202212.11.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/bento/boxes/ubuntu-16.04/versions/202212.11.0/providers/virtualbox/unknown/vagrant.box
    box:
==> box: Successfully added box 'bento/ubuntu-16.04' (v202212.11.0) for 'virtualbox'!

Creating Vagrantfile: 


For this, just go to your directory where you would be keeping the VM files and initialize vagrant.

PS C:\Users\parvi> cd C:\Vagrant\Demo

PS C:\Vagrant\Demo> vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
PS C:\Vagrant\Demo>

Editing VagrantFile:


When opened in notepad, Mostly you'll see comments but if you'll focus, you'll know that there are just 3 uncommented lines, I am here just adding few more options (info provided in file's comments only and some web reference) to not breach my Machine's resource limit since I am new to this.

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-16.04"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048      # MBs.
    vb.cpus   = 1        
    vb.name   = "MyUbuntu"
    
    # Tell VirtualBox this VM is running on an SSD.
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', '0', '--nonrotational', 'on']
  end
end

Start the VM:


Run below command and wait till it returns the control.

C:\Vagrant\Demo>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/ubuntu-16.04' version '202212.11.0' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 6.1.40
    default: VirtualBox Version: 7.1
==> default: Mounting shared folders...
    default: C:/Vagrant/Demo => /vagrant

C:\Vagrant\Demo>

Note: If you get error like, "SizeOfImage (0x263000) isn't close enough to the mapping size (0x266000)", try upgrading Virtual Box version. I got this error and had to download the latest version which is 7.1.4. Post that, above command ran successfully.

Connect to New VM using SSH and run few commands for validation.

C:\Vagrant\Demo>vagrant ssh
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


This system is built by the Bento project by Chef Software
More information can be found at https://github.com/chef/bento
vagrant@vagrant:~$ df -h
Filesystem                    Size  Used Avail Use% Mounted on
udev                          979M     0  979M   0% /dev
tmpfs                         201M  5.3M  195M   3% /run
/dev/mapper/vagrant--vg-root   62G  1.2G   57G   3% /
tmpfs                        1001M     0 1001M   0% /dev/shm
tmpfs                         5.0M     0  5.0M   0% /run/lock
tmpfs                        1001M     0 1001M   0% /sys/fs/cgroup
/dev/sda1                     720M   59M  625M   9% /boot
vagrant                       454G  392G   63G  87% /vagrant
tmpfs                         201M     0  201M   0% /run/user/1000
vagrant@vagrant:~$
vagrant@vagrant:~$
vagrant@vagrant:~$ hostname
vagrant

To be continued....

No comments:

Post a Comment