vagrant 基礎知識拾憶

1,介紹Vagrant

我們做web開發的時候經常要安裝各種本地測試環境,比如apache,php,mysql,redis等等。出于個人使用習慣,可能我們還是比較習慣用windows。雖然說在windows下搭建各種開發環境是可行的,各大開發環境都有windows版本。然而在windows下配置有時候會顯得繁瑣,并且還會導致開發環境(windows)和生產環境(lunix)不一致。

能不能在windows下也像linux那樣開發?也許你想到了,用虛擬機。用虛擬機裝個linux系統就好了。裝完linux系統就設置共享目錄,設置網絡端口映射,等等。好像也有那么點繁瑣。

還有,假如我們是一個團隊進行開發,那么每個人的電腦上都要裝個虛擬機+ linux系統+各種運行環境。手動設置麻煩不說,大家的開發環境不太一致(可能你裝了apcahe我裝了nginx等),也是頭疼。能不能把各種設置都自動化,并且保持整個團隊的開發環境一致呢?

Vagrant就是為了解決這個問題而生的。它使用開源 VirtualBox 作為虛擬化支持,可以輕松的跨平臺部署。

2,下載

下載VirtualBox:http://download.virtualbox.org/virtualbox/5.1.10/VirtualBox-5.1.10-112026-Win.exe

上面給出的是5.1.10版本的下載鏈接。要下載其他版本請訪問官網https://www.virtualbox.org/wiki/Downloads

下載Vagrant:

https://releases.hashicorp.com/vagrant/1.8.7/vagrant_1.8.7.msi

上面給出的是1.8.7版本的下載鏈接。要下載其他版本請訪問官網

http://www.vagrantup.com/downloads.html

下載虛擬鏡像:

https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box

上面給出的是centos-6.7鏡像下載鏈接,要下載其他鏡像請訪問官網

http://www.vagrantbox.es/

3,安裝

下載好上面的軟件包后,先安裝VirtualBox,然后安裝Vagrant。都是雙擊即可安裝的,所以沒什么好介紹。下面介紹下怎么把鏡像導入。

安裝完vagrant后 檢測是否安裝成功 打開cmd

C:\Users\Administrator>vagrant

Usage: vagrant [options] ?[]

-v, --version ? ? ? ? ? ? ? ? ? ?Print the version and exit.

-h, --help ? ? ? ? ? ? ? ? ? ? ? Print this help.

Common commands:

box ? ? ? ? ? ? manages boxes: installation, removal, etc.

connect ? ? ? ? connect to a remotely shared Vagrant environment

destroy ? ? ? ? stops and deletes all traces of the vagrant machine

global-status ? outputs status Vagrant environments for this user

halt ? ? ? ? ? ?stops the vagrant machine

help ? ? ? ? ? ?shows the help for a subcommand

init ? ? ? ? ? ?initializes a new Vagrant environment by creating a Vagrant

file

login ? ? ? ? ? log in to HashiCorp's Atlas

package ? ? ? ? packages a running vagrant environment into a box

plugin ? ? ? ? ?manages plugins: install, uninstall, update, etc.

port ? ? ? ? ? ?displays information about guest port mappings

powershell ? ? ?connects to machine via powershell remoting

provision ? ? ? provisions the vagrant machine

push ? ? ? ? ? ?deploys code in this environment to a configured destinatio

n

rdp ? ? ? ? ? ? connects to machine via RDP

reload ? ? ? ? ?restarts vagrant machine, loads new Vagrantfile configurati

on

resume ? ? ? ? ?resume a suspended vagrant machine

share ? ? ? ? ? share your Vagrant environment with anyone in the world

snapshot ? ? ? ?manages snapshots: saving, restoring, etc.

ssh ? ? ? ? ? ? connects to machine via SSH

ssh-config ? ? ?outputs OpenSSH valid configuration to connect to the machi

ne

status ? ? ? ? ?outputs status of the vagrant machine

suspend ? ? ? ? suspends the machine

up ? ? ? ? ? ? ?starts and provisions the vagrant environment

version ? ? ? ? prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced

or not commonly used. To see all subcommands, run the command

`vagrant list-commands`.

先新建一個工作目錄

比如我新建了E:\VagrantWorkcentos-6.7-x86_64

打開cmd命令提示符,進入新目錄,以我上面的目錄為例,輸入

C:\Users\Administrator>e:

E:\>cd VagrantWorkcentos-6.7-x86_64

然后輸入命令初始化

E:\VagrantWorkcentos-6.7-x86_64>vagrant init centos6.7

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-centos-6.7.box復制到本目錄E:\VagrantWorkcentos-6.7-x86_64下,執行

E:\VagrantWorkcentos-6.7-x86_64>vagrant box add centos6.7 vagrant-centos-6.7.box

==> box: Box file was not detected as metadata. Adding it directly...

==> box: Adding box 'centos6.7' (v0) for provider:

box: Unpacking necessary files from: file://E:/VagrantWorkcentos-6.7-x86_64/

vagrant-centos-6.7.box

