所在位置:

vagrant工具的使用

Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境,它底层支持VirtualBox、VMware作为虚拟机系统

Vagrant的安装(基于Mac系统)

  • 从苹果的应用商店下载和安装最新的 Xcode
  • 下载和安装最新的 VirtualBox
  • 下载和安装最新的 Vagrant

选择合适的box镜像

Vagrant的创建镜像常用指令

vagrant box add {title} {url}
vagrant init {title}
vagrant up
vagrant ssh

初始化box镜像

第一种方法是在线安装
  • 可以从 vagrantbox 或者 hash 查看所需要的box的地址
  • 下载的box镜像放在 ~/.vagrant.d/boxes/ 目录下
vagrant box add precise64 http://files.vagrantup.com/precise64.box
cd ~/vagrant
vagrant init precise64
本地安装
vagrant box add precise64 ~/box/precise64.box
cd ~/vagrant
vagrant init precise64

配置Vagrant文件

上面执行的 vagrant init precise64 命令会在当前目录下生成一个 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://atlas.hashicorp.com/search.

  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  config.ssh.insert_key = false
  config.ssh.private_key_path = ["~/.ssh/id_rsa"]
  config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"

  config.vm.define "web" do |web|
    web.vm.box = "ubuntu"
    web.vm.hostname = "web"
    web.vm.network "forwarded_port", guest: 8080, host:8080
    web.vm.network "forwarded_port", guest: 5000, host:5000
    web.vm.network "private_network", ip: "11.11.11.11"
    web.vm.synced_folder "~/workspace/vagrant/web", "/home/vagrant/web", create: true
    web.vm.provider "virtualbox" do |vb|
      vb.gui = false,
      vb.name = "my_ubuntu_web"
      vb.cpus = 1
      vb.memory = 1024
    end
  end

  config.vm.define "db" do |db|
    db.vm.box = "ubuntu"
    db.vm.hostname = "db"
    db.vm.network "forwarded_port", guest: 9000, host:9000
    db.vm.network "forwarded_port", guest: 8000, host:8000
    db.vm.network "private_network", ip: "22.22.22.22"
    db.vm.synced_folder "~/workspace/vagrant/db", "/home/vagrant/db", create: true
    db.vm.provider "virtualbox" do |vb|
      vb.gui = false,
      vb.name = "my_ubuntu_db"
      vb.cpus = 1
      vb.memory = 1024
    end
  end

  # 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.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # 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.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # 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

注意: 如果没有 ~/.ssh/id_rsa 文件,可以先用 ssh-keygen 命令生成

安装box镜像和启动

可以使用下面的命令进行安装

vagrant up web 
vagrant up db 
vagrant provision
vagrant ssh web
vagrant ssh db

共享自己定义的box

  • 使用 vagrant package 命令生成一个自定义的 custom.box
  • 共享给别人或者上传到网上给别人下载
  • 使用 vagrant box add ~/custom.box 来运行自定义的box

删除box镜像

  • vagrant global-status 命令查看 {machine id}
  • 使用 vagrant destory {machine id} 命令彻底删除

常用的Vagrant命令

  • vagrant box add 添加box
  • vagrant init 初始化box的操作,会生成vagrant的配置文件Vagrantfile
  • vagrant up 启动本地环境
  • vagrant ssh 通过 ssh 登录本地环境所在虚拟机
  • vagrant halt 关闭本地环境
  • vagrant suspend 暂停本地环境
  • vagrant resume 恢复本地环境
  • vagrant reload 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
  • vagrant destroy 彻底移除本地环境
  • vagrant box list 显示当前已经添加的box列表
  • vagrant package 打包命令,可以把当前的运行的虚拟机环境进行打包
  • vagrant status 查看当前虚拟机的运行状态
  • vagrant global-status 显示当前用户Vagrant的所有环境状态

参考网站

【上一篇】梦想

【下一篇】Centos使用公钥配置和禁止root远程登录

相关文章
  • 没有相关文章