Puppet扩展篇1-自定义fact结合ENC(hirea)的应用实践

news/2024/7/3 18:38:35

零基础学习Puppet自动化配置管理系列文档

在大量节点加入Puppet之后,你至少会面临两个比较大的问题:

1、由于节点数的增多,site.pp文件必然会编写更多的节点条目,以及节点包含的类。假设你用Puppet管理500个节点,存在三种情况:1、所有节点有共同的类,也可以理解为模块;2、所有节点分成了50组,每组节点有不同的应用,每组应用编写一个模块;3、每个节点也应该有一个自己的模块,在这种环境下,可想而知,site.pp文件是多么的复杂,多么的难以维护。

2、在使用MCO推送更新的时候,后面可以跟上fact变量用于过滤一组节点。而官方给的facter只能满足普遍存在的fact变量,可是,你要对一组节点进行归类怎么办呢,比如有10个节点的应用是Apache,15个节点属于Nginx等,那么就要自定义fact变量。现在面临的问题是,所有节点都要自定义fact变量,又如何做到集中管理呢?

一、先来解决节点与类结合的问题

其实、节点与类之间的结合,更多时候是要借助ENC(外部节点分类器)来完成,因为puppet软件本身并不具备此类功能。Puppet-dashboard企业版和Foreman已经具备了这方面的功能,而且是图形界面操作,还是很方便的。众所周知,Puppet-dashboard社区版目前最新版本为1.2.23,到目前为止已经有一年多未更新了,可见Puppetlabs的意图很明确,逼着你用企业版。而Foreman部署和使用略复杂些,其次Foreman很重,它的内部并不仅仅是包含puppet还包含了很多其它管理工具,非常庞大,可以酌情考虑使用。接下来要介绍的是官方推荐的hiera软件,一个很强大的ENC,编写的文件只要遵循yaml格式即可,使用非常方便,可惜的是没有图形界面。但这并不代表你不会开发图形界面哦。

1、在Master端安装hiera工具

hiera工具和Puppetmaster结合需要通过hiera-puppet工具完成,在puppet-server 3.0.*版本后,这个工具已经集成到了puppet-server安装包中。而我们现在使用的环境仍然是puppet2.7版本,所以需要安装hiera-puppet工具。

[root@puppetmaster RHEL6U4]# yum install hiera hiera-puppet #注意依赖关系
...
Dependencies Resolved
===========================================================================
Package          Arch       Version                 Repository       Size
===========================================================================
Installing:
hiera            noarch     1.3.2-1.el6             rhel-puppet      23 k
hiera-puppet     noarch     1.0.0-1.el6             rhel-puppet      14 k
Installing fordependencies:
ruby-irb         x86_64     1.8.7.352-7.el6_2       rhel-base       311 k
ruby-rdoc        x86_64     1.8.7.352-7.el6_2       rhel-base       375 k
rubygem-json     x86_64     1.5.5-1.el6             rhel-puppet     763 k
rubygems         noarch     1.3.7-1.el6             rhel-puppet     206 k
Transaction Summary
===========================================================================
...

2、编辑hiera主配置文件hiera.yaml

2.1、创建软连接

默认hiera.yaml主配置文件在/etc目录下,为了结合后期版本控制系统集中管理,建议将此文件copy到/etc/puppet目录下,然后创建软连接指向/etc/hiera.yaml即可。

[root@puppetmaster ~]# mv /etc/hiera.yaml /etc/puppet/
[root@puppetmaster ~]# ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
[root@puppetmaster ~]# ll /etc/hiera.yaml
lrwxrwxrwx 1root root 22Apr 2020:05/etc/hiera.yaml -> /etc/puppet/hiera.yaml

2.2、编辑hiera.yaml

  • 添加全局变量common,注释掉defaults、global和clientcert。

  • 添加系统类型变量osfamily

  • 添加主机名变量hostname

  • 添加datadir路径位置,中间用了puppet环境变量,这里的环境变量和puppet应用的环境变量是一致的。如果你只有一种环境,只需要将其中变量去掉即可。

