Spring Cloud Alibaba-VirtualBox&Vagrant(二十五)

625 阅读3分钟

安装

  • 安装VirtualBox(官方文档)
  • 安装Vagrant(官方文档)
  • 添加boxbox地址
  • 选择支持VirtualBox的box
  • 通过vim写Vagrantfile文件/
  • 通过命令
  • vagrant up(在VirtualBox就可以看到了)

VirtualBox&Vagrant在Mac下安装的坑

问题一:-bash: vagrant: command not found

傻瓜式安装VirtualBox后vagrant -v提示-bash: vagrant: command not found

解决一

把vagrant添加到环境变量里

  • cd ~/
  • vim .bash_profile
export VAGRANT_HOME=/opt/vagrant
PATH=$VAGRANT_HOME/bin:$PATH
  • source .bash_profile

问题二:Vagrant could not detect VirtualBox

傻瓜式安装VirtualBox后,执行vagrant up命令提示没有装,大致报错如下,于是联想到环境变量的问题

Vagrant could not detect VirtualBox! Make sure VirtualBox is properly install...

解决二

把vagrant添加到环境变量里VBoxManage添加到环境变量里

  • ln -s /Applications/VirtualBox.app/Contents/MacOS/VBoxManage /usr/local/bin
  • cd ~/
  • vim .bash_profile
PATH=/usr/local/bin:$PATH
  • source .bash_profile

问题三

VirtualBox没有发现用vagrant创建的虚拟机

解决三

  • sudo -i(切换到root)
  • chmod -R 777 /opt/vagrant(授权)
  • vagrant up(不要sudo vagrant up,否则建在root用户下就看不到了)

Vagrant常用命令

命令 描述
vagrant init [box-name] 在空文件夹初始化虚拟机,得到一个Vagrantfile文件,也可以直接自己写一个Vagrantfile文件代替这一步
vagrant up 启动虚拟机
vagrant ssh 登陆虚拟机
vagrant suspend 挂起虚拟机
vagrant reload 重启虚拟机
vagrant status 查看虚拟机状态
vagrant destroy 销毁虚拟机
vagrant box list 列出box
vagrant box add box-name(box-url) 添加box
vagrant box update box-name 更新box
vagrant box remove box-name 销毁box
vagrant box repackage box-name 重新打包box

Vagrantfile

  • 默认文件(vagrant init centos/7)
# -*- 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 = "base"

  # 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

  • 重点解析
命令 描述
config.vm.hostname 虚拟主机名
config.vm.network 虚拟主机网络
config.vm.synced_folder 目录
config.ssh.username 默认vagrant
config.vm.box box名字
config.vm.hostname 多台虚拟机如vagrant up centos1(hostname)指定启动这一台
  • 虚拟主机网络说明
    • 命令
      • config.vm.network "private_network", ip: "xxx.xxx.xx.xx"(Host-only模式)
      • config.vm.network "public_network", ip: "xx.x.x.xx"(Bridge模式)
    • 方式
      • NAT:缺省创建,用于让vm可以通过host转发访问局域网甚至互联网
      • Host-only:只有主机可以访问vm,其他机器无法访问它
      • Bridge:此模式下vm就像局域网中的一台独立的机器,可以被其他机器访问
  • 端口转发
    • 命令
      • config.vm.network :forwarded_port, guest: 80, host: 8080
    • 方式
      • http://localhost:8080访问了虚拟机的80端口

插件安装

# 安装文件传输工具
vagrant plugin install vagrant-scp
# 查看已经安装插件
vagrant plugin list