首页 公告 项目 RSS

vagrant安装和使用

August 22, 2017 本文有 2388 个字 需要花费 5 分钟阅读

简介

在一次偶然的机会我碰到了vagrant,Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境,总之简单的说vagrant就是一款管理虚拟机的工具

官网

https://www.vagrantup.com/
一些文档还有镜像可以在官网里找到

安装

安装很简单,只要下载对应的安装包,安装起来就好了,比如我用的是fedora那就下载rpm包,上面写着centos的就是了
下载好后安装
sudo dnf install ./vagrant_1.9.7_x86_64.rpm
接着安装virtualbox
virtualbox的安装和vagrant一样,先下载安装包之后安装就好了

使用

首先添加一个镜像
vagrant box add ubuntu/xenial64
我添加的是ubuntu16.04 64位的虚拟机镜像,具体你想下载什么镜像你可以在
https://app.vagrantup.com/boxes/search?_ga=2.204284501.492827600.1503221431-11326269.1503221431
这个网站里找
出现下面的提示说明镜像添加成功

➜  ~ vagrant box add ubuntu/xenial64
==> box: Loading metadata for box 'ubuntu/xenial64'
    box: URL: https://vagrantcloud.com/ubuntu/xenial64
==> box: Adding box 'ubuntu/xenial64' (v20170815.1.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20170815.1.0/providers/virtualbox.box
==> box: Box download is resuming from prior download progress
==> box: Successfully added box 'ubuntu/xenial64' (v20170815.1.0) for 'virtualbox'!

之后你可以确认一下镜像是否添加成功

➜  ~ vagrant box list  
ubuntu/xenial64 (virtualbox, 20170815.1.0)

接着创建一个目录来存放Vagrantfile
mkdir bboysoul
cd bboysoul/
初始化,此操作会在当前目录下生成一个Vagrantfile

➜  bboysoul vagrant init ubuntu/xenial64
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.
➜  bboysoul ls
Vagrantfile
➜  bboysoul 

接着启动一个虚拟机

➜  bboysoul vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/xenial64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/xenial64' is up to date...
==> default: Setting the name of the VM: bboysoul_default_1503234312551_82817
==> 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: Running 'pre-boot' VM customizations...
==> 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: ubuntu
    default: SSH auth method: password
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    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: 5.0.40
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => /home/bboysoul/bboysoul

启动完成之后你可以打开virtualbox,你可以看到一个已经启动完成的虚拟机
接着ssh进去

➜  bboysoul vagrant ssh
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-92-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


_____________________________________________________________________
WARNING! Your environment specifies an invalid locale.
 The unknown environment variables are:
   LC_CTYPE=zh_CN.UTF-8 LC_ALL=
 This can affect your user experience significantly, including the
 ability to manage packages. You may install the locales by running:

   sudo apt-get install language-pack-zh
     or
   sudo locale-gen zh_CN.UTF-8

To see all available language packs, run:
   apt-cache search "^language-pack-[a-z][a-z]$"
To disable this message for all users, run:
   sudo touch /var/lib/cloud/instance/locale-check.skip
_____________________________________________________________________

ubuntu@ubuntu-xenial:~$ 

从启动虚拟机的时候的输出信息我们可以看到还挂载了一个共享目录

==> default: Mounting shared folders...
    default: /vagrant => /home/bboysoul/bboysoul

在虚拟机里的路径是/vagrant,实体机的路径是/home/bboysoul/bboysoul
也就是Vagrantfile的所在路径

ubuntu@ubuntu-xenial:~$ ls /vagrant/
Vagrantfile  ubuntu-xenial-16.04-cloudimg-console.log
ubuntu@ubuntu-xenial:~$ 

接着你可以退出来重启虚拟机
vagrant reload

➜  bboysoul vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'ubuntu/xenial64' is up to date...
==> 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: Running 'pre-boot' VM customizations...
==> 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: ubuntu
    default: SSH auth method: password
==> 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: 5.0.40
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => /home/bboysoul/bboysoul
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

你可以使用
vagrant halt
来关闭虚拟机

➜  bboysoul vagrant halt  
==> default: Attempting graceful shutdown of VM...

vagrant up
来启动虚拟机

➜  bboysoul vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/xenial64' is up to date...
==> 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: Running 'pre-boot' VM customizations...
==> 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: ubuntu
    default: SSH auth method: password
==> 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: 5.0.40
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => /home/bboysoul/bboysoul
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

一些常用的命令

可以从vagrant的help来看

➜  bboysoul vagrant list-commands
Below is a listing of all available Vagrant commands and a brief
description of what they do.

box             manages boxes: installation, removal, etc.
cap             checks and executes capability
connect         connect to a remotely shared Vagrant environment
destroy         stops and deletes all traces of the vagrant machine
docker-exec     attach to an already-running docker container
docker-logs     outputs the logs from the Docker container
docker-run      run a one-off command in the context of a container
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 Vagrantfile
list-commands   outputs all available Vagrant subcommands, even non-primary ones
login           log in to HashiCorp's Vagrant Cloud
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
provider        show provider for this environment
provision       provisions the vagrant machine
push            deploys code in this environment to a configured destination
rdp             connects to machine via RDP
reload          restarts vagrant machine, loads new Vagrantfile configuration
resume          resume a suspended vagrant machine
rsync           syncs rsync synced folders to remote machine
rsync-auto      syncs rsync synced folders automatically when files change
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 machine
status          outputs status of the vagrant machine
suspend         suspends the machine
up              starts and provisions the vagrant environment
validate        validates the Vagrantfile
version         prints current and latest Vagrant version

现在列举一些常用的

  • vagrant box 用来管理box的
  • vagrant box add 添加一个box
  • vagrant box list 列出可用的box
  • vagrant box remove 删除一个box
  • vagrant box
  • vagrant halt 停止一个虚拟机
  • vagrant init 初始化一个虚拟机
  • vagrant package 打包一个虚拟机
  • vagrant reload 重启一个虚拟机
  • vagrant ssh ssh进虚拟机
  • vagrant up 启动一个虚拟机
  • vagrant status 查看虚拟机状态

打包分发虚拟机

当你把所有的环境都配置好之后,下一步要做的就是打包分发了
一个命令就可以搞定你的打包

➜  bboysoul vagrant package
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /home/bboysoul/bboysoul/package.box
➜  bboysoul ls
package.box  ubuntu-xenial-16.04-cloudimg-console.log  Vagrantfile

如显示所示vagrant会把你的虚拟机先关闭再进行打包,之后会在当前目录下生成一个package.box的文件,之后你把这个文件通过网盘或者其他文件共享的方式发给要部署的机器直接部署就好了

高级使用

你会看到我们一直没有说vagrant的配置文件Vagrantfile,这个文件主要配置了虚拟机的配置、SSH配置、Vagrant的一些基础配置。
下面我说一下常见的配置

  • box的名称配置
    config.vm.box = "ubuntu12.04"
  • 同步目录配置
    config.vm.synced_folder "本机地址", "虚拟机文件地址"
  • 主机hostname设置
    config.vm.hostname = "for_work"
  • 端口转发配置
    config.vm.network "forwarded_port", guest: 80, host: 8080

其他的配置文件大家可以参考下面这个网站https://www.vagrantup.com/docs/vagrantfile/

最后说两句

说真的我是真的不知道世界上还有一个系统叫windows,听说用windows的人技术水平都很高,所以我想见识一下。