Vagrant Introduction
html
Vagrantprovides easy to configure, reproducible, and portable work environments builton top of industry-standard technology and controlled by a single consistentworkflow to help maximize the productivity and flexibility of you and your team.node
Toachieve its magic, Vagrant stands on the shoulders of giants. Machines areprovisioned on top of VirtualBox,VMware, AWS, or any other provider. Then, industry-standard provisioning toolssuch as shell scripts, Chef, or Puppet, can be used to automatically installand configuresoftwareon the machine. mysql
Writtenby Ruby.web
More refer:https://docs.vagrantup.com/v2/why-vagrant/index.htmlsql
Official website: https://www.vagrantup.com/shell
Mirror site: http://www.vagrantbox.es/ (download box file from here)apache
Vagrant commandscentos
vagrant -h (or vagrantcommand -h)網絡
box manages boxes: installation, removal, etc.ssh
connect connect to a remotely shared Vagrant environment
destroy stops and deletes all traces of thevagrant machine
global-status outputs statusVagrant environments for this user
halt stops the vagrant machine
help shows the help for a subcommand
init initializes a new Vagrant environment bycreating a Vagrantfile
login log in to HashiCorp's Atlas
package packages a running vagrant environment intoa box
plugin manages plugins: install, uninstall,update, etc.
provision provisions the vagrant machine
push deploys codein this environment to a configured destination
rdp connects tomachine via RDP
reload restarts vagrantmachine, loads new Vagrantfileconfiguration
resume resumea suspended vagrant machine
share shareyour Vagrant environment with anyone inthe world
ssh connects tomachine via SSH
ssh-config outputs OpenSSH valid configuration to connect to the machine
status outputs statusof the vagrant machine
suspend suspends themachine
up starts andprovisions the vagrant environment
version prints currentand latest Vagrant version
Vagrant configuration -- Vagrantfile (gave 3 examples)
- following case for single machine
Vagrant.configure("2") do |config|
config.vm.define :node1 do |node1|
node1.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "node1", "--memory", "1024"] #v.gui = true
end
node1.vm.box = " centos66"
node1.vm.hostname = "node1"
node1.vm.network :public_network, ip:"192.168.199.76", bridge: "eth0"
node1.vm.synced_folder "C:/workstation", "/data"
end
end
- following two case for multi machine
case 1:
Vagrant.configure("2") do |config|
config.vm.define :node1 do |node1|
node1.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "node1", "--memory", "1024"]
#v.gui = true
end
node1.vm.box = "centos66"
node1.vm.hostname = "node1"
node1.vm.network :public_network, ip:"192.168.199.76", bridge: "eth0"
end
config.vm.define :node2 do |node2|
node2.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "node2", "--memory", "1024"]
end
node2.vm.box = "centos66"
node2.vm.hostname = "node2"
node2.vm.network :public_network, ip: "192.168.199.77", bridge: "eth0"
#node2.vm.synced_folder "/u2/vm/mysql/node2_data", "/data"
end
end
case 2:
Vagrant.configure("2") do |config|
{
:node1 => {
:box => 'centos66',
:box_url => 'http://192.168.1.200/vms/centos66_v2.box',
},
:node2 => {
:box => 'centos66',
:box_url => 'http://192.168.1.200/vms/centos66_v2.box',
},
}.each do |name,cfg|
config.vm.synced_folder "./", "/data/web/apache_default/",
owner: "apache", group: "deploycode"
config.vm.define name do |local|
local.vm.box = cfg[:box]
local.vm.box_url = cfg[:box_url]
local.vm.network "public_network"
#local.vm.network "forwarded_port", guest: 80, host: 2080,
#auto_correct: true
#local.vm.network "forwarded_port", guest: 3306, host: 3310,
#auto_correct: true
local.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", 512, "--cpus", 1]
end
end
end
end
Vagrantfile parameters explain
node1.vm.provider"virtualbox " 定義使用virtualbox做爲虛擬化平臺
v.customize["modifyvm", :id,"--name", "node1", "--memory", "1024"] 定義虛擬機大小
v.gui= true 定義啓動虛擬機的時候是否啓動GUI界面,true爲啓動,false不啓動
node1.vm.box= "centos66" 定義虛擬機使用哪一個box文件
node1.vm.hostname= "node1" 定義虛擬機的主機名
node1.vm.synced_folder「 C:/workstation」,「 /data 」 把本地文件夾共享給虛擬機,支持邏輯路徑,即./
node1.vm.network:public_network, ip:「192.168.199.76」,bridge: 「eth0 定義虛擬的網絡類型,分配IP,使用網橋的方式並綁定到eth0上
-- 網絡類型有:
Public network:公共網絡(虛擬機和宿主機同樣的網絡)
eg: config.vm.network" public_network", ip: "192.168.1.200 "
Privatenetwork:私有網絡(宿主機能夠訪問虛擬機,虛擬機間能夠相互訪問,
虛擬機能夠訪問外網)
eg: config.vm.network" private_network ", ip: "192.168.1.200"
Forwardedport:端口映射,把宿主機的某一端口映射到虛擬機的某一端口上
eg: config.vm.forwarded_port80,8080, protocol: 「udp」 默認只轉發tcp協議
包
Vagrantfile complete case
cd C:\workstation
Download a box file into this directory,such ascentos66_v2.box
vagrantbox add centos66 centos66_v2.box
vagrantbox list
vagrant init
Modified the Vagrantfile as before
vagrant up centos66
vagrant status centos66
Then you can login into this machine and install MySQL
- ip:port -> 127.0.0.1:2222
- username:password -> vagrant:vagrant
- login to super user(root) with sudo su -
vagrant halt
vagrant package --base centos66 --output centos66_new.box --vagrantfile Vagrantfile
vagrant destroy centos66
vagrant box remove centos66