Hello Vagrant

回想之前,想要安裝個虛擬機是多麼的麻煩。先要費盡心機找到想要的操做系統鏡像文件,而後安裝虛擬化軟件,按照其提供的GUI界面操做一步步建立,整個過程費時費力。可是,自從使用了Vagrant之後,咱腰不酸了,腿不痛了,一口氣起5個虛擬機還不費勁。mysql

Vagrant是什麼?

這是官網上Vagrant的介紹。web

Create and Configure lightweight, reproducible, and portable development environments.sql

即用來建立和配置輕量級、可重現的、便攜式的開發環境。shell

使用Vagrant能夠將建立虛擬機的整個過程自動化起來,並具備高度的重用性。假如你是個開發者,你能夠很容易爲每一個團隊成員建立如出一轍的開發環境,從根本上防止‘在個人機器上能夠工做’之類的bug。假如你是個測試人員,能夠一鍵建立多個如出一轍的測試環境並行跑測試,而且跑完測試後還能夠一鍵銷燬這些測試環境,達到真正的按需建立。若是你是devops成員,須要和AWS、Chef之類的工具打交道,那麼Vagrant是個很好的結合點。你能夠經過Vagrant在AWS上直接建立虛擬機,而且自動運行Chef的腳本配置你的新虛擬機。數據庫

幾個概念

正式介紹Vagrant功能以前先了解一下Vagrant使用的一些概念。apache

  • Provider - 供應商,在這裏指Vagrant調用的虛擬化工具。Vagrant自己並無能力建立虛擬機,它是調用一些虛擬化工具來建立,如VirtualBox,VMWare,甚至AWS。json

  • Box - 可被Vagrant直接使用的虛擬機鏡像文件。針對不一樣的Provider,Box文件的格式是不同的。ubuntu

  • Vagrantfile - Vagrant根據Vagrantfile中的配置來建立虛擬機。在Vagrantfile文件中你須要指明使用哪一個Box,須要預安裝哪些軟件,虛擬機的網絡配置等。windows

Vagrant的安裝

安裝Vagrant很是簡單,能夠在Downloads頁面選擇最新的版本安裝。Vagrant支持Windows、Linux、Mac等平臺。api

Box管理

使用Vagrant以前先要給Vagrant添加Box,也就是可供Vagrant使用的虛擬機鏡像文件。Vagrant官網自己維護了一些鏡像文件,咱們能夠直接使用。http://www.vagrantbox.es/上面有更多的box能夠供咱們使用。

1
2
3
4
5
6
#添加名爲precise32的box文件  $ vagrant init precise32 http://files.vagrantup.com/precise32.box $ vagrant box list precise32 (virtualbox)  $ vagrant box remove precise64 virtualbox 

能夠看到Box與Provider是相關的,每一個Box都必須指定Provier,只有使用對應的Provier才能正確使用Box。

建立並運行虛擬機

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ vagrant box list precise32 (virtualbox) $ vagrant init precise32 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. $ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'precise32'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Booting VM... [default] Waiting for machine to boot. This may take a few minutes... [default] Machine booted and ready! [default] Mounting shared folders... [default] -- /vagrant 

vagrant init precise32會在當前目錄下生成一個Vagrantfie文件,其使用precise32做爲box。vagrant up則是使用virtual box這個provider來初始化並啓動precise32這個虛擬機。

咱們能夠詳細的看看Vagrantfile這個文件。

Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- mode: ruby -*- # vi: set ft=ruby :  # Vagrantfile API及語法版本 VAGRANTFILE_API_VERSION = "2"  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|   # 使用的box  config.vm.box = "precise32"   # Create a forwarded port mapping which allows access to a specific port  # within the machine from a port on the host machine. In the example below,  # accessing "localhost:8080" will access port 80 on the guest machine.  # config.vm.network :forwarded_port, guest: 80, host: 8080   # 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"   # Create a public network, which generally matched to bridged network.  # Bridged networks make the machine appear as another physical device on  # your network.  # config.vm.network :public_network   # If true, then any SSH connections made will enable agent forwarding.  # Default value: false  # config.ssh.forward_agent = true   # Share an additional folder to the guest VM. The first argument is  # the path on the host to the actual folder. The second argument is  # the path on the guest to mount the folder. And the optional third  # argument is a set of non-required options.  # config.vm.synced_folder "../data", "/vagrant_data"   # Provider-specific configuration so you can fine-tune various  # backing providers for Vagrant. These expose provider-specific options.  # Example for VirtualBox:  #  # config.vm.provider :virtualbox do |vb|  # # Don't boot with headless mode  # vb.gui = true  #  # # Use VBoxManage to customize the VM. For example to change memory:  # vb.customize ["modifyvm", :id, "--memory", "1024"]  # end  #  # View the documentation for the provider you're using for more  # information on available options.   # Enable provisioning with Puppet stand alone. Puppet manifests  # are contained in a directory path relative to this Vagrantfile.  # You will need to create the manifests directory and a manifest in  # the file precise32.pp in the manifests_path directory.  #  # An example Puppet manifest to provision the message of the day:  #  # # group { "puppet":  # # ensure => "present",  # # }  # #  # # File { owner => 0, group => 0, mode => 0644 }  # #  # # file { '/etc/motd':  # # content => "Welcome to your Vagrant-built virtual machine!  # # Managed by Puppet.\n"  # # }  #  # config.vm.provision :puppet do |puppet|  # puppet.manifests_path = "manifests"  # puppet.manifest_file = "site.pp"  # end   # Enable provisioning with chef solo, specifying a cookbooks path, roles  # path, and data_bags path (all relative to this Vagrantfile), and adding  # some recipes and/or roles.  #  # config.vm.provision :chef_solo do |chef|  # chef.cookbooks_path = "../my-recipes/cookbooks"  # chef.roles_path = "../my-recipes/roles"  # chef.data_bags_path = "../my-recipes/data_bags"  # chef.add_recipe "mysql"  # chef.add_role "web"  #  # # You may also specify custom JSON attributes:  # chef.json = { :mysql_password => "foo" }  # end   # Enable provisioning with chef server, specifying the chef server URL,  # and the path to the validation key (relative to this Vagrantfile).  #  # The Opscode Platform uses HTTPS. Substitute your organization for  # ORGNAME in the URL and validation key.  #  # If you have your own Chef Server, use the appropriate URL, which may be  # HTTP instead of HTTPS depending on your configuration. Also change the  # validation key to validation.pem.  #  # config.vm.provision :chef_client do |chef|  # chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"  # chef.validation_key_path = "ORGNAME-validator.pem"  # end  #  # If you're using the Opscode platform, your validator client is  # ORGNAME-validator, replacing ORGNAME with your organization name.  #  # If you have your own Chef Server, the default validation client name is  # chef-validator, unless you changed the configuration.  #  # chef.validation_client_name = "ORGNAME-validator" end 