box: Progress: 100% (Rate: 177M/s, Estimated time remaining: --:--:--)

The box you're attempting to add already exists. Remove it before

adding it again or add it with the `--force` flag.

Name: centos6.7

Provider: virtualbox

Version: 0

檢查是否導入成功

E:\VagrantWorkcentos-6.7-x86_64>vagrant box list

centos6.7 (virtualbox, 0)

4,配置

用文本編輯器打開D:VagrantWorkcentos-6.6-x86_64目錄下的Vagrantfile文件便可以進行一些常用配置。

下面列舉出幾個常用的配置。要用到其他配置請訪問官網文檔或者百度谷歌一下。

1,端口映射

config.vm.network :forwarded_port, guest: 80, host: 8080

把上面這句代碼前面的#號去掉。它表示映射本機的8080端口到虛擬機的80端口

2,如果需要自己自由的訪問虛擬機,但是別人不需要訪問虛擬機,可以使用private_network,并為虛擬機設置IP。

config.vm.network :private_network, ip: 192.168.1.10

把上面這句代碼前面的#號去掉即可

3,目錄同步:

config.vm.synced_folder "C:\D\www", "/vagrant_data"

第一個是本地目錄 第二個是vagrant虛擬機目錄

5,啟動

進入目錄E:\VagrantWorkcentos-6.7-x86_64后執行命令

vagrant up

E:\VagrantWorkcentos-6.7-x86_64>vagrant up

Bringing machine 'default' up with 'virtualbox' provider...

==> default: Importing base box 'centos6.7'...

==> default: Matching MAC address for NAT networking...

==> default: Setting the name of the VM: VagrantWorkcentos-67-x86_64_default_148

0154303134_79930

==> 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: 80 (guest) => 8080 (host) (adapter 1)

default: 22 (guest) => 2222 (host) (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: Remote connection disconnect. Retrying...

default:

default: Vagrant insecure key detected. Vagrant will automatically replace

default: this with a newly generated keypair for better security.

default:

default: Inserting generated public key within guest...

default: Removing insecure key from the guest if it's present...

default: Key inserted! Disconnecting and reconnecting using new SSH key...

==> 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 o

f

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 t

he

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.30

default: VirtualBox Version: 5.1

==> default: Configuring and enabling network interfaces...

==> default: Mounting shared folders...

default: /vagrant => E:/VagrantWorkcentos-6.7-x86_64

虛擬機啟動之后則可以通過vagrant ssh 聯入虛擬機進行進一步的環境配置,或者軟件安裝相關的工作,在Windows系統下,并不能直接通過 vagrant ssh 連到虛擬機,需要使用 Putty,Xshell 等第三方工具進行連接。連接地址192.168.1.10,端口22。登錄的帳號root/vagrant 密碼為vagrant

檢測狀態

E:\VagrantWorkcentos-6.7-x86_64>vagrant status

Current machine states:

default ? ? ? ? ? ? ? ? ? running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to

shut it down forcefully, or you can run `vagrant suspend` to simply

suspend the virtual machine. In either case, to restart it again,

simply run `vagrant up`.


上圖跟下面windows下面的目錄一一對應


打包分發

當你配置好開發環境后,退出并關閉虛擬機。在終端里對開發環境進行打包:

vagrant package

打包完成后會在當前目錄生成一個package.box 的文件,將這個文件傳給其他用戶,其他用戶只要添加這個 box 并用其初始化自己的開發目錄就能得到一個一模一樣的開發環境了。

導出指定的box

vagrant package --base vagrant_default_1437988069090_46714 --output php_new.box

其中 base指的是虛擬機名字, output輸出包名

6,導出

在cmd里進行工作目錄后,執行下面命令

vagrant package

完成后會在當前目錄就會生成package.box,之后新建虛擬機則可使用這個box。如果事先在你的虛擬機里建立好了各種開發環境,那么你直接把這個box給你的團隊其他成員安裝,這樣就可以省去一臺臺電腦部署的時間,還可以保持開發環境一致。很方便有木有。

7,其他命令

下面列舉出一些常用的cmd操作命令

vagrant up (啟動虛擬機)

vagrant halt (關閉虛擬機——對應就是關機)

vagrant suspend (暫停虛擬機——只是暫停,虛擬機內存等信息將以狀態文件的方式保存在本地,可以執行恢復操作后繼續使用)

vagrant resume (恢復虛擬機 —— 與前面的暫停相對應)

vagrant box remove centos6.6 (移除box,其中centos6.6是box名)

vagrant destroy (刪除虛擬機,刪除后在當前虛擬機所做進行的除開Vagrantfile中的配置都不會保留)

8,擴容

一直以來都是以vagrant+docker作為開發環境,可是久而久之,原Box自帶的8G容量就捉襟見肘了。時不時需要手動刪除一些東西。

Virtualbox 本身只支持vdi硬盤文件格式的擴容,對vmdk 格式的卻不支持。但是卻提供vmdk到vdi格式的轉化,正好可以利用這一功能進行擴容。

因為要用到VBoxManage,所以要把D:\Program Files\Oracle\VirtualBox這個加入path。

關閉虛擬機, 從Virtualbox頁面查看硬盤文件地址(選中虛擬機->右鍵->設置->存儲)(如:E:\VirtualBox VMs\VagrantWorkcentos-67-x86_64_default_1480157989392_34389)。進到文件所在目錄后執行:

$VBoxManage clonehd "centos-vm-disk1.vmdk" "centos-vm-disk1.vdi" --format VDI

給vdi格式硬盤文件擴容

$VBoxManage modifyhd centos-vm-disk1.vdi --resize 40960

從Virutalbox存儲界面刪除原硬盤文件,然后再加入新的VDI格式硬盤文件

vagrant up啟動虛擬機,然后vagrant ssh進入

利用cfdisk工具創建主分區,謹記選擇格式為Linux LVM (8e)

$ sudo cfdisk /dev/sda

cfdisk

這里的具體操作如下:

# vagrant ssh

# sudo su -

Findthe name of the logical volume mapping the file-systemison(ie./dev/mapper/VolGroupOS-lv_root).

# df

Findthe name of the physical volume(ordevice)that all the partitions are created on(ie./dev/sda).

# fdisk -l

Createanewprimary partitionforuseasaLinuxLVM

# fdisk /dev/sda

Pressp toprintthe partition table to identify the number of partitions.Bydefaultthere are two-sda1andsda2.

Pressn to create anewprimary partition.

Presspforprimary.

Press3forthe partition number,depending the output of the partition tableprint.

PressEntertwo times to accept thedefaultFirstandLastcylinder.

Presst to change the system's partition ID

Press 3 to select the newly creation partition

Type 8e to change the Hex Code of the partition for Linux LVM

Press w to write the changes to the partition table.

Reboot the machine, then ssh back in when it is up again and switch to the root user once more.

# reboot

# vagrant ssh

# sudo su -

利用pvcreate命令給新的分區創建物理卷

$sudo pvcreate /dev/sda3

Physical volume "/dev/sda3" successfully created

查看VG Name,我自己的VG Name是centos

$ sudo pvdisplay|grep"VG Name"

VGName ? ? ?VolGroup

將新分區擴展到centos這個組

$vgextend ? ?VolGroup ? /dev/sda3

Volume group "VolGroup" successfully extended

擴展邏輯分區

$?lvextend /dev/mapper/VolGroup-lv_root /dev/sda3

resize并且生效

$ resize2fs /dev/mapper/VolGroup-lv_root

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 3

Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 10098688 (4k) blocks.

The filesystem on /dev/mapper/VolGroup-lv_root is now 10098688 blocks long.

在centos 7下,這一步會出錯

resize2fs1.42.9(28-Dec-2013)

resize2fs:Badmagic numberinsuper-blockwhiletrying to open/dev/mapper/centos-root

Couldn't find valid filesystem superblock.

resize2fs

這時,只需要使用xfs_growfs命令替換就行了

xfs_growfs /dev/mapper/VolGroup-lv_root

xfs_growfs: /dev/mapper/VolGroup-lv_root is not a mounted XFS filesystem

至此,大分告成。vagrant reload重啟虛擬機查看效果

df -h

Filesystem? ? ? ? ? ? Size? Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root

38G? 5.4G? 31G? 15% /

tmpfs? ? ? ? ? ? ? ? 939M? ? 0? 939M? 0% /dev/shm

/dev/sda1? ? ? ? ? ? 477M? 57M? 396M? 13% /boot

于是乎,媽媽再也不用擔心我的虛擬機磁盤沒容量了。

參考:https://www.madcoder.cn/vagrant-box-resize.html

https://gist.github.com/christopher-hopper/9755310

5.當VitrualBox啟動的時候出現? 不能為虛擬電腦打開一個任務的時候 請升級VitrualBox軟件以及Vgrant軟件后 就可以用了

http://www.lxweimin.com/p/a2003036f69b

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372

推薦閱讀更多精彩內容

  • 什么是Vagrant簡單來說,Vagrant 就是一個虛擬機的集成管理器。 我們用它可以快速創建虛擬機,可以快速部...
    嗝喯唲閱讀 660評論 0 0
  • 前言 大家對VMware或者VirtualBox一定不會陌生,虛擬化的好處自然深入人心,而現在我們可以通過Vagr...
    王奧OX閱讀 1,117評論 0 4
  • 做小物最有成就感,開始認真看書,布藝人又怎么能少了布藝書簽呢。 紅紅的小狐貍,特別的書簽,有萌到你嗎? 拍了過程圖...
    葉樣悠閱讀 332評論 0 4
  • 1.css的繼承 1-1, p { color: #2f2 } 以上p標簽的設置不僅僅應用于p標簽,還應用于p...
    隨意人生_1b90閱讀 331評論 0 0
  • 大半夜,洶涌奔騰。 我,只想寫所有溫柔的話,給你。 在這里不想要喪,已經夠喪了,不可以再多。 我不想去講,...
    薯梨閱讀 208評論 0 0