关于vagrant:vagrant使用

46次阅读

共计 4272 个字符,预计需要花费 11 分钟才能阅读完成。

装置 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 镜像

正文完
 0