Vagrant安装步骤

Vagrant安装步骤

  1. 下载添加box镜像

    vagrant box add base 远端的box地址或者本地的box文件名

  2. 建立box镜像关联

    vagrant box add centos72 vagrant-centos-7.2.box

    输出结果如下

    f:\vagrant\centos7.2>vagrant box add centos72 vagrant-centos-7.2.box
    ==> box: Box file was not detected as metadata. Adding it directly...
    ==> box: Adding box 'centos72' (v0) for provider:
        box: Unpacking necessary files from: file://f:/vagrant/centos7.2/vagrant-centos-7.2.box
        box:
    The box you're attempting to add already exists. Remove it before
    adding it again or add it with the `--force` flag.
    
    Name: centos72
    Provider: virtualbox
    Version: 0
    
  3. 初始化

    vagrant init centos72

    输出结果如下

    f:\vagrant\centos7.2>vagrant init centos72
    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.
    
    
  4. 按需求配置Vagrantfile (生成3台虚拟机)

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    Vagrant.configure("2") do |config|
      config.vm.define "vgrant01" do |vb|
        config.vm.provider "virtualbox" do |v|
          v.memory = 1024
          v.cpus = 1
        end
        vb.vm.host_name = "vagrant01"
        vb.vm.network :public_network, ip: "10.0.0.15"
        vb.vm.box = "centos72"
      end
    
    
      config.vm.define "vgrant02" do |vb|
        config.vm.provider "virtualbox" do |v|
          v.memory = 1024
          v.cpus = 1
        end
        vb.vm.host_name = "vagrant02"
        vb.vm.network :public_network, ip: "10.0.0.16"
        vb.vm.box = "centos72"
      end
    
    
      config.vm.define "vgrant03" do |vb|
        config.vm.provider "virtualbox" do |v|
          v.memory = 1024
          v.cpus = 1
        end
        vb.vm.host_name = "vagrant03"
        vb.vm.network :public_network, ip: "10.0.0.17"
        vb.vm.box = "centos72"
      end
    end
    
  5. 启动虚拟机

    vagrant up

vagrant常用命令

  1. 显示当前已经添加box列表

    vagrant box list

  2. 删除相应box列表

    vagrant box remove

  3. 停止当前正在运行的虚拟机并销毁所有创建的资源

    vagrant destory

  4. 关闭虚拟机

    vagrant halt

  5. 将当前运行的虚拟机环境打包

    vagrant package

  6. 重启虚拟机,主要用于重新载入配置文件

    vagrant reload

  7. 输出用于连接ssh的一些信息

    vagrant ssh-config

  8. 挂起当前虚拟机

    vagrant suspend

  9. 恢复被挂起状态

    vagrant resume

  10. 获取当前虚拟机状态

    vagrant status

posted @ 2019-08-31 15:18  大胡子哥dhzg  Views(236)  Comments(0)    收藏  举报