vagrant安装及配置文件

柳仲卿
2023-12-01

vagrant用于创建和部署虚拟化开发环境。以下是在ubuntu系统上进行操作

1.vagrant 安装

   apt-get install vagrant ----安装agrant

   vagrant box add   puppetlabs/ubuntu-14.04-64-nocm ----添加vagrant box

   apt-get install virtualbox-5.1 ----安装virtualbox,vagrant是通过virtual/vm进行虚拟机创建的

安装遇到的问题:

  (1)apt-get intall vagrant安装的版本比较老

  解决办法:从官网上下载最新版本的agrant进行安装,下载地址:https://www.vagrantup.com/downloads.html

  (2)vagrant box add命令会主动从网络上下载需要的box,但是会比较慢,而且时间长了就中断,需要重新下

 解决办法:找到对应的vagrant box文件,下载到本地安装。下载地址:https://vagrantcloud.com/puppetlabs/boxes/ubuntu-14.04-64-nocm/versions/1.0.3/providers/virtualbox.box


2.vagrant配置文件

以我本地的配置文件为例,通过vagrant同时建立三台虚拟机

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
    (1..3).each do |i|
        config.vm.define "vpp#{i}" do |node|
          # Pick the right distro and bootstrap, default is ubuntu1404

          node.vm.box = "puppetlabs/ubuntu-14.04-64-nocm"
          #node.vm.hostname="node#{i}"
          node.vm.box_check_update = false


          homedir = File.expand_path("~/")
          Dir["#{homedir}/.gnupg/**/*"].each do |fname|
            if File.file?(fname)
              destname = fname.sub(Regexp.escape("#{homedir}/"),'')
              node.vm.provision "file", source: fname, destination: destname
            end
          end

          # Copy in the .gitconfig if it exists
          if File.file?(File.expand_path("~/.gitconfig"))
            node.vm.provision  "file", source: "~/.gitconfig", destination: ".gitconfig"
          end

          # vagrant-cachier caches apt/yum etc to speed subsequent
          # vagrant up
          # to enable, run
          # vagrant plugin install vagrant-cachier
          #
          if Vagrant.has_plugin?("vagrant-cachier")
            node.cache.scope = :box
          end

          # Define some physical ports for your VMs to be used by DPDK
          nics = (ENV['VPP_VAGRANT_NICS'] || "4").to_i(10)
          for i in 1..nics
            node.vm.network "private_network", type: "dhcp"
          end

          # use http proxy if avaiable
          if ENV['http_proxy'] && Vagrant.has_plugin?("vagrant-proxyconf")
           node.proxy.http     = ENV['http_proxy']
           node.proxy.https    = ENV['https_proxy']
           node.proxy.no_proxy = "localhost,127.0.0.1"
          end

          vmcpu=(ENV['VPP_VAGRANT_VMCPU'] || 2)
          vmram=(ENV['VPP_VAGRANT_VMRAM'] || 4096)

          node.ssh.forward_agent = true

          node.vm.synced_folder "../../", "/vpp", disabled: false

          node.vm.provider "virtualbox" do |vb|
              vb.customize ["modifyvm", :id, "--ioapic", "on"]
              vb.memory = "#{vmram}"
              vb.cpus = "#{vmcpu}"

              #support for the SSE4.x instruction is required in some versions of VB.
              vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.1", "1"]
              vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.2", "1"]
          end
        end  
    end
end

3.vagrant常用命令

  vagrant up ----创建并启动虚拟机

  vagrant status ----查看虚拟机状态

  vagrant ssh ----登录虚拟机

  vagrant destroy ----删除虚拟机

  vagrant reload ----修改vagrantfile后重新加载

 类似资料: