vagrant 介绍,安装与使用
生活随笔
收集整理的這篇文章主要介紹了
vagrant 介绍,安装与使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
可以幫你統(tǒng)一團(tuán)隊(duì)成員的開發(fā)環(huán)境。如果你或者你的伙伴創(chuàng)建了一個(gè)Vagrantfile,那么你只需要執(zhí)行vagrant up就行了,所有的軟件都會(huì)安裝并且配置好。團(tuán)隊(duì)成員可以通過(guò)相同的Vagrantfile來(lái)創(chuàng)建他們的開發(fā)環(huán)境,無(wú)論他們是在Linux, Mac OS X, 或者Windows下,這樣就可以保證你團(tuán)隊(duì)成員的代碼是跑在相同的環(huán)境中。安裝非常簡(jiǎn)單,直接下載對(duì)應(yīng)操作系統(tǒng)的版本就可以了https://www.vagrantup.com/downloads.htmlVagrant 安裝后還不能直接使用,因?yàn)樗皇且粋€(gè)虛擬機(jī)管理和配置工具,虛擬機(jī)系統(tǒng)的安裝和運(yùn)行還得靠專門的虛擬化軟件。Vagrant 默認(rèn)已經(jīng)內(nèi)置了 VirtualBox Provider 用來(lái)跟 VirtualBox 交互,所以再去?VirtualBox 官網(wǎng)下載并安裝 VirtualBox 就可以正式開始使用了。
程序員搞最多的就是碼代碼了,可能做很多個(gè)項(xiàng)目,公司里搞java的,php的,自己回家再玩?zhèn)€python什么的,想體驗(yàn)下新版本的mongodb或者nginx,Emacs或者vim的配置啦,保不準(zhǔn)哪個(gè)項(xiàng)目前個(gè)版本的數(shù)據(jù)庫(kù)是mysql,下個(gè)版本用了postgresql, 這么多東西全都搞在一起,裝在一個(gè)電腦上,肯定會(huì)被這各種配置環(huán)境搞的暈暈的,煩不勝煩。怎么辦呢,最好是每個(gè)項(xiàng)目都有一個(gè)干凈的開發(fā)環(huán)境,只為這個(gè)項(xiàng)目,可是我們不可能為每一個(gè)項(xiàng)目配一個(gè)電腦吧,有了,虛擬機(jī),給每一個(gè)項(xiàng)目配一個(gè)虛擬機(jī),開發(fā)A的時(shí)候就啟A的虛擬機(jī),這樣各個(gè)開發(fā)環(huán)境互相獨(dú)立,干干凈凈。還有一個(gè)問(wèn)題,我們的項(xiàng)目有多個(gè)開發(fā)人員,如何保障大家的開發(fā)環(huán)境都一樣呢,總不能每個(gè)人都一個(gè)個(gè)點(diǎn)擊鼠標(biāo),填寫配置參數(shù),建好后上支一個(gè)個(gè)軟件安裝吧,這太麻煩了,太不geek了。我們想要的是,環(huán)境只配置一遍,然后可以把這個(gè)環(huán)境打包deliver給別人,別人拿到后,直接啟起來(lái)就可以用。那有沒有這樣的東西呢,肯定是有的,Vagrant,它就是用來(lái)干這個(gè)的。Create a single file for your project to describe the type of machine you want, the software that needs to be installed, and the way you want to access the machine. Store this file with your project code.Run a single command — "vagrant up"SSH into this machine with?vagrant sshThe first step in configuring any Vagrant project is to create aVagrantfile:
$ mkdir vagrant_getting_started $ cd vagrant_getting_started $ vagrant init This will place a?Vagrantfile?in your current directory.?specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.vagrant box add hashicorp/precise64the username is "hashicorp", and the box is "precise64".?run?vagrant destroy?back on your host machine, and Vagrant will terminate the use of any resources by the virtual machine.The?vagrant destroy?command does not actually remove the downloaded box file. To?completely?remove the box file, you can use the?vagrant box remove?command.By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the?/vagrant?directory in your guest machine.Note that when you?vagrant ssh?into your machine, you're in/home/vagrant.?/home/vagrant?is a different directory from the synced/vagrant?directory.logout退出!installing packages, users, and more with?provisioning:Let us now serve those files using a webserver.We could just SSH in and install a webserver and be on our way, but then every person who used Vagrant would have to do the same thing. Instead, Vagrant has built-in support for?automated provisioning. Using this feature, Vagrant will automatically install software when youvagrant up?so that the guest machine can be repeatably created and ready-to-use.Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :shell, path: "bootstrap.sh" endThe "provision" line is new, and tells Vagrant to use the?shellprovisioner to setup the machine, with the?bootstrap.sh?file. The file path is relative to the location of the project root (where the Vagrantfile is).If the guest machine is already running from a previous step, run?vagrant reload --provisionport forwarding allows you to specify ports on the guest machine to share via a port on the host machine外面的宿主機(jī). This allows you to access a port on your own machine, but actually have all the network traffic forwarded to a specific port on the guest machine.Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :shell, path: "bootstrap.sh" config.vm.network :forwarded_port, guest: 80, host: 4567 end?load?http://127.0.0.1:4567?in your browser.?分享Vagrant Share lets you share your Vagrant environment to anyone around the world with an Internet connection. It will give you a URL that will route directly to your Vagrant environment from any device in the world that is connected to the Internet.https://www.vagrantup.com/docs/getting-started/share.htmlBefore being able to share your Vagrant environment, you will need an account on?HashiCorp's Atlas. Do not worry, it is free.Once you have an account, log in using?vagrant login:$ vagrant login Username or Email: mitchellh Password (will be hidden): You are now logged in! SHAREOnce you are logged in, run?vagrant share:$ vagrant share ... ==> default: Your Vagrant Share is running! ==> default: URL: http://frosty-weasel-0857.vagrantshare.com ... Your URL will be different, so do not try the URL above. Instead, copy the URL that?vagrant share?outputted for you and visit that in a web browser. It should load the Apache page we setup earlier.To end the sharing session, hit?Ctrl+C?in your terminal.-----------------------------------停止:Suspending?the virtual machine by calling?vagrant suspend?will save the current running state of the machine and stop it. When you are ready to begin working again, just run?vagrant up, and it will be resumed from where you left off. The main benefit of this method is that it is super fast, usually taking only 5 to 10 seconds to stop and start your work. The downside is that the virtual machine still eats up your disk space, and requires even more disk space to store all the state of the virtual machine RAM on disk.Halting?the virtual machine by calling?vagrant halt?will gracefully shut down the guest operating system and power down the guest machine. You can use?vagrant up?when you are ready to boot it again. The benefit of this method is that it will cleanly shut down your machine, preserving the contents of disk, and allowing it to be cleanly started again. The downside is that it'll take some extra time to start from a cold boot, and the guest machine still consumes disk space.Destroying?the virtual machine by calling?vagrant destroy?will remove all traces 痕跡of the guest machine from your system. It'll stop the guest machine, power it down, and remove all of the guest hard disks. Again, when you are ready to work again, just issue a?vagrant up. The benefit of this is that?no cruft?is left on your machine. The disk space and RAM consumed by the guest machine is reclaimed 回收的and your host machine is left clean. The downside is that?vagrant up?to get working again will take some extra time since it has to reimport the machine and re-provision it.
程序員搞最多的就是碼代碼了,可能做很多個(gè)項(xiàng)目,公司里搞java的,php的,自己回家再玩?zhèn)€python什么的,想體驗(yàn)下新版本的mongodb或者nginx,Emacs或者vim的配置啦,保不準(zhǔn)哪個(gè)項(xiàng)目前個(gè)版本的數(shù)據(jù)庫(kù)是mysql,下個(gè)版本用了postgresql, 這么多東西全都搞在一起,裝在一個(gè)電腦上,肯定會(huì)被這各種配置環(huán)境搞的暈暈的,煩不勝煩。怎么辦呢,最好是每個(gè)項(xiàng)目都有一個(gè)干凈的開發(fā)環(huán)境,只為這個(gè)項(xiàng)目,可是我們不可能為每一個(gè)項(xiàng)目配一個(gè)電腦吧,有了,虛擬機(jī),給每一個(gè)項(xiàng)目配一個(gè)虛擬機(jī),開發(fā)A的時(shí)候就啟A的虛擬機(jī),這樣各個(gè)開發(fā)環(huán)境互相獨(dú)立,干干凈凈。還有一個(gè)問(wèn)題,我們的項(xiàng)目有多個(gè)開發(fā)人員,如何保障大家的開發(fā)環(huán)境都一樣呢,總不能每個(gè)人都一個(gè)個(gè)點(diǎn)擊鼠標(biāo),填寫配置參數(shù),建好后上支一個(gè)個(gè)軟件安裝吧,這太麻煩了,太不geek了。我們想要的是,環(huán)境只配置一遍,然后可以把這個(gè)環(huán)境打包deliver給別人,別人拿到后,直接啟起來(lái)就可以用。那有沒有這樣的東西呢,肯定是有的,Vagrant,它就是用來(lái)干這個(gè)的。Create a single file for your project to describe the type of machine you want, the software that needs to be installed, and the way you want to access the machine. Store this file with your project code.Run a single command — "vagrant up"SSH into this machine with?vagrant sshThe first step in configuring any Vagrant project is to create aVagrantfile:
- Mark the root directory of your project. Many of the configuration options in Vagrant are relative to this root directory.
- Describe the kind of machine and resources you need to run your project, as well as what software to install and how you want to access it.
$ mkdir vagrant_getting_started $ cd vagrant_getting_started $ vagrant init This will place a?Vagrantfile?in your current directory.?specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.vagrant box add hashicorp/precise64the username is "hashicorp", and the box is "precise64".?run?vagrant destroy?back on your host machine, and Vagrant will terminate the use of any resources by the virtual machine.The?vagrant destroy?command does not actually remove the downloaded box file. To?completely?remove the box file, you can use the?vagrant box remove?command.By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the?/vagrant?directory in your guest machine.Note that when you?vagrant ssh?into your machine, you're in/home/vagrant.?/home/vagrant?is a different directory from the synced/vagrant?directory.logout退出!installing packages, users, and more with?provisioning:Let us now serve those files using a webserver.We could just SSH in and install a webserver and be on our way, but then every person who used Vagrant would have to do the same thing. Instead, Vagrant has built-in support for?automated provisioning. Using this feature, Vagrant will automatically install software when youvagrant up?so that the guest machine can be repeatably created and ready-to-use.Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :shell, path: "bootstrap.sh" endThe "provision" line is new, and tells Vagrant to use the?shellprovisioner to setup the machine, with the?bootstrap.sh?file. The file path is relative to the location of the project root (where the Vagrantfile is).If the guest machine is already running from a previous step, run?vagrant reload --provisionport forwarding allows you to specify ports on the guest machine to share via a port on the host machine外面的宿主機(jī). This allows you to access a port on your own machine, but actually have all the network traffic forwarded to a specific port on the guest machine.Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :shell, path: "bootstrap.sh" config.vm.network :forwarded_port, guest: 80, host: 4567 end?load?http://127.0.0.1:4567?in your browser.?分享Vagrant Share lets you share your Vagrant environment to anyone around the world with an Internet connection. It will give you a URL that will route directly to your Vagrant environment from any device in the world that is connected to the Internet.https://www.vagrantup.com/docs/getting-started/share.htmlBefore being able to share your Vagrant environment, you will need an account on?HashiCorp's Atlas. Do not worry, it is free.Once you have an account, log in using?vagrant login:$ vagrant login Username or Email: mitchellh Password (will be hidden): You are now logged in! SHAREOnce you are logged in, run?vagrant share:$ vagrant share ... ==> default: Your Vagrant Share is running! ==> default: URL: http://frosty-weasel-0857.vagrantshare.com ... Your URL will be different, so do not try the URL above. Instead, copy the URL that?vagrant share?outputted for you and visit that in a web browser. It should load the Apache page we setup earlier.To end the sharing session, hit?Ctrl+C?in your terminal.-----------------------------------停止:Suspending?the virtual machine by calling?vagrant suspend?will save the current running state of the machine and stop it. When you are ready to begin working again, just run?vagrant up, and it will be resumed from where you left off. The main benefit of this method is that it is super fast, usually taking only 5 to 10 seconds to stop and start your work. The downside is that the virtual machine still eats up your disk space, and requires even more disk space to store all the state of the virtual machine RAM on disk.Halting?the virtual machine by calling?vagrant halt?will gracefully shut down the guest operating system and power down the guest machine. You can use?vagrant up?when you are ready to boot it again. The benefit of this method is that it will cleanly shut down your machine, preserving the contents of disk, and allowing it to be cleanly started again. The downside is that it'll take some extra time to start from a cold boot, and the guest machine still consumes disk space.Destroying?the virtual machine by calling?vagrant destroy?will remove all traces 痕跡of the guest machine from your system. It'll stop the guest machine, power it down, and remove all of the guest hard disks. Again, when you are ready to work again, just issue a?vagrant up. The benefit of this is that?no cruft?is left on your machine. The disk space and RAM consumed by the guest machine is reclaimed 回收的and your host machine is left clean. The downside is that?vagrant up?to get working again will take some extra time since it has to reimport the machine and re-provision it.
轉(zhuǎn)載于:https://www.cnblogs.com/elesos/p/7094692.html
總結(jié)
以上是生活随笔為你收集整理的vagrant 介绍,安装与使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 求怀念战友歌词!
- 下一篇: 求一个成熟男人的个性签名。