专栏首页bboysoulvagrant安装和使用

vagrant安装和使用

简介

在一次偶然的机会我碰到了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的人技术水平都很高,所以我想见识一下。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 自己搭建ngrok服务把树莓派放到公网上去

    为什么自己搭建ngrok服务呢?原因很简单,官方如果绑定域名要收费啊,自己有服务器,所以就自己搭建一个好了,要求的服务器配置又不高,说实在的树莓派IO真的惨不忍...

    bboysoul
  • 使用ab命令对网站进行压力测试

    这是apache自带的一个网站压力测试工具,是ApacheBench的缩写,我们可以使用这个工具来对我们的网站进行压力测试

    bboysoul
  • 磁盘分析工具Ncdu的简单使用和体验

    因为我使用的是120g的固态硬盘,所以我的本地就不能存很多东西,经常要删除很多东西,所以看每个目录的大小就很重要,下面推荐一个磁盘分析工具Ncdu,这个工具是用...

    bboysoul
  • Vagrant实战

    平时工作一直在Linux上做开发,Docker用的比较多,最近要用Vagrant搭建Linux开发环境,所以用了一下,整理一些资料。

    kongxx
  • Python十分钟制作属于你自己的个性logo

    词云的使用相信大家已经不陌生了,使用很简单,直接调用wordcloud包就可以了。它的主要功能是根据文本词汇和词汇频率生成图片,从中可以直观的看出各个词汇所占比...

    用户2769421
  • Mac下vagrant从安装到体验

    Mac下使用虚拟机时,通过vagrant+virtualbox的组合是个不错的方案,简便快捷;

    程序员欣宸
  • vagrant学习笔记 - 入门

    官方下载地址:https://www.vagrantup.com/downloads.html

    pollyduan
  • 暗网做生意不容易:黑市开搞漏洞奖励计划,最高奖励10比特币

    很多人不知道,安全是黑产都需要重视的话题。就在上周,大型暗网市场Hansa借鉴了许多公司的普遍做法,发布了漏洞赏金计划,奖励金额最高可达10比特币,约合1万美元...

    FB客服
  • CoreDNS介绍

    开始之前先吐槽一下busybox中的nslookup命令。这个命令应该是实现的不是很完全,导致我在测试DNS的成功,得到了错误的信息。先来看一下

    大江小浪
  • Apache Zeppelin 中 JDBC通用 解释器

    概述 JDBC解释器允许您无缝地创建到任何数据源的JDBC连接。 在运行每个语句后,将立即应用插入,更新和升级。 到目前为止,已经通过以下测试: ...

    片刻

扫码关注云+社区

领取腾讯云代金券