设为首页 收藏本站
查看: 1651|回复: 0

[经验分享] Puppet: Puppet是如何找到你写在Manifests文件的配置的

[复制链接]

尚未签到

发表于 2015-11-26 15:25:38 | 显示全部楼层 |阅读模式
今天群里有人问了我问题,其实说的我挺晕,因为好多基础知识点都理解的有问题,或者说还没有仔细看文档。于是,我想在这里说说Puppet Master环境下,Master是如何查找你写的Manifests文件、模块和类并编译的。
限定:我们这次只讨论Master - Agent环境中Master在接受到一个主机的请求时的情况,而且也只讨论pp文件,也不涉及缓存的内容。

DSC0000.jpg

从上面的图,我们可以看到,Master接收到Agent的请求后,会查找自己的Manifests及相关的配置,并找到请求节点相关的内容,然后编译成Catalog,发回给Agent执行。这个过程大概有这样几个步骤:1. Master加载配置文件2. 查找节点定义,查看节点中声明的资源和类3. 查找到这些资源和类,编译生成Catalog

首先,我们先从配置文件说起,下面的URL是Puppet的配置说明页面。http://docs.puppetlabs.com/references/latest/configuration.html
其中有一个值叫manifests, 用途是指定Puppet Master的Manifests入口,意思是说Master是从这个文件开始读取Manifests的。manifest
The entry-point manifest for puppet master.

  • Default:$manifestdir/site.pp

这个参数的默认值是$manifestdir/site.pp,这里的$manifestdir是什么呢?它也是Puppet的一个配置项,默认值是$confdir/manifests。讨厌的是里面又有一个$confdir变量,同样它也是配置项,默认值是/etc/puppet
manifestdir
Where puppet master looks for its manifests.

  • Default:$confdir/manifests
confdir
The main Puppet configuration directory. The default for this setting is calculated based on the user. If the process is running as root or the user that Puppet is supposed to run as, it defaults to a system directory,but if it’s running as any other user, it defaults to being in the user’s home directory.

  • Default:/etc/puppet

所以Master默认情况下Manifests入口就是/etc/puppet/manifests/site.pp。它会读取这个文件来查找请求主机的节点的定义的类和资源。
一般情况下,我们建议让site.pp的内容为"import nodes/*.pp",这样,我们就在同级目录下创建一个nodes目录,并创建相应的pp文件来书写每个(组)节点的配置。
同样,我建议在节点pp文件中,只写节点有关的变量,具体的实现由加载不同的模块来完成。比如production.ppnodes /^web\d+\.xxx\.com$/ {    $www_home="/data/www"    include httpd    include httpd::vhost}这样是指定所有web开头的主机安装Apache应用,并指定了$www_home变量的内容。
而Puppet是如何去加载httpd的类的呢?
http://docs.puppetlabs.com/puppet/3/reference/modules_fundamentals.html这篇文章写了Puppet模块的基本原理,推荐阅读。在上面的配置列表中,我们可以查到模块默认可以存在$confdir/modules和/usr/share/puppet/modules中,一般情况下,我们是保存在/etc/puppet/modules下。模块中的结构如下 DSC0001.jpg
其中manifests/init.pp要求定义同模块名相同的类,然后其它的pp文件中应使用my_module::other_class形式的类名。原因是nodes中声明加载的类会被::打散,用来告诉自动加载器如何从模块中找到对应的文件。
When a class or defined resource is declared, Puppet will use its full name to find the class or defined type in your modules. Names are interpreted as follows:
当类或自定义资源类型被声明时,Puppet将使用全名来查找它们在你的模块中的位置。查找过程如下:

  • The first segment in a name (excluding the empty “top” namespace) identifies the module.Every class and defined type should be in its own file in the module’s manifests directory,and each file should have the .pp fileextension.
  • 名字中的第一节用来识别模块,每个类都应该在自己的文件夹下有一个manifests文件名字的pp文件
  • If there are no additionalnamespaces, Puppet will look for the class or defined type in the module’sinit.pp file.
  • Otherwise, Puppet will treat the final segment as the file name and any interior segments as a series of subdirectories under the manifests directory.
  • 如果类名只有第一节,那么Puppet将会找init.pp,如果有两节,将会查找第二节名字对应的pp文件,如果有3节则会查找第2节名字目录下的第3节名字的pp文件,以此类推。
Thus, every class or defined type name maps directly to a file path within Puppet’s modulepath:
如此,所有的类或自定义类型都对应能找到一个文件
namefile pathapache/apache/manifests/init.ppapache::mod/apache/manifests/mod.ppapache::mod::passenger/apache/manifests/mod/passenger.ppNote again that init.pp alwayscontains a class or defined type named after the module, and any other.pp filecontains a class or type with at least two namespace segments. (That is, apache.pp wouldcontain a class named apache::apache.)
记住只有init.pp中定义与模块名相同的类,其它的类名都至少有::分隔的两节。

由此,我们能看到,Puppet Master是首先加载site.pp,然后通过import nodes/*.pp来加载节点的定义,然后通过命名空间的自动加载来找到对应模块的类的定义。大概过程就是这样。

PS:Puppet对语法和manifests的检查没有那么的严格,比如说,你可以把你的配置都写进site.pp,也可以在init.pp中写多个类,但是为了管理方便,我还是建议你按照它的最佳实践来写。
多看文档,现在Puppet里还有不少坑没解决,多看才能尽量避开。例如,命名空间和自动加载文件里写了如下内容:
Importantnote: Earlier versions of Puppet used namespaces to navigate nested class/type definitions, and the code that resolves names still behaves as though this weretheir primary use.Thiscan sometimes result in the wrong class being loaded. This is a major outstanding design issue (issue#2053) which will not be resolved in Puppet 3. Seebelow for a full description of the issue.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-144003-1-1.html 上篇帖子: puppet核心资源类型及其常见属性学习笔记 下篇帖子: 【Puppet2D】实践1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表