從上述的文件能夠看出Vagrantfile能夠配置不少東西,好比使用的Box,須要轉發的端口,同步指定的目錄,使用Chef、puppet等對虛擬機進行預配置等。

若是修改了Vagrantfile中的配置,只須要執行vagrant reload來應用新配置。

同步目錄

虛擬機啓動起來之後就能夠ssh上去了。

1
2
3
4
5
6
7
8
9
10
11
$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)   * Documentation: https://help.ubuntu.com/ Welcome to your Vagrant-built virtual machine. Last login: Wed Oct 2 09:41:08 2013 from 10.0.2.2 vagrant@precise32:~$ who vagrant pts/0 2013-10-02 09:47 (10.0.2.2) vagrant@precise32:~$ hostname precise32 vagrant@precise32:~$ 

Vagrant會自動給虛擬機根目錄下建立一個名爲vagrant的目錄。這個目錄能夠與主機Vagrantfile所在的目錄保持同步。這個同步是相互的,不管改動了主機目錄中的文件,仍是虛擬機目錄中的文件,均可以自動同步到另外一方。

1
2
3
4
5
6
7
8
9
vagrant@precise32:~$ cd /vagrant/ vagrant@precise32:/vagrant$ ls Vagrantfile vagrant@precise32:/vagrant$ touch test.txt vagrant@precise32:/vagrant$ exit logout Connection to 127.0.0.1 closed. $ ls Vagrantfile test.txt 

多機器管理

其實Vagrantfile支持配置多臺機器,若是你須要設置多臺服務器及數據庫環境,能夠用一個Vagrantfile搞定。

Vagrantfile
1
2
Vagrant.configure("2") do |config| config.vm.provision "shell", inline: "echo Hello" config.vm.define "web" do |web| web.vm.box = "apache" end config.vm.define "db" do |db| db.vm.box = "mysql" end end 

這個文件配置了兩個box,一個叫web,一個叫db。如今啓動虛擬機就須要加上虛擬機名了。

1
2
3
4
5
6
7
8
#啓動web虛擬機 $ vagrant up web  #啓用db虛擬機 $ vagrant up db  #默認啓動全部的虛擬機 $ vagrant up 

關閉虛擬機

Vagrant提供了好幾種方法來關閉虛擬機,你能夠根據不一樣的狀況選擇不一樣的方式。

vagrant suspend將虛擬機置於休眠狀態。這時候主機會保存虛擬機的當前狀態。再用vagrant up啓動虛擬機時可以返回以前工做的狀態。這種方式優勢是休眠和啓動速度都很快,只有幾秒鐘。缺點是須要額外的磁盤空間來存儲當前狀態。

vagrant halt則是關機。若是想再次啓動仍是使用vagrant up命令,不過須要多花些時間。

vagrant destroy則會將虛擬機從磁盤中刪除。若是想從新建立仍是使用vagrant up命令。

另外1.2以上版本的Vagrant還引用了插件機制。能夠經過vagrant plugin來添加各類各樣的plugin,這給Vagrant的應用帶來了更大的靈活性和針對性。好比能夠添加vagrant-windows的插件來增長對windows系統的支持,經過添加vagrant-aws插件來實現給AWS建立虛擬機的功能。你也能夠編寫本身的插件。因爲Vagrant是ruby寫的一個gem,其插件的編寫也是使用的Ruby語言。這裏就很少作介紹了。感興趣的能夠去官網查看。

相關文章
相關標籤/搜索