欢迎光临
我们一直在努力

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

      特点:通过vagrant打包环境,可以跨平台使用。意思就是在windows下可以使用ubuntu系统配置的环境

使用的平台:windows+64位

需要准备的工具:

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

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


下载需要使用的 box

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


*一般操作命令

  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: 8080end

 

 版本”1″

 

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

 

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

  版本”2″

 

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

 

 

  版本”1″

Vagrant::Config.run do |config|  config.vm.network :bridgedend

  $ 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 = <<SCRIPTecho I am provisioning...date > /etc/vagrant_provisioned_atSCRIPTVagrant.configure("2") do |config|  config.vm.provision :shell, :inline => $scriptend

 外部脚本:

 

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!'"  endend

 

 版本”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'"  endend

 

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"  endend

 

 默认配置的目录结构:

 $ 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"#选项endend

 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文件等的路径,全部采用默认  endend

 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/




赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。