puppet 由于涉及到证书的问题,在安装时要确保域名解析的正确,这个通过 dns 以及 hosts 都可以实现。比较方便的方式是将所有的主机的 ip/domain 都写到 /etc/hosts 里面,下面三行是标准的 hosts 文件的写法:
#ip hostname.domainname hostname
127.0.0.1 localhost.localdomain localhost
192.168.101.10 master.example.com master
192.168.101.30 agent.example.com agent
同时要注意 /etc/resolv.conf 里面的 search 指令,要么要跟 hosts 文件的 domain name 一致,要么直接注释掉,使用下面命令生效或者重启:
# hostname -F /etc/hostname
查看 puppet master/agent 的版本:
# puppet --version
# puppetmaster --version
使用 yum 安装需要使用 epel 源。 需要注意的是,puppetmaster 的版本需要大于等于客户端,否则二者通信会有问题。安装之前 apt-cache policy/yum info 确认一下版本。
Puppet Master:
# yum install puppet-server
# chkconfig puppetmaster on
# iptables -A INPUT -p tcp --dport 8140 -j ACCEPT
Puppet Client:
# yum install puppet
# chkconfig puppet on
Ubuntu 的包分别是 puppetmaster, puppet。
接着就要生成证书文件了。在客户端证书请求:
# puppetd --server master.example.com
服务端签名:
# puppetca -s agent.example.com
在签名前可以先确认下是否有请求:
# puppet -l
单机运行的,也就是不依赖 puppetmaster 的,这个用来调试是不错的,但是生产过程中,如果也这样就体现不出 puppet 的强大了。单机运行:
# puppet apply file.pp
常用的资源包括:file, package, service, user, group, exec
在使用服务端/客户端模式时,可以将所有的配置都以模块的方式来管理,写成类,这样比较容易维护。一个 puppetmaster 文件的基本结构如下,以安装 munin-node 为例:
|– fileserver.conf
|– manifests
| |– modules.pp
| |– nodes.pp
| `– site.pp
|– modules
| `–munin-node
| |– files
| |– manifests
| | |– init.pp
| |– templates
| |–munin-node.conf.erb
|– puppet.conf
|–auth.conf
$ cat /etc/puppet/manifests/site.pp
node default {
include munin-node
}
import "modules.pp"
$ cat /etc/puppet/manifests/modules.pp
import "munin-node"
$ cat /etc/puppet/modules/munin-node/manifests/init.pp
class munin-node{
package {
["munin-node"]:
#ensure => installed;
ensure => present;
}
file {
"munin-node.conf":
name => "/etc/munin/munin-node.conf",
ensure => present,
owner => root,
group => root,
mode => 0644,
content => template("munin-node/munin-node.conf.erb"),
require => Package["munin-node"];
}
service {
"munin-node":
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
require => Package["munin-node"],
subscribe => File["munin-node.conf"];
}
}
$ cat /etc/puppet/modules/munin-node/templates/munin-node.conf.erb
log_level 4
log_file /var/log/munin/munin-node.log
pid_file /var/run/munin/munin-node.pid
background 1
setsid 1
user root
group root
ignore_file ~$
ignore_file DEADJOE$
ignore_file \.bak$
ignore_file %$
ignore_file \.dpkg-(tmp|new|old|dist)$
ignore_file \.rpm(save|new)$
ignore_file \.pod$
allow ^127\.0\.0\.1$
allow ^111\.111\.111\.111$
host *
port 4949
上面就是一个模块的基本结构了,其实 munin-node.conf.erb 这个文件放在 files/ 目录下会更好。按我的理解,files/ 目录用来放“静态”的文件,而 templates/ 目录用来放含有变量的文件。
puppet 跟密钥相关的文件都在 /var/lib/puppet/ssl/ 目录下。 在大规模 Puppet 环境中使用 Apache+Passenger 代替 WEBrick。在启动 puppet/puppetmaster 时,可以通过 daemon 的方式,或者直接运行:
daemon:
# /etc/init.d/puppet start
# /etc/init.d/puppetmaster start
daemon 方式其实是一个 cron 任务,默认情况下,客户端会半小时向服务端发送一次请求,如文件有改动则执行。个人认为通过 no-daemon 方式更可控,并且在测试的时候也更加方便。
server:
# puppet master --no-daemonize --verbose --logdest(console, syslog, log file)
client:
# puppet agent --server master.example.com --no-daemonize --onetime
在安装至运行成功前可能会出现各种错误,大多是由于 hosts 配置错误,防火墙,selinux 引起的,出错注意检查这三项就可以了。
下面是遇到的一些错误。
1.Could not run: Could not create PID file: /var/lib/puppet/run/master.pid
# ps -aux | grep puppet
应该能看到相应的进程。如果以 daemonize 的方式启动,当然不能再以 no-daemonize 的方式启动了,杀死该进程重启。
2.info: Creating a new SSL key for client.puppet
err: Could not request certificate: No route to host – connect(2)
Exiting; failed to retrieve certificate and waitforcert is disabled
或者是
Could not find a default provider for user
不是防火墙就是 selinux 引起的,注意检查。
3.warning: peer certificate won't be verified in this SSL session
info: Caching certificate for client.puppet
err: Could not retrieve catalog from remote server: certificate verify failed
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run
c/s 时间同步问题:
# ntpdate ntp.ubuntu.com
4.在使用 no-daemonize 方式启动 puppetmaster 是,出现如下错误,这是个小 bug:
Removing mount files: /etc/puppet/files does not exist or is not a directory
$ sudo mkdir /etc/puppet/files
5.其余的一些问题基本都是证书引起的,终极解决办法是先吊销清除客户端的证书,然后在客户端删除生成的证书请求:
master:
# puppetca -c agent.example.com
# puppetca -r agent.example.com
client:
# mv /var/lib/puppet /tmp