IT技术博客大学习 共学习 共进步

如何有效运行puppet cron任务以及如何触发运行puppet

MySQLOPS 数据库与运维自动化技术分享 2012-02-07 23:18:19 浏览 3,762 次
[导读]

本次学习的内容是如何有效分发cron任务,有效分发,大家都懂的,以及如何解发更新puppet 动作,运维人员

都知道,通常配置文件变更,需要重新reload服务,以应用的新的配置文件.大家看过sky之前的文章,其中都有

讲得,具体还是来看puppet cookbook的示例吧!

[正文]

                                                              有效分发cron任务

当你有许多服务器,需要执行相同的cron作业,不在同一时间集中运行他们通常是个好
主意.如果作业是用来访问一个普通服务器,那么会给该服务器带来太多压力,即使他们
不想,那么所有服务器都在同一时刻等待,这可能会影响它提供其它服务的能力.

Puppet 的inline_template功能允许我们使用的Ruby的逻辑来根据主机名
来设置不同的运行时间.

怎么办呢…
1.添加以下内容到节点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
define cron_random( $command, $hour ) {
cron { $name:
command => $command,
minute => inline_template("<%= (hostname+name).hash.abs %60 %>"),
hour     => $hour,
ensure => "present",
}
}
cron_random { "hello-world":
command => "/bin/echo 'Hello world'",
hour => 2,
}
cron_random { "hello-world-2":
command => "/bin/echo 'Hello world'",
hour => 1,
}

2.运行Puppet:

1
# puppet agent --test

info: Retrieving plugin
info: Caching catalog for cookbook.bitfieldconsulting.com
info: Applying configuration version ’1305713506′
notice: /Stage[main]//Node[cookbook]/Cron_random[hello-world]/
Cron[hello-world]/ensure: created
notice: /Stage[main]//Node[cookbook]/Cron_random[hello-world-2]/
Cron[hello-world-2]/ensure: created
notice: Finished catalog run in 1.07 seconds

它是如何工作的

我们为每个cron作业选择了一个随机分钟数,也就是说,不是真正的随机(或者修改
Puppet的每次运行时间),但或多或少保证为每个不同的每个主机上的cron作业.

我们可以通过使用Ruby的哈希方法,计算任何数值对象,在这种情况下,一个字符串

该值每次是相同的,看起来那个值是是随机的,它不会改变Puppet 再次运行.

hash会生成一个大的整数,我们希望是从0-59之间,所以,我们使用了Ruby(%)(模数)
来运算并限制在此范围内的结果.虽然只有60个值,但是hash 函数的设计目的是统一
的生产尽可能的输出,所以,应该是很少的碰撞和分钟值应分布均匀.

我们想要在不同的机器上,根据不同的值来区分作业,因此我们使用主机名,以主机名
来计算哈希值.但是,但是,我们也希望在同一台机器上,不同的作业,hash值也是不同的.
所以我们结合了name变量.这将是cron的作业名称(例如Lhello-world).

还有更多…..

在这个例子中,我们只随机分钟的cron作业,并提供了小时的一部分定义.
如果你有时需要指定一周中的某一天,你也可以把它添加到cron_random,
你可以设置参数,以及指定默认值:

