吸毒的虫子 发表于 2015-11-26 09:12:59

vagrant


1. Vagrant的介绍


虚拟开发环境


平常我们经常会遇到这样的问题:在开发机上面开发完毕程序,放到正式环境之后会出现各种奇怪的问题:描述符少了、nginx配置不正确、MySQL编码不对、php缺少模块、glibc版本太低等。

所以我们就需要虚拟开发环境,我们虚拟和正式环境一样的虚拟开发环境,而随着个人开发机硬件的升级,我们可以很容易的在本机跑虚拟机,例如VMware、VirtualBox等。因此使用虚拟化开发环境,在本机可以运行自己喜欢的OS(Windows、Ubuntu、Mac等),开发的程序运行在虚拟机中,这样迁移到生产环境可以避免环境不一致导致的莫名错误。

虚拟开发环境特别适合团队中开发环境、测试环境、正式环境不同的场合,这样就可以使得整个团队保持一致的环境,我写这一章的初衷就是为了让大家和我的开发环境保持一致,让读者和我们整个大团队保持一致的开发环境。


Vagrant


Vagrant 是一款用来构建虚拟开发环境的工具,非常适合
php/python/ruby/java 这类语言开发 web 应用,“代码在我机子上运行没有问题”这种说辞将成为历史。Vagrant就是为了方便的实现虚拟化环境而设计的,使用Ruby开发,基于VirtualBox等虚拟机管理软件的接口,提供了一个可配置、轻量级的便携式虚拟开发环境。使用Vagrant可以很方便的就建立起来一个虚拟环境,而且可以模拟多台虚拟机,这样我们平时还可以在开发机模拟分布式系统。

Vagrant还会创建一些共享文件夹,用来给你在主机和虚拟机之间共享代码用。这样就使得我们可以在主机上写程序,然后在虚拟机中运行。如此一来团队之间就可以共享相同的开发环境,就不会再出现类似“只有你的环境才会出现的bug”这样的事情。我们可以通过
Vagrant 封装一个 Linux 的开发环境,分发给团队成员。成员可以在自己喜欢的桌面系统(Mac/Windows/Linux)上开发程序,代码却能统一在封装好的环境里运行,非常霸气。

团队新员工加入,常常会遇到花一天甚至更多时间来从头搭建完整的开发环境,而有了Vagrant,只需要直接将已经打包好的package(里面包括开发工具,代码库,配置好的服务器等)拿过来就可以工作了,这对于提升工作效率非常有帮助。

Vagrant不仅可以用来作为个人的虚拟开发环境工具,而且特别适合团队使用,它使得我们虚拟化环境变得如此的简单,只要一个简单的命令就可以开启虚拟之路。




2 Vagrant安装配置
  

实际上Vagrant只是一个让你可以方便设置你想要的虚拟机的便携式工具,它底层支持VirtualBox、VMware甚至AWS作为虚拟机系统,虽然
Vagrant 也支持 VMware,不过 VMware 是收费的,对应的 Vagrant 版本也是收费的。我们将使用VirtualBox来进行说明,所以第一步需要先安裝Vagrant和VirtualBox。


VirtualBox安装


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

sudo apt-get install virtualbox


Vagrant安装


下载地址:http://downloads.vagrantup.com/ 根据提示一步步安装。



ubuntu直接apt-get: sudo apt-get install vagrant


Vagrant配置


当我们安装好VirtualBox和Vagrant后,我们要开始考虑在VM上使用什么操作系统了,一个打包好的操作系统在Vagrant中称为Box,即Box是一个打包好的操作系统环境,目前网络上什么都有,所以你不用自己去制作操作系统或者制作Box:vagrantbox.es上面有大家熟知的大多数操作系统,你只需要下载就可以了,下载主要是为了安装的时候快速,当然Vagrant也支持在线安装。


建立开发环境目录

/home/liukuan73/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,后面的命令都是基于这个标识来操作的。

假设我们下载的镜像存放路径是 ~/box/precise64.box,在终端里输入:

$ vagrant box add base ~/box/precise64.box

base 是我们给这个
box 命的名字,~/box/precise64.box 是
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

我在开发机上面是这样操作的,首先进入我们的开发环境目录/home/liukuan73/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中的镜像文件被放到了:/home/liukuan73/.vagrant.d/boxes/,如果在window系统中应该是放到了: C:\Users\当前用户名\.vagrant.d\boxes\目录下。

通过vagrant box add这样的方式安装远程的box,可能很慢,所以建议大家先下载box到本地再执行这样的操作。


初始化




创建一个开发目录(比如:~/vagrant),你也可以使用已有的目录,切换到开发目录里,用base镜像初始化当前目录的环境:

$ cd ~/vagrant# 切换目录
$ vagrant init base # 初始化
$ vagrant up# 启动环境

初始化的命令如下:

vagrant init Name

如果你添加的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...
Importing base box 'base'...
Matching MAC address for NAT networking...
Setting the name of the VM...
Clearing any previously set forwarded ports...
Creating shared folders metadata...
Clearing any previously set network interfaces...
Preparing network interfaces based on configuration...
Forwarding ports...
-- 22 => 2222 (adapter 1)
Booting VM...
Waiting for VM to boot. This can take a few minutes.
VM booted and ready for use!
Mounting shared folders...
-- /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            SizeUsed Avail Use% Mounted on
/dev/mapper/lucid64-root
78G945M   73G   2% /
none                  179M176K179M   1% /dev
none                  184M   0184M   0% /dev/shm
none                  184M   64K184M   1% /var/run
none                  184M   0184M   0% /var/lock
none                  184M   0184M   0% /lib/init/rw
none                   78G945M   73G   2% /var/lib/ureadahead/debugfs
/dev/sda1             228M   17M199M   8% /boot
/vagrant            298G   76G222G26% /vagrant

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


Vagrantfile配置文件详解


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



NOTE:配置版本说明:


1
2
3

Vagrant.configure("2") do |config|
#
...
end


当前支持的两个版本:"1"和"2". "1":描述是Vagrant 1.0.x的配置(如看到Vagrant::Config.run do |config| 此也为Vagrant 1.0.x 的配置);"2":描述的是1.1+ leading up to 2.0.x的配置。vagrant 1.1+ 的Vagrantfiles能够与vagrant
1.0.x的Vagrantfiles保持向后兼容,也大幅引入新的功能和配置选项




[*]
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", "ubuntu", "--memory", "512"]
end
这行设置的意思是调用VBoxManage的modifyvm的命令,设置VM的名称为astaxie,内存为512MB。你可以类似的通过定制其它VM属性来定制你自己的VM。
[*]
网络设置
Vagrant有两种方式来进行网络连接,一种是private(host-only),意思是主机和虚拟机之间的网络互访,而不是虚拟机访问internet的技术,也就是只有你一個人自High,其他人访问不到你的虚拟机。另一种是public(Bridge),该模式下的VM就像是局域网中的一台独立的主机,也就是说需要VM到你的路由器要IP,这样的话局域网里面其他机器就可以访问它了:

private:允许多个虚拟机通过主机通过网络互相通信,vagrant允许用户分配一个静态IP,然后使用私有网络设置。

版本"2"


1
2
3

Vagrant.configure("2") do |config|
config.vm.network
:private_network, ip: "192.168.50.4"
end




版本"1"


1
2
3

Vagrant::Config.run do |config|
config.vm.network
:hostonly, "192.168.50.4"
end




桥接网络(公共网络,局域网DHCP服务器自动分配IP)设置如下:

版本"2"


1
2
3

Vagrant.configure("2") do |config|
config.vm.network :public_network
或:config.vm.network "public_network", :bridge => 'eth0'
end





版本"1"


1
2
3

Vagrant::Config.run do |config|
config.vm.network
:bridged
end
[*]
hostname设置
hostname的设置非常简单,Vagrantfile中加入下面这行就可以了:

config.vm.hostname = "ubuntu"
设置hostname非常重要,因为当我们有很多台虚拟服务器的时候,都是依靠hostname來做识别的,例如Puppet或是Chef,都是通过hostname來做识别的,既然设置那么简单,所以我们就別偷懒,设置一个。
[*]
同步目录
我们上面介绍过/vagrant目录默认就是当前的开发目录,这是在虚拟机开启的时候默认挂载同步的。默认的,vagrant将共享你的工作目录(即Vagrantfile所在的目录)到虚拟机中的/vagrant,所以一般不需配置即可,如需要可以通过配置来设置额外的同步目录:

