Skip to content

Vagrant Basic

Vagrant 简介

命令行实用工具来快速管理虚拟机的生命周期。(终于不用手动繁琐的创建测试环境了.)

安装与卸载

安装(Ubuntu 18.04环境下)

其他系统安装参考: https://www.vagrantup.com/docs/installation

前往vagrant官方下载站点,下载与当前系统版本相对应的软件版本,解压缩,然后添加到环境变量中即可。

curl https://releases.hashicorp.com/vagrant/2.2.10/vagrant_2.2.10_linux_amd64.zip -o vagrant_2.2.10_linux_amd64.zip
unzip vagrant_2.2.10_linux_amd64.zip
chmod +x vagrant
mv vagrant /usr/bin
curl https://releases.hashicorp.com/vagrant/2.2.10/vagrant_2.2.10_linux_amd64.zip -o vagrant_2.2.10_linux_amd64.zip
unzip vagrant_2.2.10_linux_amd64.zip
chmod +x vagrant
mv vagrant /usr/bin

检验安装是否成功:

$ vagrant --version
Vagrant 2.2.10
$ vagrant --version
Vagrant 2.2.10

⚠️禁用kvm

First find out the name of the hypervisor:

$ lsmod | grep kvm
kvm_intel             204800  6
kvm                   593920  1 kvm_intel
irqbypass              16384  1 kvm
$ lsmod | grep kvm
kvm_intel             204800  6
kvm                   593920  1 kvm_intel
irqbypass              16384  1 kvm

The one we're interested in is kvm_intel. You might have another.

Blacklist the hypervisor (run the following as root):

$ echo 'blacklist kvm-intel' >> /etc/modprobe.d/blacklist.conf
$ echo 'blacklist kvm-intel' >> /etc/modprobe.d/blacklist.conf

卸载

其他系统参考:https://www.vagrantup.com/docs/installation/uninstallation

rm -rf /opt/vagrant
rm -f /usr/bin/vagrant
rm -rf /opt/vagrant
rm -f /usr/bin/vagrant

快速开始

初始化Vagrant

$ mkdir vagrant_workspace && cd vagrant_workspace
$ vagrant init hashicorp/bionic64
$ mkdir vagrant_workspace && cd vagrant_workspace
$ vagrant init hashicorp/bionic64

初始化完毕之后会在当前目录生成一个名为vagrantfile的清单文件

启动虚拟机:

$ vagrant up
$ vagrant up

使用SSH连入创建完毕的虚拟机:

$ vagrant ssh
$ vagrant ssh

删除(摧毁)虚拟机:

$ vagrant destroy
$ vagrant destroy

摧毁虚拟机不会删除所下载的文件,如果需要删除,则需要运行:

# 查看box
$ vagrant box list
# 添加一个box
$ vagrant box add hashicorp/bionic64
# 删除box
$ vagrant box remove ubuntu/trusty64
# 查看box
$ vagrant box list
# 添加一个box
$ vagrant box add hashicorp/bionic64
# 删除box
$ vagrant box remove ubuntu/trusty64

VagrantfIle

简单示例1:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end

启动:vagrant up

简单示例2:

$ mkdir html
$ echo "vagrant page here." > html/index.html

# 编写脚本
#!/usr/bin/env bash
apt-get update -y
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

# 配置vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 4567
end
$ mkdir html
$ echo "vagrant page here." > html/index.html

# 编写脚本
#!/usr/bin/env bash
apt-get update -y
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

# 配置vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 4567
end

重载配置:vagrant reload --provision

最后打开浏览器访问http://127.0.0.1:4567即可。

配置项详解:

