Hello Vagrant


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

Vagrant是什么?

這是官網上Vagrant的介紹。

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

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

使用Vagrant可以將創建虛擬機的整個過程自動化起來,並具有高度的重用性。假如你是個開發者,你可以很容易為每個團隊成員創建一模一樣的開發環境,從根本上防止‘在我的機器上可以工作’之類的bug。假如你是個測試人員,可以一鍵創建多個一模一樣的測試環境並行跑測試,並且跑完測試后還可以一鍵銷毀這些測試環境,達到真正的按需創建。如果你是devops成員,需要和AWS、Chef之類的工具打交道,那么Vagrant是個很好的結合點。你可以通過Vagrant在AWS上直接創建虛擬機,並且自動運行Chef的腳本配置你的新虛擬機。

幾個概念

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

  • Provider - 供應商,在這里指Vagrant調用的虛擬化工具。Vagrant本身並沒有能力創建虛擬機,它是調用一些虛擬化工具來創建,如VirtualBox,VMWare,甚至AWS。

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

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

Vagrant的安裝

安裝Vagrant非常簡單,可以在Downloads頁面選擇最新的版本安裝。Vagrant支持Windows、Linux、Mac等平台。

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語言。這里就不多做介紹了。感興趣的可以去官網查看。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM