Vagrant安装、使用

Vagrant安装配置

实际上Vagrant只是一个让你能够方便设置你想要的虚拟机的便携式工具,它底层支持VirtualBox、VMware甚至AWS做为虚拟机系统,本书中咱们将使用VirtualBox来进行说明,因此第一步须要先安裝Vagrant和VirtualBox。php

VirtualBox安装

VirtualBox是Oracle开源的虚拟化系统,它支持多个平台,因此你能够到官方网站:https://www.virtualbox.org/wiki/Downloads/ 下载适合你平台的VirtualBox最新版本并安装,它的安装过程都很傻瓜化,一步一步执行就能够完成安装了。html

Vagrant安装

最新版本的Vagrant已经没法经过gem命令来安装,由于依赖库太多了,因此目前没法使用gem来安装,目前网络上面不少教程仍是相似这样的命令,那些都是错误的。目前惟一安装的办法就是到官方网站下载打包好的安装包:http://www.vagrantup.com/downloads.html 他的安装过程和VirtualBox的安装同样都是傻瓜化安装,一步一步执行就能够完成安装。nginx

尽可能下载最新的程序,由于VirtualBox常常升级,升级后有些接口会变化,老的Vagrant可能没法使用。web

要想检测安装是否成功,能够打开终端命令行工具,输入vagrant,看看程序是否是已经能够运行了。若是不行,请检查一下$PATH里面是否包含vagrant所在的路径。shell

Vagrant配置

当咱们安装好VirtualBox和Vagrant后,咱们要开始考虑在VM上使用什么操做系统了,一个打包好的操做系统在Vagrant中称为Box,即Box是一个打包好的操做系统环境,目前网络上什么都有,因此你不用本身去制做操做系统或者制做Box:vagrantbox.es上面有你们熟知的大多数操做系统,你只须要下载就能够了,下载主要是为了安装的时候快速,固然Vagrant也支持在线安装。ubuntu

创建开发环境目录

个人开发机是Mac,因此我创建了以下的开发环境目录,读者能够根据本身的系统不一样创建一个目录就能够:浏览器

/Users/astaxie/vagrant

下载box

前面讲了box是一个操做系统环境,实际上它是一个zip包,包含了Vagrant的配置信息和VirtualBox的虚拟机镜像文件.咱们这一次的实战使用官方提供了一个box:Ubuntu lucid 64 http://files.vagrantup.com/lucid64.box服务器

固然你也能够选一个本身团队在用的系统,例如CentOS、Debian等,咱们能够经过上面说的地址下载开源爱好者们制做好的box。固然你本身作一个也行,下一节我会讲述如何本身制做包。网络

添加box

添加box的命令以下:架构

vagrant box add base 远端的box地址或者本地的box文件名

vagrant box add 是添加box的命令

base是box的名称,能够是任意的字符串,base是默认名称,主要用来标识一下你添加的box,后面的命令都是基于这个标识来操做的。

例子:

vagrant box add base http://files.vagrantup.com/lucid64.box
vagrant box add base https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box
vagrant box add base CentOS-6.3-x86_64-minimal.box
vagrant box add "CentOS 6.3 x86_64 minimal" CentOS-6.3-x86_64-minimal.box

我在开发机上面是这样操做的,首先进入咱们的开发环境目录/Users/astaxie/vagrant,执行以下的命令

vagrant box add base lucid64.box

安装过程的信息:

Downloading or copying the box...
Extracting box...te: 47.5M/s, Estimated time remaining: --:--:--)
Successfully added box 'base' with provider 'virtualbox'!

box中的镜像文件被放到了:/Users/astaxie/.vagrant.d/boxes/,若是在window系统中应该是放到了: C:\Users\当前用户名\.vagrant.d\boxes\目录下。

经过vagrant box add这样的方式安装远程的box,可能很慢,因此建议你们先下载box到本地再执行这样的操做。

初始化

初始化的命令以下:

vagrant init

若是你添加的box名称不是base,那么须要在初始化的时候指定名称,例如

vagrant init "CentOS 6.3 x86_64 minimal"

初始化过程的信息:

A `Vagrantfile` has been placed in this directory.
You are now ready to `vagrant up` your first virtual environment!
Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.

这样就会在当前目录生成一个 Vagrantfile的文件,里面有不少配置信息,后面咱们会详细讲解每一项的含义,可是默认的配置就能够开箱即用。

启动虚拟机

启动虚拟机的命令以下:

vagrant up

启动过程的信息:

Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'base'...
[default] Matching MAC address for NAT networking...
[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] Mounting shared folders...
[default] -- /vagrant

链接到虚拟机

上面已经启动了虚拟机,以后咱们就能够经过ssh来链接到虚拟机了。好比在个人开发机中能够像这样来链接:

vagrant ssh

链接到虚拟机后的信息以下:

Linux lucid64 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: Fri Sep 14 07:31:39 2012 from 10.0.2.2

这样咱们就能够像链接到一台服务器同样进行操做了。

window机器不支持这样的命令,必须使用第三方客户端来进行链接,例如putty、Xshell4等.

putty为例:

主机地址: 127.0.0.1

端口: 2222

用户名: vagrant

密码: vagrant

 

系统信息

进入系统以后咱们能够看一下系统的基础信息:

vagrant@lucid64:/vagrant$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/lucid64-root
                       78G  945M   73G   2% /
none                  179M  176K  179M   1% /dev
none                  184M     0  184M   0% /dev/shm
none                  184M   64K  184M   1% /var/run
none                  184M     0  184M   0% /var/lock
none                  184M     0  184M   0% /lib/init/rw
none                   78G  945M   73G   2% /var/lib/ureadahead/debugfs
/dev/sda1             228M   17M  199M   8% /boot
/vagrant              298G   76G  222G  26% /vagrant

/vagrant这个目录是自动映射的,被映射到/Users/astaxie/vagrant,这样就方便咱们之后在开发机中进行开发,在虚拟机中进行运行效果测试了。

Vagrantfile配置文件详解

在咱们的开发目录下有一个文件Vagrantfile,里面包含有大量的配置信息,主要包括三个方面的配置,虚拟机的配置、SSH配置、Vagrant的一些基础配置。Vagrant是使用Ruby开发的,因此它的配置语法也是Ruby的,可是咱们没有学过Ruby的人仍是能够跟着它的注释知道怎么配置一些基本项的配置。

  1. box设置

    config.vm.box = "base"

    上面这配置展现了Vagrant要去启用那个box做为系统,也就是上面咱们输入vagrant init Box名称时所指定的box,若是沒有输入box名称的話,那么默认就是base,VirtualBox提供了VBoxManage这个命令行工具,可让咱们设定VM,用modifyvm这个命令让咱们能够设定VM的名称和内存大小等等,这里说的名称指的是在VirtualBox中显示的名称,咱们也能够在Vagrantfile中进行设定,在Vagrantfile中加入以下这行就能够设定了:

    config.vm.provider "virtualbox" do |v|
       v.customize ["modifyvm", :id, "--name", "astaxie", "--memory", "512"]
     end

    这行设置的意思是调用VBoxManage的modifyvm的命令,设置VM的名称为astaxie,内存为512MB。你能够相似的经过定制其它VM属性来定制你本身的VM。

  2. 网络设置

    Vagrant有两种方式来进行网络链接,一种是host-only(主机模式),意思是主机和虚拟机之间的网络互访,而不是虚拟机访问internet的技术,也就是只有你一個人自High,其余人访问不到你的虚拟机。另外一种是Bridge(桥接模式),该模式下的VM就像是局域网中的一台独立的主机,也就是说须要VM到你的路由器要IP,这样的话局域网里面其余机器就能够访问它了,通常咱们设置虚拟机都是自high为主,因此咱们的设置通常以下:

    config.vm.network :private_network, ip: "11.11.11.11"

    这里咱们虚拟机设置为hostonly,而且指定了一个IP,IP的话建议最好不要用192.168..这个网段,由于颇有可能和你局域网里面的其它机器IP冲突,因此最好使用相似11.11..这样的IP地址。

  3. hostname设置

    hostname的设置很是简单,Vagrantfile中加入下面这行就能够了:

    config.vm.hostname = "go-app"

    设置hostname很是重要,由于当咱们有不少台虚拟服务器的时候,都是依靠hostname來作识别的,例如Puppet或是Chef,都是经过hostname來作识别的,既然设置那么简单,因此咱们就別偷懒,设置一个。

  4. 同步目录

    咱们上面介绍过/vagrant目录默认就是当前的开发目录,这是在虚拟机开启的时候默认挂载同步的。咱们还能够经过配置来设置额外的同步目录:

    config.vm.synced_folder  "/Users/astaxie/data", "/vagrant_data"

    上面这个设定,第一个参数是主机的目录,第二个参数是虚拟机挂载的目录

  5. 端口转发

    config.vm.network :forwarded_port, guest: 80, host: 8080

    上面这句配置可厉害了,这一行的意思是把对host机器上8080端口的访问请求forward到虚拟机的80端口的服务上,例如你在你的虚拟机上使用nginx跑了一个php应用,那么你在host机器上的浏览器中打开http://localhost:8080时,Vagrant就会把这个请求转发到VM里面跑在80端口的nginx服务上,所以咱们能够经过这个设置来帮助咱们去设定host和VM之间,或是VM和VM之间的信息交互。

          修改完Vagrantfile的配置后,记得要用vagrant reload命令来重启VM以后才能使用VM更新后的配置

 

 

Vgrant使用入门

前面咱们已经学会了如何安装并配置Vagrant,并且也已经按照默认的方式开启了,那么这一小节就给你们介绍一下Vagrant的高级应用。

Vagrant经常使用命令

前面讲了Vagrant的几个命令:

  • 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:~$

经过上面的信息咱们能够看到虚拟机之间通讯是畅通的,因此如今开始你伟大的架构设计吧,你想设计怎么样的架构均可以,惟一限制你的就是你主机的硬件配置了。

相关文章
相关标签/搜索