Friday, 24 January 2025

Vagrant Learning : Day 2

On Day 1, we understood what Vagrant is all about and how to get started with using Vagrant. So we download 1 Box (bento/ubuntu-16.04) and configured a VM using the same with very basic configuration in VAGRANTFILE. Today we'll explore some more configuration options and also see how to manage this VM. 

Let's start the created VM again..

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: 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
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run. 

There are 2 things to notice here,
a) It automatically enables port forwarding, that means I should be able to connect via SSH as well, let's see.

==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)

#Testing SSH Connectivity

PS C:\Users\parvi> ssh vagrant@localhost -p 2222
The authenticity of host '[localhost]:2222 ([127.0.0.1]:2222)' can't be established.
ED25519 key fingerprint is SHA256:x+KKvcYn8P7Eoudbv+adxtEXXG/tFDqUB68jgCJydGU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:2222' (ED25519) to the list of known hosts.
vagrant@localhost's password:
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
Last login: Tue Jan 21 17:44:34 2025 from 10.0.2.2
vagrant@vagrant:~$ hostname
vagrant
vagrant@vagrant:~$

b) It is also creating a shared folder. And it points to HOME directory where I have my VAGRANTFILE.

==> default: Mounting shared folders...
    default: C:/Vagrant/Demo => /vagrant

VM Management IMP Commands

There are few more command to manage running VM like,

vagrant halt	#to stop the VM
vagrant up	#to start the VM
vagrant reload	#to restart the VM
vagrant destroy -f	#to remove the VM

This is how the file structure is created with this build,

vagrant@vagrant:/etc$ 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   62G  87% /vagrant
tmpfs                         201M     0  201M   0% /run/user/1000
vagrant@vagrant:/etc$

Shared Folder Access & Usage


Let's see what's inside /vagrant, which is a shared folder.

vagrant@vagrant:/etc$ cd /vagrant/
vagrant@vagrant:/vagrant$ ls
Vagrantfile
vagrant@vagrant:/vagrant$

Like I said, it points to HOME directory, let me create text1, text2, text3 manually on Windows OS and see if those will be accessible here.

vagrant@vagrant:/vagrant$ ls -lrt
total 4
-rwxrwxrwx 1 vagrant vagrant 444 Jan 21 17:31 Vagrantfile
-rwxrwxrwx 1 vagrant vagrant   0 Jan 24 17:02 text1.txt
-rwxrwxrwx 1 vagrant vagrant   0 Jan 24 17:03 text2.txt
-rwxrwxrwx 1 vagrant vagrant   0 Jan 24 17:03 text3.txt
vagrant@vagrant:/vagrant$

Yes, they are, which means I can access file from OS that easily. Moreover I can also, have any script configured to be run with VM creation or provision it to be run with VM reboot. I just need to add entry in VAGRANTFILE like,

config.vm.provision "shell", inline: <<-SHELL
  sh /vagrant/dummy_script.sh
SHELL

This gives flexibility to have more than just OS installation configured with Vagrant and you can have the entire architecture setup and configured in 1 go. That is what we are targeting to learn and probably I'll need to make more capacity in my HOST machine to manage big architectures like Oracle RAC.

User Management


At part of last learning today, with default setup, we login as VAGRANT user having password also as "vagrant". But you can switch to "root" OR create any other user and switch to that from this VAGRANT user, for example,

vagrant@vagrant:/vagrant$ sudo su
root@vagrant:/vagrant# adduser oracle
Adding user `oracle' ...
Adding new group `oracle' (1001) ...
Adding new user `oracle' (1001) with group `oracle' ...
Creating home directory `/home/oracle' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for oracle
Enter the new value, or press ENTER for the default
        Full Name []: oracle
        Room Number []: oracle
        Work Phone []: oracle
        Home Phone []: oracle
        Other []: oracle
Is the information correct? [Y/n] Y
root@vagrant:/vagrant# su oracle
oracle@vagrant:/vagrant$ ls
text1.txt  text2.txt  text3.txt  Vagrantfile
oracle@vagrant:/vagrant$

To be continued....

No comments:

Post a Comment