最近在開發上經常要部署伺服器,部署前必需要進行練習與測試,但因為使用遠端機器測試很麻煩,加上部署時環境出了問題又要重灌,十分浪費時間。所以就花了點時間學習使用Vagrant這個軟體,今天來分享給大家如何使用Vagrant的基本功能。
到底Vagrant是甚麼呢?
Vagrant是一款可以構建同配置虛擬開發環境的軟體。
為甚麼要用Vagrant?
Vagrant可以快速構建VM,部驟簡單,實現多機器環境架構,方便開發者進行開發工作。
廢話不多說,接下來教大家如何使用Vagrant。
下載安裝Vagrant與Virtual Box
Vagrant早期以VirtualBox作為虛擬化軟體,但1.1以後的版本開始可以使用VMware等。這篇文章以VirtualBox為虛據器作教學,所以首先需要安裝Vagrant與Virtual Box,兩個軟體都可以到他們的官網下載(Virtual Box官網 和 Vagrant官網)。根據要運行的系統下載合適的版本,安裝亦十分方便。
使用Vagrant新增作業系統
建立VM前,大家請先想好VM要使用甚麼作業系統,想好再來就是要下載作業系統,那到底要到哪裹下載適合Vagrant用的作業系統呢?根據Vagrant上描述,一個打包好的作業系統稱作Box,而官網上就提供了一個網址(HashiCorp’s Atlas box catalog)可以下載Box。除了這個網址外,網路上亦有很多提供別人分享出來的Box的網站。
//下載box的指令格式 $ vagrant box add [您想要的Box名稱] [下載網址]//下載一個名為hashicorp/precise32的box,如沒有指定網址,則預設會到上述提到的<a href="https://atlas.hashicorp.com/boxes/search" target="_blank">HashiCorp's Atlas box catalog</a>網址下載 $ vagrant box add hashicorp/precise32//從指定網址下載box,例如:在<a href="http://www.vagrantbox.es" target="_blank">www.vagrantbox.es</a>網站選好一個CentOS 6.6版本 $ vagrant box add centos-6-6 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box鏈入指令後Vagrant便會開始下載Box,下載後便可以用來建立VM囉。有時候開發工作上,需要下載很多不同的Box,下載後可以使用以下指令查看所有下載的Box。
$ vagrant box list好了,下載完成Box之後,我們便正式可以建立VM。首先,建立虛擬機器必須先產生一個設定檔來設定VM,第一步需要為您的專案建立一個資料夾,進入資料夾下指令vagrant init 或 vagrant init Box名稱來產生設定檔,執行成功後資料夾下會多了一個名為Vagrantfile的檔案,該檔案就是設定檔。
$ mkdir [Project name] $ cd [Project name] // 如沒有指定Box名稱,Vagrantfile下會指定使用base,需要手動將檔案內的base更改為安裝的Box名稱,Vagrantfile接下來再作說明。 $ vagrant init //或 $ vagrant init [Box名稱]$ vagrant init [Box名稱] 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.啓動與連接VM
預設的設定檔已經是可以直接運行的,執行vagrant up指令讓VM運行起來。
$ vagrant up$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box '[Box 名稱]' is up to date... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 => 2222 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... default: Warning: Remote connection disconnect. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.3.26 default: VirtualBox Version: 5.0 ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => /path/[Project name] ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.執行完成後,代表VM已啓動,我們可以透過vagrant ssh連入VM。vagrant登入所使用的用戶預設為vagrant,現在您可以隨意玩弄您的VM了。
$ vagrant ssh共享資料夾
如何將本機的檔案放置到VM呢?Vagrant建立出來的VM內有一個共享資料夾/vagrant,該資料夾會與vagrant設定檔所在的資料夾共享資料,所以想要將檔案放置到VM,直接把檔案放在設定檔的資料夾便可。
shell腳本自動化配置系統
假如系統要用作為Web伺服器,我們需要一個Web軟體,例如安裝Apache。將安裝軟體的指令寫成一個shell服本,這裹命名為bootstrap.sh。
#!/usr/bin/env bash yum install -y httpd if ! [ -L /var/www ]; then //移除預設Web的路徑 rm -rf /var/www //建立捷徑直接指向共享資料夾 ln -fs /vagrant /var/www fi編輯bootstrap.sh完成後,我們要將Vagrantfile進行修改,修改後執行指令vagrant reload –provision使VM在重啓時便自動執行腳本。
Vagrant.configure("2") do |config| config.vm.box = "[Box名稱]" config.vm.provision :shell, path: "bootstrap.sh" end將常用的VM環境打包成Box
自己身為一個開發員,時間永遠覺得是不夠用,而重覆的部署行為浪費了很多時間。為了提升效率和處理更多有意義的事情,我們除了可以下載別人建立的Box之外,亦可以將自己常用的VM環境打包成Box,方便日後重覆使用,節省了大部份時間。配置好常用的VM環境後,執行vagrant package指令來打包吧!
$ vagrant package ==> default: Attempting graceful shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Exporting VM... ==> default: Compressing package to: /path/Project name/package.box打包完成後,當前資料夾下會建立一個名為vagrant.box的Box檔案,我們便可以將這個Box加入到Box list,日後我們便可以使用這個Box建立相同的系統環境。
$ vagrant box add [custom-box-name] package.box $ vagrant box listVagrant指令
//關閉VM $ vagrant halt //暫停VM $ vagrant suspend //恢復暫停的VM $ vagrant resume //重啓VM $ vagrant reload鏈結到這頁!

reload 的意思是重裝整個vm 嗎?所以我要保存之server 設定是不是只能暫停
不是,vagrant reload的說明是「restarts vagrant machine, loads new Vagrantfile configuration」,只是重啓VM和重載Vagrantfile配置文件