基于vagrant+VirtualBox快速搭建虚拟机

下载 VirtualBox 和Vagrant ,并安装

在这里插入图片描述

为什么我们要选择Vagrant呢?因为它有跨平台、可移动、自动化部署无需人工参与等优点。

  1. VirtualBox 安装
    VirtualBox 是一款开源的虚拟机软件,和VMWare是同类型的软件,用于在当前的电脑上构建一台虚拟机,在这台虚拟机上可以安装系统和软件,与真实的电脑一般无二。
    官方网站下载:https://www.virtualbox.org/wiki/Downloads 下载适合你平台的 VirtualBox 最新版本,直接根据向导进行操作即可完成安装。

  2. Vagrant 安装
    Vagrant用于创建和部署虚拟化开发环境。它使用Oracle的开源VirtualBox虚拟化系统.。
    官方网站下载:https://www.vagrantup.com/downloads.html,直接根据向导进行操作即可完成安装,这个安装完毕后会进行重启电脑,在重启电脑后可以打开cmd命令行窗口vagrant -v进行验证看是否已经可以使用了。

注意:下载的时候,virtualbox和vagrant的版本要搭配,建议都下载最新版的。还有就是要根据自己的操作系统版本进行选择32位或者64位下载。在windows系统中,可能还需要配置环境变量以及一定要开启VT-x/AMD-V硬件加速。

vagrant环境安装

下载box,本地安装
  1. 在线查找需要的box,官方网址:https://app.vagrantup.com/boxes/search
    在这里插入图片描述
  2. 下载box镜像,将其添加box到本地vagrant环境
vagrant box add {title}{url}
vagrant box add centos7 http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1902_01.VirtualBox.box --insecure

注解
{title}可以自行设置,我这里使用的是 centos7
{url}是下载到本地box路径。也可以先把box先下载到本地在进行添加(这时就是本地路径了)
我的路径是:http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1902_01.VirtualBox.box
–insecure:不验证SSL证书

在线安装(安装比较慢)

在空文件夹初始化虚拟机(这时会生成Vagrantfile文件)

vagrant init centos/7

vagrant 命令

描述命令描述命令
在初始化完的文件夹内启动虚拟机vagrant up查找虚拟机的运行状态vagrant status
ssh登录启动的虚拟机vagrant ssh挂起启动的虚拟机vagrant suspend
唤醒虚拟机vagrant resume重启 虚拟机vagrant reload
关闭虚拟机vagrant halt删除当前虚拟机vagrant destroy
在终端里对开发环境进行打包vagrant package修改文件重启(相当于先 halt,再 up)vagrant reload

box管理命令

描述命令描述命令
查看本地box列表vagrant box list添加box到列表vagrant box add name url
从box列表移除vagrant box remove name输出用于ssh连接的一些信息vagrant ssh-config

举例

  1. 初始化
$ vagrant init centos/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.
  1. 启动(在线拉取box比较慢)
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: A newer version of the box 'centos/7' for provider 'virtualbox' is
==> default: available! You currently have version '1902.01'. The latest is version
==> default: '2004.01'. Run `vagrant box update` to update.
==> default: Setting the name of the VM: dome1_default_1589966685326_12869
==> 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+VirtualBox/dome/dome1/ => /vagrant
  1. 查看服务器信息
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/vagrant+VirtualBox/dome/dome1/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  1. 连接成功
$ vagrant ssh
[vagrant@bogon ~]$

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:腾讯云+社区
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2ngqief0p3aco

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值