备注: 以上变量其实就是fact变量。

[root@puppetmaster ~]# vim /etc/puppet/hiera.yaml
---
:backends:
- yaml
:hierarchy:
#  - defaults
#  - "%{clientcert}"
- common
- "%{environment}"
- "%{osfamily}"
- "%{hostname}"
#  - global
:yaml:
:datadir:"/etc/puppet/environments/%{environment}/hiera"

hiera主配置文件编写完成之后,需要重启puppetmaster后方可生效。


[root@puppetmaster hiera]# /etc/init.d/puppetmaster restart
Stopping puppetmaster:                                     [  OK  ]
Starting puppetmaster:                                     [  OK  ]


2.3、创建变量common对应的文件

[root@puppetmaster hiera]# vim common.yaml
---
puppetserver:
- 'puppetmaster.kisspuppet.com'

通过hiera命令测试


[root@puppetmaster ~]# hiera puppetserver environment=kissprd
["puppetmaster.kisspuppet.com"]
[root@puppetmaster ~]# hiera puppetserver environment=kissdev
nil


通过以上命令可以得知在环境为kissprd的情况下,puppetserver的变量为puppetmaster.kisspuppet.com,值为nil的意思是不存在。

2.4、创建变量osfamily对应的文件

[root@agent1 ~]# facter osfamily
RedHat
[root@puppetmaster hiera]# vim RedHat.yaml
---
classes:
- 'puppet'
- 'yum'

通过hiera命令测试


[root@puppetmaster hiera]# hiera classes environment=kissprd
nil
[root@puppetmaster hiera]# hiera classes environment=kissprd osfamily=RedHat
["motd", "puppet", "yum"]
[root@puppetmaster hiera]# hiera classes environment=kissprd osfamily=SLES
nil


通过以上命令可以得在环境为kissprd,系统为RedHat的情况下,classes的变量为三个值(puppet、yum)。

2.5、创建变量hostname对应的所有节点文件

[root@agent1 ~]# facter hostname
agent1
[root@puppetmaster hiera]# vim agent1.yaml
---
classes:
- 'motd'
certname:
- 'agent1_cert.kisspuppet.com'
[root@puppetmaster hiera]# vim agent2.yaml
---
classes:
- 'motd'
certname:
- 'agent2_cert.kisspuppet.com'
[root@puppetmaster hiera]# vim agent3.yaml
---
certname:
- 'agent3_cert.kisspuppet.com'

通过hiera命令测试


[root@puppetmaster hiera]# hiera classes environment=kissprd hostname=agent
1
["motd"]
[root@puppetmaster hiera]# hiera classes environment=kissprd hostname=agent
2
["motd"]
[root@puppetmaster hiera]# hiera classes environment=kissprd hostname=agent
3
nil
[root@puppetmaster hiera]# hiera certname environment=kissprd hostname=agent1
["agent1_cert.kisspuppet.com"]
[root@puppetmaster hiera]# hiera certname environment=kissprd hostname=agent2
["agent2_cert.kisspuppet.com"]
[root@puppetmaster hiera]# hiera certname environment=kissprd hostname=agent3
["agent3_cert.kisspuppet.com"]


通过以上命令测试可以得知,系统fact变量hostname为agent1和agent2的情况下,hiera变量classes为motd。certname变量为各自的certname变量。

3、应用hiera变量于Puppetmaster

3.1、将hiera变量应用于site.pp文件中

以前的写法:

[root@puppetmaster ~]# vim /etc/puppet/environments/kissprd/manifests/site.pp
$puppetserver = 'puppetmaster.kisspuppet.com'
classenvironments{
includepuppet,yum
}
node 'puppetmaster_cert.kisspuppet.com'{
includeenvironments
}
node 'agent1_cert.kisspuppet.com'{
includeenvironments,motd
}
node 'agent2_cert.kisspuppet.com'{
includeenvironments,motd
}
node 'agent3_cert.kisspuppet.com'{
includeenvironments
}