1
define cron_random( $command, $hour, $weekday = "*" ) {

如果你也想随机小时(例子如:在一天的任何埋单运行,需要均匀分配24小时),你
可以修改cron_random如下所示:

1
hour => inline_template("<%= (hostname+name).hash.abs % 24 %>"),

另请参阅:
从cron运行Puppet

                                       当一个文件更新时运行puppet

这是一个非常普遍的模式,当一个指定文件更新时,Puppet采取一些动作.例如 ,
在rsync配置文件添加了版片段,每个片段文件都调用exec来管理,当主配置文件(
rsync.conf)有改动时.

通常puppet每次运行时都会执行exec资源,除非你指定了以下某个参数:
creats
onlyif
unless
refreshonly => true

refreshonly参数意味着exec如果它收到从另一个资源的通知就运行.(例如,文件)
即可以这样理解,通知n次,就执行n次.

准备…

1.安装nginx 软件包

1
# apt-get install nginx

如何去做…
1.创建一个nginx模块,目录结构如下:

1
2
3
# mkdir /etc/puppet/modules/nginx
# mkdir /etc/puppet/modules/nginx/files
# mkdir /etc/puppet/modules/nginx/manifests

2.创建/etc/puppet/modules/nginx/manifests/init.pp文件,内容如下:

1
import "*"

3.创建/etc/puppet/modules/nginx/manifests/nginx.pp文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class nginx {
package { "nginx": ensure => installed }
service { "nginx":
enable => true,
ensure => running,
}
exec { "reload nginx":
command => "/usr/sbin/service nginx reload",
require => Package["nginx"],
refreshonly => true,
}
file { "/etc/nginx/nginx.conf":
notify => Exec["reload nginx"],
}
}

4.复制nginx.conf到新模块:

1
cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files

5.添加以下内容到节点

1
include nginx

6.从复制的nginx.conf文件做个测试,改变文件内容.

1
# echo \# >>/etc/puppet/modules/nginx/files/nginx.conf

7.运行Puppet:

1
# puppet agent --test

info: Retrieving plugin
info: Caching catalog for cookbook.bitfieldconsulting.com
info: Applying configuration version ’1303745502′
— /etc/nginx/nginx.conf
2010-02-15 00:16:47.000000000 -0700
+++ /tmp/puppet-file20110425-31239-158xcst-0
09:39:49.586322042 -0600
2011-04-25
@@ -48,3 +48,4 @@
#
#
proxy on;
}
# }
+#
info: FileBucket adding /etc/nginx/nginx.conf as {md5}7bf139588b5e
cd5956f986c9c1442d44
info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Filebucketed
/etc/nginx/nginx.conf to puppet with sum 7bf139588b5ecd5956f986c9c
1442d44
notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content:
content changed ‘{md5}7bf139588b5ecd5956f986c9c1442d44′ to ‘{md5}d
28d08925174c3f6917a78797c4cd3cc’
info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Scheduling
refresh of Exec[reload nginx]
notice: /Stage[main]/Nginx/Exec[reload nginx]: Triggered ‘refresh’
from 1 events
notice: Finished catalog run in 1.69 seconds

它是如何工作的…

对于大多数服务,你可以简单的定义了一个服务资源并从config文件获得通知.
这将导致Puppet会重启该服务以应用配置文件变化.但是,Nginx 有时不能正确
的重新启动,尤其是使用puppet来重新启动nginx,所以我从网站上做了个补救
措施,使用puppet来运行/etc/init.d/nginx reload来替代restart,这里的
它是如何工作的.

exec资源的refreshonly参数设置为true:

1
2
3
4
5
exec { "reload nginx":
command => "/usr/sbin/service nginx reload",
require => Package["nginx"],
refreshonly => true,
}

因此只要收到notfify,exec资源就运行.

配置文件资源提供必要的notify,如果配置文件有改动:

1
2
3
4
file { "/etc/nginx/nginx.conf":
notify => Exec["reload nginx"],
}

每当Puppet需要更新这个文件,它将运行exec,调用/usr/sbin/service nginx reload 来应用变化.

还有更多…
你可以使用相似的模式,每个资源更新,任何地方需要触发动作.
包括以下可能的用途:
触发服务重新加载
运行语法检查,然后重新启动服务
串联配置片段
运行测试
exec 链

当单个文件更新时,如果你有几个命令需要去执行,它需要在所有命令前都需要提供
subscribe 参数指向该文件.而不是文件的notfiy命令.但是效果是一样的.

[总结]

本小节文字虽然不多,但确实比较,大家可以举一返三的.puppet cookbook中文翻译文档

会陆续更新完成.puppet这工具有太多东西了,发现自己还有很多没有弄明白,庆幸的是

puppet 高级群里有热心的网友已经在翻译pro puppet pdf文档了,大家感兴趣的话,可以

关注下.

建议继续学习

  1. 使用Apache 和Passenger来运行puppetmaster (阅读 8,163)
  2. 自动化运维之企业实际案例分析 (阅读 4,622)
  3. puppet使用rsync来同步文件教程 (阅读 4,080)
  4. DevOps之Puppet (阅读 3,821)
  5. puppet vagrant 管理VirtualBox 虚拟机 (阅读 3,442)
  6. puppet运维之使用自定义函数 (阅读 3,301)
  7. puppet extlookup 和puppet hiera使用 (阅读 3,041)
  8. puppetmaster集群解决方案之puppet客户端共享一张证书 (阅读 2,780)
  9. puppet手册之建立软件安装源 (阅读 2,562)
  10. puppet 手册检查puppet配置文件和使用puppet tags (阅读 2,482)