您的位置:首页 > 运维架构

10分钟带你光速入门运维工具之-Puppe

2017-01-08 10:12 246 查看
    一、简介

    当服务器数量达到一定的规模时,仅依靠人为完成批量部署服务器个资源的配置,运维工作将变得繁琐且容易出错,为了解决这一问题,我们应该怎么办呢?我们可以引入一批工具,这批工具可编写相应的manifests代码,运行它便可以自动完成所有的工作,目前比较流行的运维工具主要有:puppet,ansible,
slackstack等,在这我们主要以puppet来展开话题。在一些大型互联网企业中,运维自动化管理着几百甚至上千台服务器,它可以针对多台服务器进行统一操作,例如部署统一软件、进行统一上线维护等,而且能够快速完成上线部署,减少人力及人力误操作风险。

    二、Puppet的工作原理 
 

    puppet的目的是让系统管理员只集中于要管理的目标服务器,而忽略实现的细节。puppet既可以在单机上使用,也可以C/S结构使用,在大规模部署puppet的情况下,通常我们会使用C/S结构,在这种结构下,服务端运行puppet-master程序客户端运行puppet-client服务程序

具体的工作流程图如下所示:

对于puppet的的掌握,理解puppet的工作原理是一个必要的的阶段,只有在了解了puppet的工作原理后才能更好应用puppet,下面让我们一起了解学习puppet的工作原理:

说到puppet的工作原理,不得不从以下四个方面来说到,如下所示:

(1)定义:使用Puppet特定的语言定义基础配置信息。通常我们把这些信息写在Modules中。

(2)模板:在配置执行之前检测代码,但并不真正执行。

(3)执行:定义的配置自动部署。检测并记录下所发生变化的部分。

(4)报告:将期待的变化、实际发生的变化及任何修改发送给报告系统。

如下所示为puppet的工作数据流示意图

数据流说明:

首先所有的节点(Node)Node节点将Facts和本机信息发送给Master

Master告诉Node节点应该如何配置,将这些信息写入Catalog后传给Node。

Node节点在本机进行代码解析验证并执行,将结果反馈给Master。

Master通过API将数据发给分析工具。报告完全可以通过开放API或与其他系统集成。

整个数据流的走向是基于SSL安全协议的,如下图所示:

模板文件处理过程说明如下:

Puppet通过编译Manifest中的内容 (即模板中内容),将编译好的代码存入Catalog。在执行前先进行代码的验证,再执行,完成最开始所定义好的状态。代码编译过程如图所示:

如下所示为整个puppet自动部署过程中agent和master的详细的交互过程:

过程说明:

1. Puppet客户端Agent将节点名与facts信息发送给Master。

2. Puppet服务端Master通过分类判断请求的客户端是谁,它将要做什么。这个判断是通过中包含的配置文件定义的。

3. Puppet服务端Master将所需要的Class类信息进行编译后存入Catalog并发送给Puppet客户端Agent,到此完成第一次交互。

4. Puppet客户端Agent对Catalog进行代码验证(语法检查及错误检查)并执行。主要是代码的验证,并将执行过程的信息及结果写入日志。

5. Puppet客户端Agent最终达到最开始所定义的状态,并且将结果及任何执行数据通过开放API的形式发送给Puppet服务端Master。

以上就是puppet的工作原理需要注意是:因为整个过程中都是基于ssl实现的,所以首要的是保证agent和master间可以基于ssl通讯!