config.vm.synced_folder"/home/liukuan73/data", "/vagrant_data"
上面这个设定,第一个参数是主机的目录,第二个参数是虚拟机挂载的目录
[*]
端口转发

版本"2":


1
2
3

Vagrant.configure("2") do |config|
config.vm.network
:forwarded_port, guest: 80, host: 8080
end


版本"1"


1
2
3
4

Vagrant::Config.run do |config|
config.vm.forward_port
80, 8080
end




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




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





3 Vgrant使用入门


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


Vagrant常用命令


[*]vagrant box add URL 添加box的操作
[*]vagrant init NAME 初始化box的操作(实质应是创建Vagrantfile文件)
[*]vagrant up 启动虚拟机的操作
[*]vagrant ssh 登录虚拟机的操作
[*]vagrant box list   查看本地已添加的box


[*]vagrant box remove NAME virtualbox删除本地已添加的box,如若是版本1.0.x,执行$
vagrant box removeNAME


[*]

vagrant halt               关闭虚拟机
[*]

vagrant destroy            销毁虚拟机


[*]

vagrant
reload             重启虚拟机


[*]

vagrant
package            当前正在运行的VirtualBox虚拟环境打包成一个可重复使用的box


[*]

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
Destroying VM and associated drives...
[*]
vagrant halt
关机

$ vagrant halt
Attempting graceful shutdown of VM...
[*]
vagrant package
打包命令,可以把当前的运行的虚拟机环境进行打包

$ vagrant package
Attempting graceful shutdown of VM...
Clearing any previously set forwarded ports...
Creating temporary directory for export...
Exporting VM...
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
Attempting graceful shutdown of VM...
Setting the name of the VM...
Clearing any previously set forwarded ports...
Creating shared folders metadata...
Clearing any previously set network interfaces...
Preparing network interfaces based on configuration...
Forwarding ports...
-- 22 => 2222 (adapter 1)
Booting VM...
Waiting for VM to boot. This can take a few minutes.
VM booted and ready for use!
Setting hostname...
Mounting shared folders...
-- /vagrant
[*]
vagrant resume
恢复前面被挂起的状态

$vagrant resume
Resuming suspended VM...
Booting VM...
Waiting for VM to boot. This can take a few minutes.
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
Saving VM state and suspending execution...


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


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


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


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...
Setting the name of the VM...
Clearing any previously set forwarded ports...
Creating shared folders metadata...
Clearing any previously set network interfaces...
Preparing network interfaces based on configuration...
Forwarding ports...
-- 22 => 2222 (adapter 1)
Running any VM customizations...
Booting VM...
Waiting for VM to boot. This can take a few minutes.
VM booted and ready for use!
Setting hostname...
Configuring and enabling network interfaces...
Mounting shared folders...
-- /vagrant
Setting the name of the VM...
Clearing any previously set forwarded ports...
Fixed port collision for 22 => 2222. Now on port 2200.
Creating shared folders metadata...
Clearing any previously set network interfaces...
Preparing network interfaces based on configuration...
Forwarding ports...
-- 22 => 2200 (adapter 1)
Running any VM customizations...
Booting VM...
Waiting for VM to boot. This can take a few minutes.
VM booted and ready for use!
Setting hostname...
Configuring and enabling network interfaces...
Mounting shared folders...
-- /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 Aug8 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 Aug8 18:58:50 2013 from 10.0.2.2
vagrant@db:~$

通过上面的信息我们可以看到虚拟机之间通信是畅通的,所以现在开始你伟大的架构设计吧,你想设计怎么样的架构都可以,唯一限制你的就是你主机的硬件配置了。
  参考自:https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.1.md
  https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.2.md


  https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.3.md


  http://topmanopensource.iyunv.com/blog/2002302
  http://segmentfault.com/a/1190000000264347
页: [1]
查看完整版本: vagrant