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
把上面这句代码前面的#号去掉即可
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软件后 就可以用了