Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6941146
  • 博文数量: 701
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12021
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院架构师,专注企业数字化各个方面,MES/ERP/CRM/OA、物联网、传感器、大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(701)

分类: 系统运维

2015-08-30 11:20:55

运维及自动化部署的几个工具:puppet,docker,vargrant,chef 作为运维工程师都可以熟练应用。

1> 简介:

 vagrant提供了易于配置,重复性好,便携式的工作环境,这些对开发人员非常有用,它可以让开发人员可以创建简单且可重复使用的基于 VirtualBox的虚拟机(现在也支持VMware和AWS等),这些虚拟机可以快速的创建和销毁。vagrant也可以和puppet,chef等 结合,实现虚拟机管理的自动化。vagrant的官网: style="font-size:14px;font-family:黑体, simhei;">本文出自cclo的blog,转载时请务必以超链接形式标明文章原始出处:http://xuclv.blog.51cto.com/5503169/1239250


2> 安装:(OS:ubuntu12.04 vagrant:1.2.2)

 $ sudo apt-get install virtualbox

 $ sudo dpkg -i vagrant_1.2.2_x86_64.deb


 版本1.0.x也可以这样安装(OS:ubuntu12.04)

 sudo apt-get install vagrant

 或

 1:sudo apt-get install virtualbox

 2:sudo apt-get install ruby1.9.1 rubygems

 3:gem install vagrant


 NOTE:在物理机的基础上安装virtualbox,如果用vm创建的虚拟机中再安装virtualbox和vagrant,那么vagrant将起不来。这里下载相应的vagrant版本,注意1.0.x版本和1.1+版本的配置有较大不同,稍后在做介绍。


3> 一个简单的项目

 版本1.1+

 $ vagrant init precise32

 $ vagrant up


 版本1.0.x and 1.1+

 $ vagrant box add precise32

 $ vagrant init precise32

 $ vagrant up

 上述命令运行后,将有一个虚拟机VirtualBox运行Ubuntu 12.04 LTS 32位的系统。使用命令$ vagrant ssh进入该系统。


NOTE:"precise32"为虚拟机的名字,可以更改为你想用的名字。

      box是一个zip包,包含了vagrant的配置信息和VirtualBox的虚拟机镜像文件

      "为镜像所在的路径,可以为本地路径,所以建议将box下载下来后,指定为本地路径,速度会更快

      这里 style="font-size:14px;font-family:黑体, simhei;">有许多公共的base boxes可供下载和使用,后续将会介绍如何创建一个base box。


4> 常用的vagrant命令:

 $ 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                 #进入虚拟环境


5> Vagrantfile

 官方解释是这样的:The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines。翻译出来太生涩,简单来说就是配置这个虚拟主机网络连接方式,端口转发,同步文件夹,以及怎么和puppet,chef结合的一个配置 文件。执行完$ vagrant init后,在工作目录中,你会发现此文件。


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


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

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

 版本"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|
  # Forward guest port 80 to host port 8080
  config.vm.forward_port 80, 8080
end


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

  版本"2"

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

  版本"1"

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

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

    Name:            eth0

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

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


  (3) 私有网络:允许多个虚拟机通过主机通过网络互相通信,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


7> 同步文件夹

默认的,vagrant将共享你的工作目录(即Vagrantfile所在的目录)到虚拟机中的/vagrant,所以一般不需配置即可,如你需要可配置:

版本"2"

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

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


8> vagrant和shell(实现在虚拟机启动的时候自运行需要的shell命令或脚本)

 版本"2"

 内嵌脚本:

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

 复杂点的调用如下:

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

 外部脚本:

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

 脚本可传递参数:

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


 版本"1":

 内部脚本:

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

 外部脚本:

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

 脚本参数:

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


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

(1) vagrant调用puppet单独使用

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
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    #启用或添加如下行:

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

 3.创建puppet的主目录

  $ mkdir manifests

 4.配置pp文件

  $ vim manifests/default.pp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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,所以此篇文章不对其进行介绍,有兴趣的可以自行研究.


阅读(11959) | 评论(0) | 转发(4) |
给主人留下些什么吧!~~