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软件后 就可以用了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • 什么是Vagrant简单来说,Vagrant 就是一个虚拟机的集成管理器。 我们用它可以快速创建虚拟机,可以快速部...
    嗝喯唲阅读 616评论 0 0
  • 前言 大家对VMware或者VirtualBox一定不会陌生,虚拟化的好处自然深入人心,而现在我们可以通过Vagr...
    王奥OX阅读 1,078评论 0 4
  • 做小物最有成就感,开始认真看书,布艺人又怎么能少了布艺书签呢。 红红的小狐狸,特别的书签,有萌到你吗? 拍了过程图...
    叶样悠阅读 315评论 0 4
  • 1.css的继承 1-1, p { color: #2f2 } 以上p标签的设置不仅仅应用于p标签,还应用于p...
    随意人生_1b90阅读 313评论 0 0
  • 大半夜,汹涌奔腾。 我,只想写所有温柔的话,给你。 在这里不想要丧,已经够丧了,不可以再多。 我不想去讲,...
    薯梨阅读 186评论 0 0