02 - Vagrant
Download the following initial tools for DevOps Labs
- VirtualBox - www.virtualbox.org
- Gitscm - git-scm.com/download - Git Bash ( Unix based console in windows )
- Vagrant - vagrantup.com/downloads
- Search vagrant cloud for Market Place
- https://app.vagrantup.com/boxes/search
Vagrant
- Vagrant is computer software for creating and configuring virtual development environments.
- It can be seen as wrapper around virtualization software such as VirtualBox, KVM, VMware, and around configuration management software such as Ansible, Chef, Salt or Puppet.
- Vagrant is simpler way to Virtualizaiton and configuraiton management.
- Create folder for lab
- mkdir devlab
- cd devlab
- mkdir vm1
- mkdir vm2
- cd vm1
- Initiat vagrant and config new vm
- vagrant init
- vi vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "bento/centos-7.5"
config.vm.hostname = "testVM"
# *** Define VM box and host name
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |v|
v.name = "testVM"
end
# *** Define network and port
config.vm.network "forwarded_port" , guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.33.14"
# *** Define Memory and CPU
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = "2"
end
end
Useful Commands
- vagrant init -- Initialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you'll need to specify a base image in the Vagrantfile.
- vagrant up -- starts vagrant environment (also provisions only on the FIRST vagrant up)
- vagrant ssh -- connects to machine via SSH
- vagrant ssh <boxname> -- If you give your box a name in your Vagrantfile, you can ssh into it with boxname. Works from any directory.
- vagrant halt -- stops the vagrant machine
- vagrant suspend -- suspends a virtual machine (remembers state)
- vagrant destroy -- stops and deletes all traces of the vagrant machine
- vagrant destroy -f -- same as above, without confirmation
- vagrant -v -- get the vagrant version
- vagrant status -- outputs status of the vagrant machine
- vagrant global-status -- outputs status of all vagrant machines
- vagrant global-status --prune -- same as above, but prunes invalid entries
- vagrant provision --debug -- use the debug flag to increase the verbosity of the output
- vagrant push -- yes, vagrant can be configured to deploy code!
- vagrant up --provision | tee provision.log -- Runs vagrant up, forces provisioning and logs all output to a file
- sudo su -
- ifconfig
- free -m
- cat /proc/cpuinfo
- vagrant halt ** VM power off
- vagrant destory ** Destory VM
- rm -rf .vagrant
Create 2 VM's using the static configuration
$ vi Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "vm1" do |vm1|
vm1.vm.box = "ubuntu/bionic64"
vm1.vm.hostname = "linuxvm1"
vm1.vm.network "private_network", ip: "192.168.33.30"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = "2"
vb.name = "linuxvm1"
end
end
config.vm.define "vm2" do |vm2|
vm2.vm.box = "ubuntu/bionic64"
vm2.vm.hostname = "linuxvm2"
vm2.vm.network "private_network", ip: "192.168.33.31"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = "1"
vb.name = "linuxvm2"
end
end
end
Vagrant create multiple VMs loops
$ vi Vagrantfile
Vagrant.configure("2") do |config|
config.vm.provider :virtualbox do |vb|
vb.memory = 1024
vb.cpus = 1
end
MACHINE = ["app1.dev","db.dev"]
N = 1
(0..N).each do |i|
config.vm.define "server#{i}" do |node|
node.vm.hostname = MACHINE[i]
node.vm.box = "bento/centos-7.5"
node.vm.network :private_network, ip: "192.168.33.#{10+i}"
end
end
# Update the server to latest version
config.vm.provision :shell, :inline => "yum install -y net-tools"
end
Thank you for sharing useful information with us.
ReplyDeleteDevOps Training
DevOps Online Training