应用了hiera之后的写法:


[root@puppetmaster ~]# vim /etc/puppet/environments/kissprd/manifests/site.pp
$puppetserver = hiera('puppetserver')  #引用了hiera中common.yaml中的全局变量puppetserver
node default{
hiera_include('classes')  #引用了hiera中osfamily和hostname变量
}


备注:以后添加节点,只需要编写yaml文件即可,而site.pp文件无需在进行修改。

3.2、将hiera变量应用于模块puppet中

未使用hiera之前的编写方式

[root@puppetmaster hiera]# vim /etc/puppet/environments/kissprd/environment/modules/puppet/manifests/params.pp
classpuppet::params {
$puppetserver = 'puppetmaster.kisspuppet.com'
$certname = "${::hostname}_cert.kisspuppet.com"
case$operatingsystemmajrelease{
5: {
$puppet_release = '2.7.23-1.el5'
$facter_release = '1.7.3-1.el5'
}
6: {
$puppet_release = '2.7.23-1.el6'
$facter_release = '1.7.3-1.el6'
}
default: {
fail("Module puppet is not supported on ${::operatingsystem}")
}
}
}

应用了hiera变量之后的编写方式


[root@puppetmaster hiera]# vim /etc/puppet/environments/kissprd/environment/modules/puppet/manifests/params.pp
classpuppet::params {
$puppetserver = hiera('puppetserver')  #应用了hiera全局文件common.yaml中的puppetserver变量
$certname = hiera('certname')  #应用了hiera中变量hostname对应的节点文件$hostname.yaml中的certname变量
case$operatingsystemmajrelease{
5: {
$puppet_release = '2.7.23-1.el5'
$facter_release = '1.7.3-1.el5'
}
6: {
$puppet_release = '2.7.23-1.el6'
$facter_release = '1.7.3-1.el6'
}
default: {
fail("Module puppet is not supported on ${::operatingsystem}")
}
}
}


4、测试

4.1、测试classes

wKiom1NTlcjjAV6pAAKZUI7BbXg874.jpg

wKioL1NTlZ6y6BnwAAF_ezwx_eI983.jpg


4.2、测试hiera变量certname和puppetserver

[root@agent3 ~]# vim /etc/puppet/puppet.conf
### config by  puppet ###
[main]
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true
[agent]
classfile = $vardir/classes.txt
localconfig = $vardir/localconfig
#    server = puppetmaster.kisspuppet.com   #注释掉进行测试
#    certname = agent3_cert.kisspuppet.com
runinterval = 10
[root@agent3 ~]# puppet agent -t --environment=kissprd --server=puppetmaster.kisspuppet.com --certname=agent3_cert.kisspuppet.com
info: Retrieving plugin
info: Loading facts in/var/lib/puppet/lib/facter/hwclock.rb
info: Caching catalog foragent3_cert.kisspuppet.com
info: Applying configuration version '1398008880'
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
--- /etc/puppet/puppet.conf    2014-04-2023:46:05.839948111+0800
+++ /tmp/puppet-file20140420-5618-45rytg-02014-04-2023:48:01.653944253+0800
@@ -7,6+7,6@@
[agent]
classfile = $vardir/classes.txt
localconfig = $vardir/localconfig
-#    server = puppetmaster.kisspuppet.com
-#    certname = agent3_cert.kisspuppet.com
+    server = puppetmaster.kisspuppet.com
+    certname = agent3_cert.kisspuppet.com
runinterval = 10
info: FileBucket adding {md5}13430e5962e7584c9422e5adc1f3ba43
info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Filebucketed /etc/puppet/puppet.conf to puppet withsum 13430e5962e7584c9422e5adc1f3ba43
notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: content changed '{md5}13430e5962e7584c9422e5adc1f3ba43'to '{md5}23545b7afd09af671920f122a20db952'
info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Scheduling refresh of Class[Puppet::Service]
info: Class[Puppet::Service]: Scheduling refresh of Service[puppet]
notice: /Service[puppet]: Triggered 'refresh'from 1events
notice: Finished catalog run in0.54seconds
[root@agent3 ~]# cat /etc/puppet/puppet.conf
### config by  puppet ###
[main]
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true
[agent]
classfile = $vardir/classes.txt
localconfig = $vardir/localconfig
server = puppetmaster.kisspuppet.com
certname = agent3_cert.kisspuppet.com
runinterval = 10

