1.3 Vgrant使用入门

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

Vagrant常用命令

前面讲了Vagrant的几个命令:

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

Vagrant还包括如下一些操作:

  • vagrant box list

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

    1. $ vagrant box list
    2. base (virtualbox)
  • vagrant box remove

    删除相应的box

    1. $ vagrant box remove base virtualbox
    2. Removing box 'base' with provider 'virtualbox'...
  • vagrant destroy

    停止当前正在运行的虚拟机并销毁所有创建的资源

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

    关机

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

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

    1. $ vagrant package
    2. [default] Attempting graceful shutdown of VM...
    3. [default] Clearing any previously set forwarded ports...
    4. [default] Creating temporary directory for export...
    5. [default] Exporting VM...
    6. [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

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

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

    恢复前面被挂起的状态

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

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

    1. $vagrant ssh-config
    2. Host default
    3. HostName 127.0.0.1
    4. User vagrant
    5. Port 2222
    6. UserKnownHostsFile /dev/null
    7. StrictHostKeyChecking no
    8. PasswordAuthentication no
    9. IdentityFile "/Users/astaxie/.vagrant.d/insecure_private_key"
    10. IdentitiesOnly yes
    11. LogLevel FATAL
  • vagrant status

    获取当前虚拟机的状态

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

    挂起当前的虚拟机

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

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

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

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

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

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

  1. Vagrant.configure("2") do |config|
  2. config.vm.define :web do |web|
  3. web.vm.provider "virtualbox" do |v|
  4. v.customize ["modifyvm", :id, "--name", "web", "--memory", "512"]
  5. end
  6. web.vm.box = "base"
  7. web.vm.hostname = "web"
  8. web.vm.network :private_network, ip: "11.11.1.1"
  9. end
  10. config.vm.define :db do |db|
  11. db.vm.provider "virtualbox" do |v|
  12. v.customize ["modifyvm", :id, "--name", "db", "--memory", "512"]
  13. end
  14. db.vm.box = "base"
  15. db.vm.hostname = "db"
  16. db.vm.network :private_network, ip: "11.11.1.2"
  17. end
  18. end

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

  1. $ vagrant up
  2. Bringing machine 'web' up with 'virtualbox' provider...
  3. Bringing machine 'db' up with 'virtualbox' provider...
  4. [web] Setting the name of the VM...
  5. [web] Clearing any previously set forwarded ports...
  6. [web] Creating shared folders metadata...
  7. [web] Clearing any previously set network interfaces...
  8. [web] Preparing network interfaces based on configuration...
  9. [web] Forwarding ports...
  10. [web] -- 22 => 2222 (adapter 1)
  11. [web] Running any VM customizations...
  12. [web] Booting VM...
  13. [web] Waiting for VM to boot. This can take a few minutes.
  14. [web] VM booted and ready for use!
  15. [web] Setting hostname...
  16. [web] Configuring and enabling network interfaces...
  17. [web] Mounting shared folders...
  18. [web] -- /vagrant
  19. [db] Setting the name of the VM...
  20. [db] Clearing any previously set forwarded ports...
  21. [db] Fixed port collision for 22 => 2222. Now on port 2200.
  22. [db] Creating shared folders metadata...
  23. [db] Clearing any previously set network interfaces...
  24. [db] Preparing network interfaces based on configuration...
  25. [db] Forwarding ports...
  26. [db] -- 22 => 2200 (adapter 1)
  27. [db] Running any VM customizations...
  28. [db] Booting VM...
  29. [db] Waiting for VM to boot. This can take a few minutes.
  30. [db] VM booted and ready for use!
  31. [db] Setting hostname...
  32. [db] Configuring and enabling network interfaces...
  33. [db] Mounting shared folders...
  34. [db] -- /vagrant

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

  1. $ vagrant ssh web
  2. vagrant@web:~$
  3. $ vagrant ssh db
  4. vagrant@db:~$

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

  1. $ vagrant ssh web
  2. Linux web 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
  3. Ubuntu 10.04.4 LTS
  4. Welcome to the Ubuntu Server!
  5. * Documentation: http://www.ubuntu.com/server/doc
  6. New release 'precise' available.
  7. Run 'do-release-upgrade' to upgrade to it.
  8. Welcome to your Vagrant-built virtual machine.
  9. Last login: Thu Aug 8 18:55:44 2013 from 10.0.2.2
  10. vagrant@web:~$ ssh 11.11.1.2
  11. The authenticity of host '11.11.1.2 (11.11.1.2)' can't be established.
  12. RSA key fingerprint is e7:8f:07:57:69:08:6e:fa:82:bc:1c:f6:53:3f:12:9e.
  13. Are you sure you want to continue connecting (yes/no)? yes
  14. Warning: Permanently added '11.11.1.2' (RSA) to the list of known hosts.
  15. vagrant@11.11.1.2's password:
  16. Linux db 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
  17. Ubuntu 10.04.4 LTS
  18. Welcome to the Ubuntu Server!
  19. * Documentation: http://www.ubuntu.com/server/doc
  20. New release 'precise' available.
  21. Run 'do-release-upgrade' to upgrade to it.
  22. Welcome to your Vagrant-built virtual machine.
  23. Last login: Thu Aug 8 18:58:50 2013 from 10.0.2.2
  24. vagrant@db:~$

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