vagrant
apt install vagrant
box
官網
可以去官網查找需要的box
vagrant box add generic/ubuntu1804
執行上面的指令可以下載下傳一個box,這裡會有一個重定向的過程,實際上檔案是從提供方的伺服器上下載下傳,而不是vagrant的伺服器上下載下傳。并且這裡不像docker一樣有官方的服務,都是第三方提供。
這個box和docker裡image概念比較像。檔案會下載下傳到預設位置,也是使用類似aufs的檔案管理方式來應對多個執行個體
國内鏡像
以下載下傳ubuntu18.04為例
vagrant box add \
https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box \
--name ubuntu/bionic
instance
建立一個空檔案夾,進去
vagrant init generic/ubuntu1804
如果是使用清華的box則執行下面的
vagrant init ubuntu/bionic
執行上面的指令後會在檔案夾裡建立一個Vagrantfile,這個檔案内容和網站一樣,隻是有更多的參數說明。全文如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "generic/ubuntu1804"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
然後執行下面指令啟動
vagrant up
但是可能會報錯:
Bringing machine 'default' up with 'libvirt' provider...
Error while connecting to libvirt: Error making a connection to libvirt URI qemu:///system?no_verify=1&keyfile=/root/.ssh/id_rsa:
Call to virConnectOpen failed: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
livirt
根據網上的方案執行下面指令安裝一個第三方庫
apt install -y libvirt-daemon-system
virtualbox
另一個方法是使用virtualbox
apt install -y virtualbox
apt install -y virtualbox-dkms
systemctl start virtualbox
vagrant up
啟動成功後控制台會列印一些資訊
通過vagrant ssh進入虛拟機,然後sudo su直接切換root賬号
網絡
通過修改Vagrantfile寫入下面兩行開啟dhcp擷取獨立ip的形式(第二行是本來就由,取消注釋就可以了),更多功能參見官網
Vagrant.configure("2") do |config|
config.vm.network "public_network",
use_dhcp_assigned_default_route: true
end
然後啟動的時候會提示使用哪個網卡,選實體網卡即可。啟動後使用ip a會看到三條記錄,除了lo就是一個nat和一個bridged。
記憶體&CPU
看上去vagrant使用的記憶體機關是M,可以通過打開Vagrantfile來修改記憶體和CPU
config.vm.provider "virtualbox" do |v|
v.memory = 8192
v.cpus = 2
end
主要下面end的注釋要打開。
更全面的配置(例如hostname)可以參考官網說明