Wednesday, August 5, 2020

Setup the VM machine using vagrant



Install VirtualBox

Install Vagrant

  Install VirtualBox  
        https://www.virtualbox.org/wiki/Downloads  --  for windows
                           
Install Vagrant
              https://releases.hashicorp.com/vagrant/  -- select the version (msi file for windows)


  • Create a folder vagrant and open a command prompt
  • run the command vagrant init from opened command prompt.
  • A vagrant file will create in open folder.
  • write a vagrant file for os.
Creating centos virtual machine.


Sample vagrant file for centos and past this line in vagrant file
  
     VAGRANTFILE_API_VERSION = "2"
 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 config.vm.box = "centos/7"
 config.vm.hostname = "Server01"
 config.ssh.insert_key = false
 #config.vm.provision :shell, path: "VAGRANT_ENV/bootstrap.sh"
 # Create a private network, which allows host-only access to the machine
 # using a specific IP.
  config.vm.network :private_network, ip: "192.168.33.10"
 config.vm.network "forwarded_port", guest: 80, host: 8888
 config.vm.network :public_network, :bridge => "eth1", :auto_config => false
 # Share an additional folder to the guest VM
 # config.vm.synced_folder ".", "/vagrant", disabled: true
 # config.vm.synced_folder ".", "/home/vagrant/provision", type: "rsync"
 config.vm.provider :virtualbox do |vb|
 # Set VM memory size
 vb.customize ["modifyvm", :id, "--memory", "20000"]
 # these 2 commands massively speed up DNS resolution, which means outbound
 # connections don't take forever (eg the WP admin dashboard and update page)
 vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
 vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
 end
 end

Multiple virtual machine from vagrant file

Vagrant.configure("2") do |config|

config.ssh.insert_key = false
config.vm.provider :virtualbox do |vb|
  vb.memory = 256
  vb.cpus = 1
end

#Disabling the default /vagrant share
config.vm.synced_folder ".", "/vagrant", disabled: true
MACHINE = ["docker-swarm-01","docker-swarm-01","docker-swarm-01"]
N = 2

(0..N).each do |i|
config.vm.define "server#{i}" do |node|
  node.vm.hostname = MACHINE[i]
  node.vm.box = "centos/7"
  node.vm.network :private_network, ip: "192.168.3.#{10+i}"
end
end
end 


and we need to run vagrant up from command prompt from vagrant file folder 

And will see the operating system in Virtual box 

    
    


   

Setup the VM machine using vagrant

Install VirtualBox Install Vagrant   Install VirtualBox            https://www.virtualbox.org/wiki/Downloads   --  for windows              ...