vagrant之运维,搭建统一开发环境

      特色:经过vagrant打包环境,能够跨平台使用。意思就是在windows下可使用ubuntu系统配置的环境
node

使用的平台:windows+64位web

须要准备的工具:shell

       virtualbox:虚拟机  https://www.virtualbox.org/wiki/Downloads
apache

       vagrant:下载地址  http://downloads.vagrantup.com/  
ubuntu


下载须要使用的 box
vim

       经过 http://www.vagrantbox.es/  进行下载
windows


*通常操做命令bash

  vagrant box add NAME URL    #添加一个box服务器

   vagrant box list            #查看本地已添加的box网络

   vagrant box remove NAME virtualbox #删除本地已添加的box,如如果版本1.0.x,执行$ vagrant box remove  NAME

   vagrant init NAME          #初始化,实质应是建立Vagrantfile文件

   vagrant up                   #启动虚拟机

   vagrant halt                 #关闭虚拟机

   vagrant destroy            #销毁虚拟机

   vagrant reload             #重启虚拟机

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

   vagrant ssh                 #进入虚拟环境


*vagrantfile文件的做用:

        配置这个虚拟主机网络链接方式,端口转发,同步文件夹,以及怎么和puppet,chef结合的一个配置文件。执行完$ vagrant init后,在工做目录中,你会发现此文件。


NOTE:配置版本说明:

 


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保持向后兼容,也大幅引入新的功能和配置选项。


配置网络(本文将提供2种版本的经常使用配置,其中版本1的配置通过实践验证)

(1) 端口转发:(假设虚拟机的80端口提供web服务,此处将经过访问物理机的8080端口转发到虚拟机的80端口,来实现web的访问)

 版本"2":

 


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

 

 版本"1"

 


Vagrant::Config.run  do   |config|
   # Forward guest port 80 to host port 8080
   config.vm.forward_port 80, 8080
end

 

 (2)桥接网络(公共网络,局域网DHCP服务器自动分配IP)

  版本"2"

 


Vagrant.configure( "2" )  do   |config|
   config.vm.network :public_network
end

 

 

  版本"1"


Vagrant::Config.run  do   |config|
   config.vm.network :bridged
end

  $ VBoxManage list bridgedifs | grep ^Name    #可经过此命令查看本机的网卡

    Name:            eth0

  指定网卡,配置可写为以下:


Vagrant::Config.run  do   |config|
   config.vm.network :bridged, :bridge =>  "eth0"
end

 

 

  (3) 私有网络:容许多个虚拟机经过主机经过网络互相通讯,vagrant容许用户分配一个静态IP,而后使用私有网络设置。

  版本"2"

 


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

 

  版本"1"

 


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


同步文件夹

默认的,vagrant将共享你的工做目录(即Vagrantfile所在的目录)到虚拟机中的/vagrant,因此通常不需配置便可,如你须要可配置:

版本"2"

 


Vagrant.configure( "2" )  do   |config|
   # other config here
   config.vm.synced_folder  "src/" ,  "/srv/website"
end

 

  "src/":物理机目录;"/srv/website"虚拟机目录

vagrant和shell(实如今虚拟机启动的时候自运行须要的shell命令或脚本)

 版本"2"

 内嵌脚本:

 


Vagrant.configure( "2" )  do   |config|
   config.vm.provision :shell,
     :inline =>  "echo Hello, World"
end

 

 复杂点的调用以下:

 


$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT
Vagrant.configure( "2" )  do   |config|
   config.vm.provision :shell, :inline => $script
end

 外部脚本:

 


Vagrant.configure( "2" )  do   |config|
   config.vm.provision :shell, :path =>  "script.sh"        #脚本的路径相对于项目根,也可以使用绝对路径
end

 

 

 脚本可传递参数:

 


Vagrant.configure( "2" )  do   |config|
   config.vm.provision :shell  do   |s|
     s.inline =  "echo $1"
     s.args   =  "'hello, world!'"
   end
end

 

 版本"1":

 内部脚本:

 


Vagrant::Config.run  do   |config|
   config.vm.provision :shell, :inline =>  "echo abc > /tmp/test"
end

 

 外部脚本:

 


Vagrant::Config.run  do   |config|
   config.vm.provision :shell, :path =>  "test.sh"
end

 

 脚本参数:

 


Vagrant::Config.run  do   |config|
   config.vm.provision :shell  do   |shell|
     shell.inline =  "echo $1 > /tmp/test"
     shell.args =  "'this is test'"
   end
end

 

vagrant和puppet(若是不知道puppet,请看这里http://xuclv.blog.51cto.com/blog/5503169/1154261)

(1) vagrant调用puppet单独使用

 


Vagrant.configure( "2" )  do   |config|
   config.vm.provision :puppet  do   |puppet|
     puppet.manifests_path =  "my_manifests" #路径相对于项目根,如无配置此项,默认为manifests
     puppet.manifest_file =  "default.pp"        #如无配置此项,默认为default.pp
     puppet.module_path =  "modules"          #路径相对于根
     puppet.options =  "--verbose --debug"
   end
end

 

 默认配置的目录结构:

 $ tree

  .

  |-- Vagrantfile

  |-- manifests

  |   |-- default.pp

(2) vagrant让puppet做为代理,链接Puppet master

 


Vagrant.configure( "2" )  do |config|
config.vm.provision :puppet_server  do |puppet|
puppet.puppet_server =  "puppet.example.com" #master域名
puppet.puppet_node =  "node.example.com" #传递给puppet服务器节点的名称。默认为”puppet“
puppet.options =  "--verbose --debug" #选项
end
end

 NOTE:

 版本1配置差异不大,再也不详述,区别:Vagrant.configure("2") do |config|改成Vagrant::Config.run do |config|

 以上Vagrantfile配置完毕后,可$ vagrant reload 重启虚拟机以来实现配置生效

 

 

 

官方给了一个例子(可尝试玩玩):

1.进入工做目录

2.修改Vagrantfile

  $ vim Vagrantfile    #启用或添加以下行:

 


Vagrant.configure( "2" )  do   |config|
   config.vm.provision :puppet     #这里没有配置pp文件等的路径,所有采用默认
   end
end

 3.建立puppet的主目录

  $ mkdir manifests

 4.配置pp文件

  $ vim manifests/default.pp

 

# Basic Puppet Apache manifest

class apache {

   exec {  'apt-get update' :
     command =>  '/usr/bin/apt-get update'
   }
   package   {  "apache2" :
     ensure => present,
   }
   service {  "apache2" :
     ensure => running,
     require => Package[ "apache2" ],
   }
   file {  '/var/www' :
     ensure => link,
     target =>  "/vagrant" ,
     notify => Service[ 'apache2' ],
     force  =>  true
   }
}
include   apache


 

5.重启虚拟机

 $ vagrant reload    #重启后可看到虚拟机中已经安装好了apache

 

后记:

总的来讲vagrant仍是一个简单好用的软件,经常使用于和puppet或者chef结合,实现测试环境的自动化部署,保证了测试环境的快速建立,便捷部署,一致性,同时也便于销毁。另,这里不经常使用chef,因此此篇文章不对其进行介绍,有兴趣的能够自行研究.


推荐一个shell集成安装环境,lnmp/lamp等等

https://oneinstack.com/install/

相关文章
相关标签/搜索