puppet主要使用于自动化运维方面,对于拥有众多服务器需要管理的管理员来说,puppet
是个不错的选择,使其管理更加轻松,容易。其设计的是c/s建构,可以一个master和多个client。
通过master可管理配置客户端的文件、用户、cron任务、软件包、系统服务等。客户端默认半
小时向master请求一次以使其与master配置同步,从而使管理员在master上设置配置就可以实
现对于众多客户端机器的管理。
安装:
由于此软件提供了包管理器的安装方式,所以通过apt-get或者yum安装既可以了,本人使
用的linux mint,所以使用的前者。当然源码包安装也行啦,puppet用ruby开发的。
master:
sudo apt-get install puppetmaster
客户端:
sudo apt-get install puppet
安装后master服务程序已经运行了。如果没运行也可以通过
sudo service puppetmaster start
开启,打开的是8140端口。
下面针对一台电脑上来说,也就是即作为master也作为client。由于master已经启动,client端需要
通过认证才能与master通信。puppet程序的组织方式与git很类似——通过二级命令来使用的。
puppet master表示master服务,puppet agent是客户端程序。
首先运行如下类似的命令:
puppet agent --test --verbose --server=chaofan.io
--server后面跟的是master的主机名。然后:
puppet cert list --all
会显示所有客户端认证的信息,只有每行前面有加号的主机才是通过签名也就是可以master通信。
如果master与客户端在同一台机器上,主机名前面应该已有了加号。如果没有可以如下:
puppet cert --sign chaofan.io
由于本人master与客户端在同一台机器上,所以主机名一样。--sign后跟客户端的主机名。
如果再puppet cert list --all 输出的信息如下:
+ "chaofan.io" (38:A5:ED:2F:A9:71:D6:7F:34:77:E1:D0:9C:C1:B0:FE)
OK,现在就可以运行客户端服务了。如下:
sudo puppet agent --no-daemonize --verbose --server=chaofan.io
开始运行上面的命令会立即向master请求,获得master上设置好的信息。输出信息如下:
1 notice: Starting Puppet client version 2.7.182 info: Caching catalog for chaofan.io3 info: Applying configuration version '1389514190'4 notice: /Stage[main]//Exec[/bin/ls test.txt]/returns: executed successfully5 notice: Finished catalog run in 0.10 seconds
第一行有版本信息,注意第四行,这就是master上设定的资源。以下介绍master配置,
这也是puppet复杂而又强大的部分,puppet提供了一套语法供用户使用。如file、exec、
user等等类型。file可以把master上的文件传送到客户端上,exec执行某个命令,user可以
在客户端创建用户。master配置路径在/etc/puppet下,
16:23+~$$ ls /etc/puppet/auth.conf etckeeper-commit-pre manifests puppet.confetckeeper-commit-post fileserver.conf modules templates
下面简单介绍一下配置,manifests目录下创建一个site.pp文件(必须创建)。在site.pp里
写入:
exec { "/bin/ls test.txt": cwd= "/tmp"}
保存后,如果在客户端执行sudo puppet agent --no-daemonize --verbose --server=chaofan.io,
如果已经执行了那么半小时后就会有上文中说到的第四行信息。在客户端的/tmp路径下会看到test.txt
文件。
我们可以注意到资源的设定基本格式exec是类型,"/bin/ls test.txt"是title,然后下面是属性值的
设置,属性与值之间用"= "间隔,这里只加了cwd。可以通过:
puppet describe exec
查看exec的详细属性介绍,通过puppet describe --list或者puppet resource --types,可以得到所有类型。
上面在site.pp配置的仅仅是比较简单的,我们可以设置在客户端安装软件(puppet默认会使用系统的
包管理器,如ubuntu会使用apt-get)。我们也可以针对某个客户端做设置,这就要使用node类型。下面
给一个复杂的例子作为收尾吧。
我们在manifests下创建test.pp。打开test.pp写入:
1 class mysql::install { 2 package { ["mysql-server-5.5"]: 3 ensure = present, 4 require = User["mysql"], 6 user { "mysql": 7 ensure= present, 8 gid = "mysql", 9 shell = "/bin/false",10 require = Group["mysql"],12 group { "mysql":13 ensure = present,16 class mysql {17 include mysql::install19 node 'chaofan.io' {20 include mysql21 }
如上的设置并不是很复杂,主要目的就是向chaofan.io主机安装mysql并创建mysql用户和用户组。
class是puppet提供的一个类似c++类的功能,然后我们在site.pp中开头加入import "test.pp"
如果在客户端执行sudo puppet agent --no-daemonize --verbose --server=chaofan.io,
如果已经执行了那么半小时后就会输出如下:
1 notice: Starting Puppet client version 2.7.182 info: Caching catalog for chaofan.io3 info: Applying configuration version '1389517596'4 notice: /Stage[main]/Mysql::Install/Group[mysql]/ensure: created5 notice: /Stage[main]//Exec[/bin/ls test.txt]/returns: executed successfully6 notice: /Stage[main]/Mysql::Install/User[mysql]/ensure: created7 notice: /Stage[main]/Mysql::Install/Package[mysql-server-5.5]/ensure: created8 notice: Finished catalog run in 29.72 seconds
注意第4、6、7行。mysql已经安装完成了。
工作中有用到了puppet,在此也算记录一下。