VAGRANTFILE_API_VERSION = "2"		# 可不定义该变量,直接指定
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|		# 其中VAGRANTFILE_API_VERSION可直接使用`2`来代替

    #######################################  使用循环语句快速创建三个虚拟机
    (1..3).each do |i|
      config.vm.define "node-#{i}" do |node|
        node.vm.provision "shell",
          inline: "echo hello from node #{i}"
      end
    end
    ####################################### 

    # ---------------------- config.vim
    config.vm.box = "generic/ubuntu1804"	# 镜像名称
    config.vm.hostname = "vm-demo1.local"		# 指定主机名称

    config.vm.network "forwarded_port", guest: 80, host: 8080, id: "http", protocol: "tcp"		# 设置转发端口,后面的id参数可以省略
    # config.vm.network "public_network", ip: "192.168.33.11", hostname: true		# 指明公网地址,并使用hostname选项,该选项可以将信息写入/etc/hosts,如`vm-demo1 192.168.33.11` 
    # config.vm.network "private_network", ip: "192.168.33.11"		# 指明私网地址
    config.vm.network "public_network", use_dhcp_assigned_default_route: true	# 使用默认dhcp获取IP
    # ----------------------

    config.vm.synced_folder "src/", "/srv/website", disabled: false, owner: "root", group: "root"		# 挂载文件夹, disable选项省略

end
VAGRANTFILE_API_VERSION = "2"		# 可不定义该变量,直接指定
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|		# 其中VAGRANTFILE_API_VERSION可直接使用`2`来代替

    #######################################  使用循环语句快速创建三个虚拟机
    (1..3).each do |i|
      config.vm.define "node-#{i}" do |node|
        node.vm.provision "shell",
          inline: "echo hello from node #{i}"
      end
    end
    ####################################### 

    # ---------------------- config.vim
    config.vm.box = "generic/ubuntu1804"	# 镜像名称
    config.vm.hostname = "vm-demo1.local"		# 指定主机名称

    config.vm.network "forwarded_port", guest: 80, host: 8080, id: "http", protocol: "tcp"		# 设置转发端口,后面的id参数可以省略
    # config.vm.network "public_network", ip: "192.168.33.11", hostname: true		# 指明公网地址,并使用hostname选项,该选项可以将信息写入/etc/hosts,如`vm-demo1 192.168.33.11` 
    # config.vm.network "private_network", ip: "192.168.33.11"		# 指明私网地址
    config.vm.network "public_network", use_dhcp_assigned_default_route: true	# 使用默认dhcp获取IP
    # ----------------------

    config.vm.synced_folder "src/", "/srv/website", disabled: false, owner: "root", group: "root"		# 挂载文件夹, disable选项省略

end

多主机(Multi-Machine)

Vagrant.configure("2") do |config|
  config.vm.network "private_network", type: "dhcp"		# 互联主机网路
  config.vm.provision "shell", inline: "echo Hello"

  config.vm.define "web" do |web|
    web.vm.box = "apache"
  end

  config.vm.define "db" do |db|
    db.vm.box = "mysql"
  end
end
Vagrant.configure("2") do |config|
  config.vm.network "private_network", type: "dhcp"		# 互联主机网路
  config.vm.provision "shell", inline: "echo Hello"

  config.vm.define "web" do |web|
    web.vm.box = "apache"
  end

  config.vm.define "db" do |db|
    db.vm.box = "mysql"
  end
end

管理多主机:

# 启动所有
$ vagrant up
# 启动指定主机
$ vagrant up web
# 启动所有
$ vagrant up
# 启动指定主机
$ vagrant up web

Plugins

vagrant share

预先条件:安装ngrok,参考 https://ngrok.com/download

安装插件:

$ vagrant plugin install vagrant-share
$ vagrant plugin install vagrant-share

共享环境:

$ vagrant share
==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:
    default: Local HTTP port: 4567
    default: Local HTTPS port: disabled
    default: Port: 2222
    default: Port: 4567
==> default: Creating Vagrant Share session...
==> default: HTTP URL: http://c726c8a40a7b.ngrok.io
==> default: 
$ vagrant share
==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:
    default: Local HTTP port: 4567
    default: Local HTTPS port: disabled
    default: Port: 2222
    default: Port: 4567
==> default: Creating Vagrant Share session...
==> default: HTTP URL: http://c726c8a40a7b.ngrok.io
==> default: 

参考链接