以上内容参考:/6170059/1760439      

    三、puppet常用资源及配置实例

   实例一: 创建centos用户为普通用户,且uid为4000,gid为3000,所属组为centos,附加组为mygrp

 #此资源依赖于事先创建mygrps组
}
group {'mygrp':
  name    => 'mygrp',#定义组名
  ensure  => present, #创建组,与此相反的是absent,删除组
  gid     => '3000', #组id
  system  => false,   #是否为系统组,默认为false 
}
总结:
user常用属性:
name:用户名,uid:用户id,gid:组id,groups:附加组,comment:注释信息
expiry:过期时间,home:家目录,shell:默认的shell类型,system:是否为系统组,
ensure:创建或删除用户即present、absent,password:加密后的密码
group常用属性:  
ensure:创建或删除组即present、absent,name:组id,system:是否为系统组等
[root@node1 manifets]# puppet apply -v  #将此manifests清单编译为伪代码catalog,并执行,且返回执行结果至此单机模式的主机上
Notice: Compiled catalog for  in environment production in  seconds
Info: Applying configuration version '1480561275'
Notice: /Stage[main]/Main/Group[mygrp]/ensure: created
Notice: /Stage[main]/Main/User[centos]/ensure: created
Notice: Finished catalog run in  seconds
[root@node1 manifets]# id centos
uid=4000(centos) gid=4000(centos) groups=4000(centos),3000(mygrp)
[root@node1 manifets]# vi 
[root@node1 manifets]# puppet apply -v 
Notice: Compiled catalog for  in environment production in  seconds
Info: Applying configuration version '1480561376'
Notice: /Stage[main]/Main/Group[mygrp]/ensure: removed
Notice: /Stage[main]/Main/User[centos]/ensure: removed
Notice: Finished catalog run in  seconds
[root@node1 manifets]# id centos
id: centos: no such user
[root@node1 manifets]#

cron{'synctime':
  command   => '/usr/sbin/ntpdate  &>/dev/null', #须执行的命令
  ensure    => present,  #创建任务计划
  minute    => '*/5',    #过多长是时间执行
  user      => root,    #以谁的身份运行
}
[root@node1 testmanifests]# puppet apply -v 
Notice: Compiled catalog for  in environment production in  seconds
Info: Applying configuration version '1480593969'
Notice: /Stage[main]/Main/Cron[synctime]/minute: minute changed '*/3' to '*/5'
Notice: Finished catalog run in  seconds
[root@node1 testmanifests]# crontab -l
# HEADER: This file was autogenerated at 2016-12-01 20:06:10 +0800 by puppet.
# HEADER: While it can still be managed manually, it is definitely not recommended.
# HEADER: Note particularly that the comments starting with 'Puppet Name' should
# HEADER: not be deleted, as doing so could cause duplicate cron jobs.
# Puppet Name: synctime
*/5 * * * * /usr/sbin/ntpdate  &>/dev/null   #查看到此任务计划存在
[root@node1 testmanifests]#
case $osfamily {
"RedHat": { $webserver='httpd' }
/(?i-mx:debian)/: { $webserver='apache2' }
default: { $webserver='httpd' }
}
package{"$webserver":
ensure=> installed,
before=> [ File[''], Service['httpd'] ],
}
file{'':
ensure=> file,
path => '/etc/httpd/conf/',
source=> '/root/testmanifests/',
}
service{'httpd':
ensure=> running,
enable=> true,
restart => 'systemctl restart ',
subscribe=> File[''],
}
[root@node1 testmanifests]# puppet apply -v 
Notice: Compiled catalog for  in environment production in  seconds
Info: Applying configuration version '1480595667'
Info: Computing checksum on file /etc/httpd/conf/
Info: FileBucket got a duplicate file {md5}42566ec31df37e3d44429b285d015e1d
Info: /Stage[main]/Main/File[]: Filebucketed /etc/httpd/conf/ to puppet with sum 42566ec31df37e3d44429b285d015e1d
Notice: /Stage[main]/Main/File[]/content: content changed '{md5}42566ec31df37e3d44429b285d015e1d' to '{md5}3dfa14b023127a3766bddfe15fe14b9a'
Info: /Stage[main]/Main/File[]: Scheduling refresh of Service[httpd]
Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in  seconds
[root@node1 testmanifests]# #可根据自行的语言习惯选择适合的语法,如下所示同样可达到上诉的目的
$webserver = $osfamily ? {
"Redhat" => 'httpd',
/(?i-mx:debian)/ => 'apache2',
default => 'httpd',
}

package{"$webserver":
ensure  => installed,
before  => [ File[''], Service['httpd'] ],
}

file{'':
path    => '/etc/httpd/conf/',
source  => '/root/testmanifests/',
ensure  => file,
}

service{'httpd':
ensure  => running,
enable  => true,
restart => 'systemctl restart ',
subscribe => File[''],
}[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: