使用Vagrant+VirtualBox搭建虚拟环境


使用VMware或者VirtualBox安装centos、ubuntu,可以通过下载相关镜像进行进行创建,但是这种方式较为繁琐,而通过vagrant可以更方便地创建centos等虚拟化环境。

搭建学习环境建议使用Vagrant+VirtualBox搭建虚拟环境,一方面是使用和生产环境类似的环境进行学习,其次在清理软件时也更加方便。 准备:

  • 1、安装好VirtualBox
  • 2、前往Vagrant官网下载vagrant

MacOS可以使用brew进行安装

brew install vagrant

等待几分钟即可安装成功:

vagrant常用命令

  • 查看vagrant版本
 vagrant --version

使用vagrant 创建一台centos虚拟机

  • 1、创建Vagrantfile 新建一个目录/Users/o0u0o/vagrant/centos7,并切换至该目录进行以下初始化命令。
vagrant init centos/7

注意:是centos/7

  • 2、 启动 使用 vagrant up 命令启动centos虚拟机
 mac@o0u0odeMacBook-Pro centos7 % vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '2004.01' 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: 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: /Users/o0u0o/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.

3、连接到服务器

vagrant ssh

查看机器状态

vagrant status

停止机器

vagrant halt

删除机器

vagrant destroy

常见问题:

1、virtualbox版本与Vagrant版本不匹配

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

Vagrant has detected that you have a version of VirtualBox installed
that is not supported by this version of Vagrant. Please install one of
the supported versions listed below to use Vagrant:

4.0, 4.1, 4.2, 4.3, 5.0, 5.1, 5.2, 6.0, 6.1

A Vagrant update may also be available that adds support for the version
you specified. Please check www.vagrantup.com/downloads.html to download
the latest version.

解决: 按照提示升级Vagrant为对应版本即可,我这里使用的是brew管理,输入命令brew upgrade vagrant升级即可!


分类:DevOps
标签: Vagrant VirtualBox
文章目录