安装vagrant

vagrant_2.2.10

安装virtualbox

virtualbox-6.1.14

安装vagrant插件

vagrant-vbguest插件
    vagrant plugin install vagrant-vbguest
vagrant-disksize插件   
    vagrant plugin install vagrant-disksize
vagrant-share插件  
    vagrant plugin install vagrant-share 
    
  • 查看vagrant插件
vagrant plugin list

vagrant-disksize (0.1.3, global)
vagrant-share (1.1.11, global)
vagrant-vbguest (0.25.0, global)

vagrant常用命令

#列出本地的box文件
vagrant box list
#在空文件夹初始化虚拟机
vagrant init NAME [URL]
#在初始化完的文件夹内启动虚拟机
vagrant up
#重启虚拟机
vagrant reload
#以默认账号vagrant连接虚拟机
vagrant ssh
#关闭虚拟机
vagrant halt
#挂起启动的虚拟机
vagrant suspend
#查找虚拟机的运行状态
vagrant status
#销毁当前虚拟机
vagrant destory

常用的镜像

Vagrant boxes search

Ubuntu 18.04

https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box

CentOS 7

https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box

使用举例

#启动一个 Ubuntu 18.04 的虚拟机
vagrant init ubuntu-bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box

#启动一个 CentOS 7 的虚拟机
vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box

Vagrantfile常用配置说明

  • 网络配置
    Private network(私有网络)
    优点:可以使用一个固定IP连接虚拟机
    缺点:其他团队成员不能访问你的虚拟机
# 固定IP
config.vm.network "private_network", ip: "192.168.50.4" 

#设置动态IP
#config.vm.network "private_network", type: "dhcp"
  • 端口转发
  • 虚拟机参数配置
  • 共享目录

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起始配置块
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.
  #定义box名称
  config.vm.box = "ubuntu18"
  #定义虚拟机名字
  config.vm.hostname = "rtm"
  #设置磁盘容量,需要安装vagrant-disksize插件
  config.disksize.size = "80GB"

  #---------------------SSH相关配置--------------------------------------------------------
  # config.ssh.username = "vagrant"   #设置默认ssh用户(默认用户是vagrant)
  # config.ssh.password = "vagrant"   #设置默认ssh密码(默认密码是vagrant)
  # config.ssh.port = 22              #设置ssh端口

  # 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"
  # ---------------------基于virtualbox的一些配置--------------------------------------------
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "8192"
    # 在virtualbox中显示的名字
    vb.name = "rtm"
    # 指定虚拟机内核数
    vb.cpus = 2
  end

  # 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
  # Ansible, Chef, Docker, Puppet and Salt 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!
Vagrant使用国内镜像安装插件和box镜像


alits
7 声望0 粉丝

引用和评论

0 条评论