Docker

Vagrant - 常用命令总结

2018-12-23  本文已影响0人  红薯爱帅

1. 简介

Vagrant支持单机模拟多台机器,而且支持一个配置文件Vagrntfile就可以跑分布式系统。

image.png

2. 常用命令

2.1. machine管理

$ vagrant init      # 初始化,生成Vagrantfile,可指定box
$ vagrant up        # 启动虚拟机,可指定machine
$ vagrant halt      # 关闭虚拟机,可指定machine
$ vagrant reload    # 重启虚拟机,并重新加载配置参数,可指定machine
$ vagrant ssh       # SSH 至虚拟机,可指定machine
$ vagrant suspend   # 挂起虚拟机,可指定machine
$ vagrant resume    # 唤醒虚拟机,可指定machine
$ vagrant status    # 查看虚拟机运行状态,可指定machine
$ vagrant destroy   # 销毁当前虚拟机,可指定machine
$ vagrant suspend   # 挂起当前的虚拟机
$ vagrant resume    # 恢复前面被挂起的状态

2.2. box管理

$ vagrant box list    # 查看本地box列表
$ vagrant box add     # 添加box到列表
$ vagrant box remove  # 从box列表移除 

2.3. machine与box转换

$ vagrant package        # 对指定machine打包成box
$ vagrant box repackage  # 对指定box重新打包成box,该box的machine会被halt
$ vagrant package --output web.box web
==> web: Attempting graceful shutdown of VM...
==> web: Clearing any previously set forwarded ports...
==> web: Exporting VM...
==> web: Compressing package to: /home/kevin/learn/vagrant/test-cluster1/web.box
$ vagrant box repackage <name> <provider> <version>

2.4. 快照管理

后续补充

3. Vagrant File

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.define "web" do |web|
    # web.vm.box = "ubuntu/xenial64"
    web.vm.box = "ubuntu/1604"
    web.vm.box_url = "file:///home/kevin/learn/vagrant/xenial-server-cloudimg-amd64-vagrant.box"
    web.vm.hostname = "web"
    web.vm.network "private_network", ip: "192.168.56.10"
    web.vm.provider "virtualbox" do |v|
      v.gui = false
      v.name = "ubuntu-0"
      v.cpus = "1"
      v.memory = "2048"
    end
  end

  config.vm.define "db" do |db|
    # db.vm.box = "ubuntu/xenial64"
    db.vm.box = "ubuntu/1604"
    db.vm.box_url = "file:///home/kevin/learn/vagrant/xenial-server-cloudimg-amd64-vagrant.box"
    db.vm.hostname = "db"
    db.vm.network "private_network", ip: "192.168.56.11"
    db.vm.provider "virtualbox" do |v|
      v.gui = false
      v.name = "ubuntu-1"
      v.cpus = "1"
      v.memory = "2048"
    end
  end
end

4. docs

上一篇下一篇

猜你喜欢

热点阅读