vagrant

Vagrant常用命令

Vagrant Cmd:

  • vagrant box add 添加box的操作
  • vagrant init 初始化box的操作
  • vagrant up 启动虚拟机的操作
  • vagrant ssh 登录虚拟机的操作

Vagrant还包括如下一些操作:

  • vagrant box list
    显示当前已经添加的box列表

      $ vagrant box list
      base (virtualbox)
    
  • vagrant box remove
    删除相应的box

      $ vagrant box remove base virtualbox
      Removing box 'base' with provider 'virtualbox'...
    
  • vagrant destroy
    停止当前正在运行的虚拟机并销毁所有创建的资源

      $ vagrant destroy
      Are you sure you want to destroy the 'default' VM? [y/N] y
      [default] Destroying VM and associated drives...
    
  • vagrant halt

    关机

      $ vagrant halt
      [default] Attempting graceful shutdown of VM...
    
  • vagrant package

    打包命令,可以把当前的运行的虚拟机环境进行打包

      $ vagrant package
      [default] Attempting graceful shutdown of VM...
      [default] Clearing any previously set forwarded ports...
      [default] Creating temporary directory for export...
      [default] Exporting VM...
      [default] Compressing package to: /Users/astaxie/vagrant/package.box
    
  • vagrant plugin

    用于安装卸载插件

  • vagrant provision

    通常情况下Box只做最基本的设置,而不是设置好所有的环境,因此Vagrant通常使用Chef或者Puppet来做进一步的环境搭建。那么Chef或者Puppet称为provisioning,而该命令就是指定开启相应的provisioning。按照Vagrant作者的说法,所谓的provisioning就是"The problem of installing software on a booted system"的意思。除了Chef和Puppet这些主流的配置管理工具之外,我们还可以使用Shell来编写安装脚本。

    例如: vagrant provision --provision-with chef

  • vagrant reload

    重新启动虚拟机,主要用于重新载入配置文件

      $ vagrant reload
      [default] Attempting graceful shutdown of VM...
      [default] Setting the name of the VM...
      [default] Clearing any previously set forwarded ports...
      [default] Creating shared folders metadata...
      [default] Clearing any previously set network interfaces...
      [default] Preparing network interfaces based on configuration...
      [default] Forwarding ports...
      [default] -- 22 => 2222 (adapter 1)
      [default] Booting VM...
      [default] Waiting for VM to boot. This can take a few minutes.
      [default] VM booted and ready for use!
      [default] Setting hostname...
      [default] Mounting shared folders...
      [default] -- /vagrant
    
  • vagrant resume

    恢复前面被挂起的状态

      $vagrant resume
      [default] Resuming suspended VM...
      [default] Booting VM...
      [default] Waiting for VM to boot. This can take a few minutes.
      [default] VM booted and ready for use!
    
  • vagrant ssh-config

    输出用于ssh连接的一些信息

      $vagrant ssh-config
      Host default
        HostName 127.0.0.1
        User vagrant
        Port 2222
        UserKnownHostsFile /dev/null
        StrictHostKeyChecking no
        PasswordAuthentication no
        IdentityFile "/Users/astaxie/.vagrant.d/insecure_private_key"
        IdentitiesOnly yes
        LogLevel FATAL
    
  • vagrant status

    获取当前虚拟机的状态

      $vagrant status
      Current machine states:
    
      default                   running (virtualbox)
    
      The VM is running. To stop this VM, you can run `vagrant halt` to
      shut it down forcefully, or you can run `vagrant suspend` to simply
      suspend the virtual machine. In either case, to restart it again,
      simply run `vagrant up`.
    
  • vagrant suspend

    挂起当前的虚拟机

      $ vagrant suspend
      [default] Saving VM state and suspending execution...
    

模拟打造多机器的分布式系统

前面这些单主机单虚拟机主要是用来自己做开发机,从这部分开始的内容主要将向大家介绍如何在单机上通过虚拟机来打造分布式造集群系统。这种多机器模式特别适合以下几种人:

  1. 快速建立产品网络的多机器环境,例如web服务器、db服务器
  2. 建立一个分布式系统,学习他们是如何交互的
  3. 测试API和其他组件的通信
  4. 容灾模拟,网络断网、机器死机、连接超时等情况

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

现在我们来建立多台VM跑起來,並且让他们之间能够相通信,假设一台是应用服务器、一台是DB服务器,那么这个结构在Vagrant中非常简单,其实和单台的配置差不多,你只需要通过config.vm.define来定义不同的角色就可以了,现在我们打开配置文件进行如下设置:

