admin管理员组文章数量:1548321
Vagrant 介绍
Vagrant是一个虚拟机box管理工具,本身并不能支持虚拟机的运行,需要依赖VirtualBox或VMware、AWS这些虚拟机运行实体软件。
另人疑惑的是,有了VirtualBox,可以自由导入导出镜像,为什么还需要Vagrant来管理虚拟机呢?先来年看看直接玩VirtualBox是怎么个流程
先创建个新的虚拟机->安装下载好的ISO系统->然后,开始各种软件安装,开始各种配置,开始各种配置的天,麻烦死了。所幸,VirtualBox是可以导入导出的,环境配置好后,可以导出来分发给别的团队成员使用。但是,问题来了,配置环境本身也有可能会经常变,需要更新,然后,更新一次后,之前所有正在使用的旧的配置系统的,全都又要重新导入一次。
使用Vagrant又是怎么个玩法呢?
首先,Vagrant本身拥有一个开放的仓库,这个仓库放有许多预先配置好各种应用的系统环境镜像
,供开发人员直接用命令下载。有点像ubuntu的包管理apt源
,CentOS的yum源
,PHP的依赖管理 composer源
,npm源
,Maven源
,所以使用起来就是方便,不需要自已到处去找VirtualBox镜像源。
其次,Vagrant可以直接启动一个临时虚拟机,并通过一个配置文件Vagrantfile中的设置,配置系统环境,安装系统需要的依赖软件,配置与宿主机的端口映射及目录映射,这就非常棒,非常爽了。我们只需要维护Vagrant的配置文件,这个配置文件包含了所有虚拟机需要运行的信息。那么,这个系统配置环境也可放到版本管理平台上进行版本管理了。
当环境需要更新,只需要更新这个配置文件,然后用Vagrant重新再启动一个虚拟机即可。
VirtualBox的玩法,不再是创建个新虚拟机就一直用到死,而是通过Vagrant频繁的删除创建临时虚拟机。
而VirtualBox及初始镜像系统只是做一件事,就是运行提供环境,而Vagrant则做系统环境的配置信息管理者。通过与宿主目录映射,可实现数据及运行程序或配置文件与虚拟机彻底分离。
Win下 Vagrant安装
1)安装VirtualBox
下载地址 https://www.virtualbox/wiki/Downloads
2)安装 Vagrant
下载地址 https://www.vagrantup/downloads.html
傻瓜式安装,安装完后,测试一下是否安装成功
$ vagrant -v
Vagrant 2.2.5
3) 添加/安装一个Vagrant Box (以laravel/homestead为例)
命令自动添加Box
vagrant box add laravel/homestead
需经过漫长的等待。 需要手动添加则如下方法
1) 手动下载laravel/homestead
- 手动添加vagrant box
vagrant box add laravel/homestead D:/Downloads/virtualbox.box
查看一下当前所有的Vagrant Box
$vagrant box list
laravel/homestead (virtualbox, 8.0.1)
4) 初始化虚拟机 laravel/homestead
进入一个空的文件夹,然后$ vagrant ini [box-name]
,如下
$ vagrant ini laravel/homestead
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` for more information on using Vagrant.
5) 启动虚拟机
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'laravel/homestead'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'laravel/homestead' version '8.0.1' is up to date...
==> default: Setting the name of the VM: laravel_default_1565837259648_23128
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
https://www.virtualbox/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default: https://www.vagrantup/docs/virtualbox/configuration.html#default-nic-type
==> default: https://www.vagrantup/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
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: Connection aborted. Retrying...
default: Warning: Connection reset. 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 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: 6.0.0 r127566
default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
default: /vagrant => D:/laravel
6) 查看虚拟机状态
$ 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`.
7) ssh登录虚拟机
vagrant ssh
** 注意上面的这些操作,都需要在第一步ini的文件夹下进行,否则无进行 **
也可以指定某个虚拟机的id进行操作,怎么知道id呢。可以查看全局的status
$ vagrant global-status
id name provider state directory
------------------------------------------------------------------------
62f16e3 default virtualbox running D:/laravel
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
然后就可以在任意目录下指定id进行对虚拟机的操作如:
- vagrant status 62f16e3 #查看状态
- vagrant ssh 62f16e3 #ssh登录
- vagrant suspend #挂起启动的虚拟机
- vagrant reload 62f16e3 #重启虚拟机
- vagrant halt 62f16e3 #关闭虚拟机
- vagrant destroy 62f16e3 #销毁当前虚拟机
8) 实现端口地址映射
- 第一种方法:直接在VirtualBox软件中,找到那个正常运行虚拟机,选择设置> 网络 > 高级 > 端口转发
主机端口: 宿主机的端口号
子系统端口: 虚拟主机的端口号
设置好后,当在访问主机的端口号时,会转发到子系统的端口号。
注意:端口如果在1024以下的转发,会出问题(如,不能使用443转到子系统作为https转发)
设置好后,执行vagrant up [id]
,注意,不是vagrant reload
reload会重置所有的设置,所以这种方法有bug
- 第二种方法:通过vagrantfile文件进行配置,永久有效
config.vmwork :forwarded_port, guest: 80, host: 8881
config.vmwork :forwarded_port, guest: 82, host: 8882
重启虚拟机vagrant reload [id]
就可得到宿主机的8881 -> 虚拟机的80 ,8882 -> 82
9) 打包分发
当里面部署了专属自己的环境后,这时可以打包虚拟机。需要关闭虚拟机后方可打包$ vagrant halt [id]
$vagrant package [id|name] [--output new_box_name] [--vagrantfile Vagrantfile]
默认名为package.box,使用--ouput
参数 可指定输出名
Vagrant常用命令如下
命令 | 作用 |
---|---|
vagrant box add | 添加box的操作 |
vagrant init | 初始化box的操作,会生成vagrant的配置文件Vagrantfile |
vagrant up | 启动本地环境 |
vagrant ssh | 通过ssh登录本地环境所在虚拟机 |
vagrant halt | 关闭本地环境 |
vagrant suspend | 暂停本地环境 |
vagrant resume | 恢复本地环境 |
vagrant reload | 修改了Vagrantfile后,使之生效(相当于先 halt,再 up) |
vagrant destroy | 彻底移除本地环境 |
vagrant box list | 显示当前已经添加的box列表 |
vagrant box remove | 删除相应的box |
vagrant package | 打包命令,可以把当前的运行的虚拟机环境进行打包 |
vagrant plugin | 用于安装卸载插件 |
vagrant status | 获取当前虚拟机的状态 |
vagrant global-status | 显示当前用户Vagrant的所有环境状态 |
版权声明:本文标题:Vagrant入门笔记 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1727200671a1102080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论