devops之vagrant

vagrant 简介

vagrant - 管理VM, 可用于隔离性更好环境的搭建 (VM vs Docker)

概念

Vagrant Box

(https://app.vagrantup.com/boxes/search)

Boxes are globally stored for the current user. Each project uses a box as an initial image to clone from, and never modifies the actual base image.

Install Box

ubuntu 18.04

vagrant box add hashicorp/bionic64   

This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) virtualbox
3) vmware_desktop


Mac Storage:

~/.vagrant.d/boxes


示例

  1. 创建Vagrantfile

  2. Vagrantfile 指定box,以及配置, 一个最简单的vagrant file

    Vagrant.configure("2") do |config|

    # box settings
    config.vm.box = "hashicorp/bionic64"
    config.vm.box_version = "1.0.282"

    # Provider Settings
    config.vm.provider "virtualbox" do |vb|
    end
    end

    注意:

By default, Vagrant shares your project directory (the one containing the Vagrantfile) to the /vagrant directory in your guest machine.

You can configure Vagrant to run this shell script when setting up the machine,

config.vm.provision :shell, path: "bootstrap.sh"

常用命令

  1. switch to the folder contains Vagrantfile and run vagrant up for vm provision

  2. run vagrant ssh to ssh to vm, run git --version to verify whether the git is automatically installed on the vm

  3. You can use vagrant status to check vm status, vagrant halt to poweroff vm and vagrant destory to delete the vm


参考

Vagrant Get Started