Vagrant.configure("2") do |config|
  config.vm.define :web do |web|
    web.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--name", "web", "--memory", "512"]
    end
    web.vm.box = "base"
    web.vm.hostname = "web"
    web.vm.network :private_network, ip: "11.11.1.1"
  end

  config.vm.define :db do |db|
    db.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--name", "db", "--memory", "512"]
    end
    db.vm.box = "base"
    db.vm.hostname = "db"
    db.vm.network :private_network, ip: "11.11.1.2"
  end
end

这里的设置和前面我们单机设置配置类似,只是我们使用了:web以及:db分別做了两个VM的设置,并且给每个VM设置了不同的hostname和IP,设置好之后再使用vagrant up将虚拟机跑起来:

$ vagrant up
Bringing machine 'web' up with 'virtualbox' provider...
Bringing machine 'db' up with 'virtualbox' provider...
[web] Setting the name of the VM...
[web] Clearing any previously set forwarded ports...
[web] Creating shared folders metadata...
[web] Clearing any previously set network interfaces...
[web] Preparing network interfaces based on configuration...
[web] Forwarding ports...
[web] -- 22 => 2222 (adapter 1)
[web] Running any VM customizations...
[web] Booting VM...
[web] Waiting for VM to boot. This can take a few minutes.
[web] VM booted and ready for use!
[web] Setting hostname...
[web] Configuring and enabling network interfaces...
[web] Mounting shared folders...
[web] -- /vagrant
[db] Setting the name of the VM...
[db] Clearing any previously set forwarded ports...
[db] Fixed port collision for 22 => 2222. Now on port 2200.
[db] Creating shared folders metadata...
[db] Clearing any previously set network interfaces...
[db] Preparing network interfaces based on configuration...
[db] Forwarding ports...
[db] -- 22 => 2200 (adapter 1)
[db] Running any VM customizations...
[db] Booting VM...
[db] Waiting for VM to boot. This can take a few minutes.
[db] VM booted and ready for use!
[db] Setting hostname...
[db] Configuring and enabling network interfaces...
[db] Mounting shared folders...
[db] -- /vagrant

看到上面的信息输出后,我们就可以通过vagrant ssh登录虚拟机了,但是这次和上次使用的不一样了,这次我们需要指定相应的角色,用来告诉ssh你期望连接的是哪一台:

$ vagrant ssh web
vagrant@web:~$

$ vagrant ssh db
vagrant@db:~$

是不是很酷!现在接下来我们再来验证一下虚拟机之间的通信,让我们先使用ssh登录web虚拟机,然后在web虚拟机上使用ssh登录db虚拟机(默认密码是vagrant):

$ vagrant ssh web
Linux web 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS

Welcome to the Ubuntu Server!
 * Documentation:  http://www.ubuntu.com/server/doc
New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Thu Aug  8 18:55:44 2013 from 10.0.2.2
vagrant@web:~$ ssh 11.11.1.2
The authenticity of host '11.11.1.2 (11.11.1.2)' can't be established.
RSA key fingerprint is e7:8f:07:57:69:08:6e:fa:82:bc:1c:f6:53:3f:12:9e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '11.11.1.2' (RSA) to the list of known hosts.
vagrant@11.11.1.2's password:
Linux db 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS

Welcome to the Ubuntu Server!
 * Documentation:  http://www.ubuntu.com/server/doc
New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Thu Aug  8 18:58:50 2013 from 10.0.2.2
vagrant@db:~$

通过上面的信息我们可以看到虚拟机之间通信是畅通的,所以现在开始你伟大的架构设计吧,你想设计怎么样的架构都可以,唯一限制你的就是你主机的硬件配置了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,084评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,623评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,450评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,322评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,370评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,274评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,126评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,980评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,414评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,599评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,773评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,470评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,080评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,713评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,852评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,865评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,689评论 2 354

推荐阅读更多精彩内容

  • 1,介绍Vagrant 我们做web开发的时候经常要安装各种本地测试环境,比如apache,php,mysql,r...
    meng_philip123阅读 2,516评论 0 12
  • 什么是Vagrant简单来说,Vagrant 就是一个虚拟机的集成管理器。 我们用它可以快速创建虚拟机,可以快速部...
    嗝喯唲阅读 650评论 0 0
  • Vagrant学习笔记 简介 vagrant用于快速创建基于VirtualBox、VMware、AWS的虚拟机,提...
    200cc阅读 8,408评论 0 15
  • 本次会议根据锦囊讨论了本周六班级会议的流程,困惑,以及确定会议角色和各自准备的工作。要点如下: 1,本次会议流程去...
    王昌伦阅读 405评论 0 1
  • 如果你只做一件事情,一定可以干的很好
    农爸爸阅读 146评论 0 0