Vagrant实战
Posted kongxx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vagrant实战相关的知识,希望对你有一定的参考价值。
介绍
平时工作一直在Linux上做开发,Docker用的比较多,最近要用Vagrant搭建Linux开发环境,所以用了一下,整理一些资料。
Vagrant其实是一个可以方便设置使用虚拟机的工具,底层支持VirtualBox和VMWare,我的Windows机器上已经安装了VirtualBox,我就基于VirtualBox说说怎么使用吧。
安装
首先从下面地址下载vagrant安装包,直接安装可执行文件就可以了。
https://www.vagrantup.com
虚拟机管理
通常情况下,我们可以通过下面两个命令来创建并启动虚拟机,但是由于下载实在太慢,所以不建议这么玩。
vagrant init centos/7
vagrant up
我们可以先通过 https://app.vagrantup.com/boxes/search 地址,直接下载虚拟机的vbox文件,然后基于这个vbox文件来创建虚拟机。
比如,这里我使用下面地址来下载CentOS7的vbox文件
https://app.vagrantup.com/centos/boxes/7
文件名为:CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
创建centos7目录,然后把CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box文件复制到目录下。
添加虚拟机
D:\\vagrant\\centos7> vagrant box add centos7 CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
box: Unpacking necessary files from: file://D:/vagrant/centos7/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
box:
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!
初始化
D:\\vagrant\\centos7> vagrant init centos7
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.
经过这一步后,会在当前目录下产生Vagrantfile文件,这个文件类似于Docker里的Dockerfile文件。
启动虚拟机
D:\\vagrant\\centos7> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1576066403778_3997
==> 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:
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: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/d/vagrant/centos7/ => /vagrant
查看虚拟机
D:\\vagrant\\centos7>vagrant box list
centos7 (virtualbox, 0)
访问虚拟机
虚拟机启动后,可以通过两种方式登录到虚拟机里
第一种方式是通过vagrant ssh命令方式
D:\\vagrant\\centos7>vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
第二种方式是通过ssh 127.0.0.1 2222命令方式。在使用vagrant up启动虚拟机的过程种,日志里会打印下面信息
...
==> 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
...
这是告诉我们vagrant做了一个端口转发,把容器的22端口映射成当前物理机的2222端口,用户是vagrant,使用private key认证。其中private key文件默认位置在当前虚拟机目录下的.vagrant\\machines\\default\\virtualbox\\private_key文件。此时我们就可以通过xshell来登录127.0.0.1的2222端口了。
挂起虚拟机
D:\\vagrant\\centos7>vagrant suspend
==> default: Saving VM state and suspending execution...
恢复虚拟机
D:\\vagrant\\centos7>vagrant resume
==> default: Resuming suspended VM...
==> 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: Machine booted and ready!
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
停止虚拟机
D:\\vagrant\\centos7>vagrant halt
==> default: Attempting graceful shutdown of VM...
启动虚拟机
D:\\vagrant\\centos7>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/d/vagrant/centos7/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
销毁虚拟机
停止当前运行的虚拟机并销毁所有创建的资源
D:\\vagrant\\centos7> vagrant destroy
删除虚拟机
D:\\vagrant> vagrant box remove centos7
以上是关于Vagrant实战的主要内容,如果未能解决你的问题,请参考以下文章
实战篇:一行命令安装Linux系统,超详细的 Vagrant 上手指南
实战篇:一行命令安装Linux系统,超详细的 Vagrant 上手指南