通过以上测试可以得知hiera变量certname和puppetserver传输正常。

特别说明: 由于hiera定义的变量需要通过转换才能变为puppet能够使用的变量,而这个转换是需要耗费CPU资源的,笔者曾经测试过一组节点(50个)同时传输hiera的变量数超过2000个,出现CPU负载过高的性能瓶颈问题需要特别注意。其次hiera数据除了用yaml格式保存外还可以存放在redis数据库中,这样查询起来性能会高很多,具体可参考官网,《pro puppet 》第二版也有这方面的介绍,可参阅。

通过以上测试至少解决了两个问题:

  • site.pp编写繁琐复杂的问题。

  • certname名定义问题(可以不适用其他fact变量进行定义,比如不再使用hostname变量,这样做的好处是即使节点hostname名变化也不会影响puppet通信)

二、集中管理自定义fact变量

思路: 参考Puppet基础篇10-自定义fact实现的四种方式介绍 在puppetmaster端编写一个facts模块,传输一个变量文件至节点对应的目录里面,变量文件名可以通过主机名进行定义。

1、在现有facts模块中直接添加

之前facts模块中的结构

[root@puppetmaster modules]# tree facts/
facts/
├── files
├── lib
│   └── facter
│       └── hwclock.rb   #通过pluginsync模式发布的自定义fact变量,无需修改
├── manifests
└── templates
5directories, 1file

2、添加管理file资源的pp文件

[root@puppetmaster manifests]# vim config.pp #定义file资源
classfacts::config{
file{ "/etc/facter/facts.d/$hostname.txt":   #文件名称通过变量hostname获取
owner   => "root",
group   => "root",
mode    => 0400,
source  => "puppet:///modules/facts/facts.d/$hostname.txt",  #文件名称通过节点变量hostname获取
require => Class['facts::exec'],
}
}
[root@puppetmaster manifests]# vim exec.pp  #定义可执行资源保证目录 /etc/facter/facts.d 存在
classfacts::exec{
exec {"create fact external":
command => "mkdir -p /etc/facter/facts.d ",
path    => ["/usr/bin","/usr/sbin","/bin","/sbin"],
creates => "/etc/facter/facts.d",
}
}
[root@puppetmaster manifests]# vim init.pp
classfacts{
includefacts::config,facts::exec
}

3、创建file资源对应的下载文件

[root@puppetmaster facts.d]# pwd
/etc/puppet/environments/kissprd/environment/modules/facts/files/facts.d
[root@puppetmaster facts.d]# vim agent1.txt
env=prd
app=weblogic
[root@puppetmaster facts.d]# vim agent2.txt
env=qa
app=db2
[root@puppetmaster facts.d]# vim agent3.txt
env=prd
app=nginx

4、应用模块facts至hiera中

由于模块facts属于全局的,应用于common.ymal或者RedHat.ymal中即可。

[root@puppetmaster hiera]# vim RedHat.yaml
---
classes:
- 'puppet'
- 'yum'
- 'facts'

5、节点测试

