vagrant 基礎(chǔ)知識(shí)拾憶

1,介紹Vagrant

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

能不能在windows下也像linux那樣開發(fā)?也許你想到了,用虛擬機(jī)。用虛擬機(jī)裝個(gè)linux系統(tǒng)就好了。裝完linux系統(tǒng)就設(shè)置共享目錄,設(shè)置網(wǎng)絡(luò)端口映射,等等。好像也有那么點(diǎn)繁瑣。

還有,假如我們是一個(gè)團(tuán)隊(duì)進(jìn)行開發(fā),那么每個(gè)人的電腦上都要裝個(gè)虛擬機(jī)+ linux系統(tǒng)+各種運(yùn)行環(huán)境。手動(dòng)設(shè)置麻煩不說,大家的開發(fā)環(huán)境不太一致(可能你裝了apcahe我裝了nginx等),也是頭疼。能不能把各種設(shè)置都自動(dòng)化,并且保持整個(gè)團(tuán)隊(duì)的開發(fā)環(huán)境一致呢?

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

2,下載

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

上面給出的是5.1.10版本的下載鏈接。要下載其他版本請?jiān)L問官網(wǎng)https://www.virtualbox.org/wiki/Downloads

下載Vagrant:

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

上面給出的是1.8.7版本的下載鏈接。要下載其他版本請?jiān)L問官網(wǎng)

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鏡像下載鏈接,要下載其他鏡像請?jiān)L問官網(wǎng)

http://www.vagrantbox.es/

3,安裝

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

安裝完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`.

先新建一個(gè)工作目錄

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

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

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復(fù)制到本目錄E:\VagrantWorkcentos-6.7-x86_64下,執(zhí)行

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

檢查是否導(dǎo)入成功

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

centos6.7 (virtualbox, 0)

4,配置

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

下面列舉出幾個(gè)常用的配置。要用到其他配置請?jiān)L問官網(wǎng)文檔或者百度谷歌一下。

1,端口映射

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

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

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

config.vm.network :private_network, ip: 192.168.1.10

把上面這句代碼前面的#號(hào)去掉即可

3,目錄同步:

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

第一個(gè)是本地目錄 第二個(gè)是vagrant虛擬機(jī)目錄

5,啟動(dòng)

進(jìn)入目錄E:\VagrantWorkcentos-6.7-x86_64后執(zhí)行命令

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

虛擬機(jī)啟動(dòng)之后則可以通過vagrant ssh 聯(lián)入虛擬機(jī)進(jìn)行進(jìn)一步的環(huán)境配置,或者軟件安裝相關(guān)的工作,在Windows系統(tǒng)下,并不能直接通過 vagrant ssh 連到虛擬機(jī),需要使用 Putty,Xshell 等第三方工具進(jìn)行連接。連接地址192.168.1.10,端口22。登錄的帳號(hào)root/vagrant 密碼為vagrant

檢測狀態(tài)

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下面的目錄一一對應(yīng)


打包分發(fā)

當(dāng)你配置好開發(fā)環(huán)境后,退出并關(guān)閉虛擬機(jī)。在終端里對開發(fā)環(huán)境進(jìn)行打包:

vagrant package

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

導(dǎo)出指定的box

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

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

6,導(dǎo)出

在cmd里進(jìn)行工作目錄后,執(zhí)行下面命令

vagrant package

完成后會(huì)在當(dāng)前目錄就會(huì)生成package.box,之后新建虛擬機(jī)則可使用這個(gè)box。如果事先在你的虛擬機(jī)里建立好了各種開發(fā)環(huán)境,那么你直接把這個(gè)box給你的團(tuán)隊(duì)其他成員安裝,這樣就可以省去一臺(tái)臺(tái)電腦部署的時(shí)間,還可以保持開發(fā)環(huán)境一致。很方便有木有。

7,其他命令

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

vagrant up (啟動(dòng)虛擬機(jī))

vagrant halt (關(guān)閉虛擬機(jī)——對應(yīng)就是關(guān)機(jī))

vagrant suspend (暫停虛擬機(jī)——只是暫停,虛擬機(jī)內(nèi)存等信息將以狀態(tài)文件的方式保存在本地,可以執(zhí)行恢復(fù)操作后繼續(xù)使用)

vagrant resume (恢復(fù)虛擬機(jī) —— 與前面的暫停相對應(yīng))

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

vagrant destroy (刪除虛擬機(jī),刪除后在當(dāng)前虛擬機(jī)所做進(jìn)行的除開Vagrantfile中的配置都不會(huì)保留)

8,擴(kuò)容

一直以來都是以vagrant+docker作為開發(fā)環(huán)境,可是久而久之,原Box自帶的8G容量就捉襟見肘了。時(shí)不時(shí)需要手動(dòng)刪除一些東西。

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

因?yàn)橐玫絍BoxManage,所以要把D:\Program Files\Oracle\VirtualBox這個(gè)加入path。

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

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

給vdi格式硬盤文件擴(kuò)容

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

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

vagrant up啟動(dòng)虛擬機(jī),然后vagrant ssh進(jìn)入

利用cfdisk工具創(chuàng)建主分區(qū),謹(jǐn)記選擇格式為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命令給新的分區(qū)創(chuàng)建物理卷

$sudo pvcreate /dev/sda3

Physical volume "/dev/sda3" successfully created

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

$ sudo pvdisplay|grep"VG Name"

VGName ? ? ?VolGroup

將新分區(qū)擴(kuò)展到centos這個(gè)組

$vgextend ? ?VolGroup ? /dev/sda3

Volume group "VolGroup" successfully extended

擴(kuò)展邏輯分區(qū)

$?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下,這一步會(huì)出錯(cuò)

resize2fs1.42.9(28-Dec-2013)

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

Couldn't find valid filesystem superblock.

resize2fs

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

xfs_growfs /dev/mapper/VolGroup-lv_root

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

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

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

于是乎,媽媽再也不用擔(dān)心我的虛擬機(jī)磁盤沒容量了。

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

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

5.當(dāng)VitrualBox啟動(dòng)的時(shí)候出現(xiàn)? 不能為虛擬電腦打開一個(gè)任務(wù)的時(shí)候 請升級(jí)VitrualBox軟件以及Vgrant軟件后 就可以用了

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

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