Vagrant : Getting Start

Vagrant คือตัวช่วยสร้าง Virtual Machine สามารถสร้างได้บน VirtualBox, VMWare, หรือ แม้กระทั่ง AWS

วิธีใช้

VagrantFile

$ vagrant init hashicorp/precise32

หลังจากคำสั่ง init VagrantFile จะถูกสร้างในไดเรกทอรี่ เนื้อหาประมาณนี้ ประมาณว่าจะลง hashicorp/precise32 แล้วนะ

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise32"
  # Put other configurations here
...
end

นี้คือลง hashicorp/percise32 ยังมีตัวอื่นๆอีกมากมาย

หรือถ้าต้องการ ubuntu desktop เขาก็จัดมาให้แล้วที่ http://cloud-images.ubuntu.com/ วิธีทำตามด้านล่าง

แต่แม่มมีแต่เวอร์ชั่นเซอร์เวอร์ว่ะ

vagrant box add myprecisevm http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box
vagrant init myprecisevm
vagrant up

NOTE : Vagrant.configure(“1”) or Vagrant.configure(“2”)

“1” represents the configuration from Vagrant 1.0.x.

“2” represents the configuration for 1.1+ to 2.0.x.

ref : https://www.vagrantup.com/docs/vagrantfile/version.html

1) Put Apache installation instruction

bootstrap.sh ( put in the same path as VagrantFile )

#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
 rm -rf /var/www
 ln -fs /vagrant /var/www
fi

VagrantFile

Vagrant.configure("2") do |config|
 config.vm.box = "hashicorp/precise32"
# Execute shell file
 config.vm.provision :shell, path: "bootstrap.sh"
end

NOTE : DocumentRoot of Apache to point to our /vagrant directory

2) Execute inline shell

config.vm.provision "shell", inline: "apt-get update"

or

config.vm.provision "shell", inline: <<-SHELL ssh-keyscan -H github.com >> /home/vagrant/.ssh/known_hosts
SHELL

x) Copy file

config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/id_rsa.pub"

x) Port Forwarding

Vagrant.configure("2") do |config|
 config.vm.box = "hashicorp/precise32"
# Port forwarding
 config.vm.provision :shell, path: "bootstrap.sh"
 config.vm.network :forwarded_port, guest: 80, host: 4567
end

Run http://127.0.0.1:4567 outside VM to see the result

3) Fix IP

Vagrant.configure("2") do |config|
 config.vm.box = "hashicorp/precise32"
 # Fix IP
 config.vm.network :private_network, ip: "192.168.13.100"
end

4) share folder

config.vm.provider :virtualbox do |vm|
vm.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/FOLDER_NAME", "1"]

# fix NAT DNS host service
vm.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end

/vagrant (in VM) is shared to folder that contains VagrantFile (outside VM)

ที่เจ๋งคือ มันเอาไฟล์พวก war ไปวางแชร์ไว้ได้

เช่น dev บนเครื่อง, สร้าง war ไปวางใน share folder, แล้วไป deploy บน VM

5) file

config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/id_rsa.pub"

6) puppet 

config.vm.provision "puppet" do |puppet|
puppet.manifests_path = "vagrant/manifests"
puppet.manifest_file = "default.pp"
end

note : do not forget to put vagrant/manifests/default.pp outside VM

7) boot_timeout

config.vm.boot_timeout = 600

increase the boot timeout if necesssary.

ref : https://stackoverflow.com/questions/32731629/where-to-find-config-vm-boot-timeout

better solution : https://dsin.wordpress.com/2017/04/21/vagrant-vagrant-up-timeout-with-a-start-job-is-running-for-raise-network-interfaces-in-vm/

Vagrant Command : https://dsin.wordpress.com/2017/11/30/vagrant-command/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s