[root@agent3 ~]# ll /etc/facter/facts.d
ls: cannot access /etc/facter/facts.d: No such file or directory
[root@agent3 ~]# puppet agent -t --environment=kissprd
info: Retrieving plugin
info: Loading facts in/var/lib/puppet/lib/facter/hwclock.rb
info: Caching catalog foragent3_cert.kisspuppet.com
info: Applying configuration version '1398010573'
notice: /Stage[main]/Facts::Exec/Exec[create fact external]/returns: executed successfully
notice: /Stage[main]/Facts::Config/File[/etc/facter/facts.d/agent3.txt]/ensure: defined content as'{md5}3330b8efe95f6747de47a9eca3a5411e'
notice: Finished catalog run in0.66seconds
[root@agent3 ~]# cat /etc/facter/facts.d/agent3.txt
env=prd
app=nginx
[root@agent3 ~]# facter env
prd
[root@agent3 ~]# facter app
nginx

其它节点测试略

注意:以上方法只是提供给你一种集中管理自定义fact的思路,并不是最好的解决方案,只不过这种方法目前笔者用于上产环境中感觉还不错,特此分享。



http://lihuaxi.xjx100.cn/news/246864.html

相关文章

2021年开源SLAM算法集锦

1. TANDEM:Tracking and Dense Mapping in Real-time using Deep Multi-view Stereo 慕尼黑工业大学Daniel Cremers团队,实时单目跟踪稠密建图纯视觉SLAM,采用Realsense D455(深度传感器IMU,但只用RGB)。 项目地址:ht…

java连接数据库不使用框架_实体框架数据库连接不重新连接

我在Entity Framework中遇到了数据库连接问题 . 我在ASP.NET MVC 3 Web应用程序中使用EF 4.1.10331,但它也因EF 5而失败 .当我的Web应用程序启动时,由于某种原因SQL Server不可用,该网站抛出一个错误,这是好的 .当SQL Server再次可…

[MySQL]Every derived table must have its own alias

2019独角兽企业重金招聘Python工程师标准>>> 关于这条报错信息,意思是指每个派生出来的表都必须有一个自己的别名。 _mysql_exceptions.OperationalError: (1248, Every derived table must have its own alias) 如下两条select语句可以说明这个问题&am…

# 学号 2017-2018-20172309 《程序设计与数据结构》第十一周学习总结

---恢复内容开始--- 学号 2017-2018-20172309 《程序设计与数据结构》第十一周学习总结 教材学习内容总结 第23章 初识Android操作系统:一个多用户的Linux系统,一个运用程序运行时与其他的运用运行是独立的。发展:在Android4.4之前所有的应用…

outlook2010 打开总是提示“正在加载配置文件”

公司员工的电脑一打开OUTLOOK2010的时候,总是提示“正在加载配置文件”,要重新启动,还是无法打开,尝试了卸载和重装的方法也没有用.其实,只需下载一个windows search 4.0即可解决.转载于:https://blog.51cto.com/shidilun/1400227

Tesla AutoPilot纯视觉方案解析

Tesla在AI day上公布他们的视觉解决方案,其核心模板的名字也叫HydraNet,解决了实时感知问题。本文简要详细介绍一下 1. Tesla视觉系统的输入和输出 图一,Tesla视觉感知系统的输入和输出Tesla的视觉系统由8个摄像头环绕车身,视野范…

java 品尝饮料_那些年我喝过的饮料

印象中小的时候口渴总是很着急,而开水又凉得很慢,后来长大点,开始喝生水,原来家里没有水管的,我家有口大水缸,是石头凿的,平时父母打水来装满,里面放个水瓢,用盖子盖好。…

linux严谨的telnet搭建并用防火墙开通与禁行

今天本打算练练防火墙iptables的东西,本想拿telnet的23号端口练手,没想到在敲实验的时候遇到了些曲折,经过一番努力,把本次实验过程生成文档。实验环境:rhel5.6及系统盘实验目的:通过iptables对telnet服务2…