puppet简介
puppet是一种Linux、Unix平台的集中配置管理系统,所谓配置管理系统,就是管理机器里面诸如文件,用户,进程,软件包这些资源,其设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系
puppet使用一种描述性语言来定义配置项,配置项中被称为”资源”,描述性语言可以声明你的配置的状态---比如声明一个软件包应该被安装或者一个服务应该被启动
用puppet,可以运行一个服务器端,然后每个客户端通过ssl证书连接服务器,得到本机器的配置列表,然后更加列表的来完成配置工作,所以如果硬件配置好,在一天之内配置好上千上万台机器是很容易实现的事情,前提得大部分机器配置类似
在大规模的生成环境中,如果只有一台puppetmaster会忙不过来的,因为puppet是用ruby写的,ruby是解析型语言,每个客户端来访 问,都要解析一次,当客户端多了就忙不过来,所以需要扩展成一个服务器组。puppetmaster可以看作一个web服务器,实际上也是由ruby提供 的web服务器模块来做的。因此可以利用web代理软件来配合puppetmaster做集群设置
puppe项目主要开发者是Luke Kanies,目前是puppet labs CEO,puppet遵循GPLv2版权协议。从1997年开始Kanies参与UNIX的系统管理工作,Puppet的开发源于这些经验。因为对已有的配置工具不甚满意,从2001年到2005年间,Kanies开始在Reductive实验室从事工具的开发。很快,Reductive实验室发布了他们的旗舰产品——
来张牛人靓照
与Luke Kanies谈Puppet工具:http://article.yeeyan.org/view/neilalaer/4629
puppet 系统架构
Puppet是开源的基于Ruby的系统配置管理工具,puppet是一个C/S结构, 当然,这里的C可以有很多,因此,也可以说是一个星型结构. 所有的puppet客户端同一个服务器端的puppet通讯. 每个puppet客户端每半小时(可以设置)连接一次服务器端, 下载最新的配置文件,并且严格按照配置文件来配置服务器. 配置完成以后,puppet客户端可以反馈给服务器端一个消息. 如果出错,也会给服务器端反馈一个消息. 下图展示了一个典型的puppet配置的数据流动情况
puppet工作流程
1) 客户端通过facter收集客户端信息并发送至服务端
2) 连接服务端并请求catalog日志
3) 请求节点(node)的信息
4) 从服务器端接收节点(node)的实例
5) 编译代码(包括语法检查等工作)
6) 查询是否有exported 虚拟资源
7) 如有,则从数据库接收虚拟资源
8) 接收完整的catalog日志
9) 存储catalog日志到数据库
10) 客户端接收完整的catalog日志
实验效果图:
实验环境:
puppetmaster 192.168.50.55 server.viong.com
puppet 192.168.50.56 client1.viong.com
centos x64 5.5 最小化安装+ Developtool
安装步骤:
由于centos最小化安装,需要yum一些常用工具
# yum install ntp vixie-cron wget vim-enhanced telnet
安装NTP同步时间,统一master和client上的时间
crontables(用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序)默认是安装的,可vixie-cron软件包是cron的主程序
wget下载安装文件
VI只默认安装了vim-minimal-7.x,所以无论是输入vi或者 vim查看文件,syntax功能都无法正常启用。因此需要用yum安装另外两个组件:vim-common-7.x和vim-enhanced- 7.x ,yum vim-enhanced-会自动下载关联vim-common
telnet只安装client,便于测试网络连通性
[root@puppet soft]# chkconfig --level 35 ntpd on
[root@puppet soft]# crontab -e
# 加入这一行:
10 5 * * * root /usr/sbin/ntpdate time.nist.gov ; /sbin/hwclock –w
#每天凌晨5点10分同步time.nist.gov,并将 Linux 时间写入 BIOS时
[root@localhost ~]# service crond restart
[root@localhost ~]# ntpdate pool.ntp.org; hwclock –w
29 Feb 15:22:47 ntpdate[15867]: step time server 196.25.1.1 offset 98.750417 sec
Puppet 要求所有机器有完整的域名(FQDN),如果没有 DNS 服务器提供域名的话,可以在两台机器上设置主机名(注意要先设置主机名再安装 Puppet,因安装 Puppet 时会把主机名写入证书,客户端和服务端通信需要这个证书):
[root@puppet puppet-2.7.14]#echo "192.168.50.56 client1.viong.com" >>/etc/hosts
[root@puppet puppet-2.7.14]#hostname server.viong.com
[root@puppet puppet-2.7.14]# vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=server.viong.com
GATEWAY=192.168.50.1
--------------------------------------------------------------------------------------------------默认DNS设置过,个别童鞋有报错无法认证记得查询一下
[root@puppet puppet-2.7.14]#vi /etc/resolv.conf
; generated by /sbin/dhclient-script
#search localdomain #注释掉这行,不然造成后面无法认证
nameserver 8.8.8.8
修改完以上文件,退出重新登录
--------------------------------------------------------------------------------------------------
安装ruby
由于puppet是由ruby语言编写,所以要安装ruby环境及库文件,命令帮助文件
[root@puppet soft]# yum install ruby ruby-libs ruby-rdoc
安装facter
facter是一个系统盘点工具,收集主机的一些资料,比如CPU,主机IP等,它收集到值发送给puppet服务器端,服务器端就可以根据不同的条件来对不同的节点机器生成不同的puppet配置文件
安装puppet之前必须先安装facter
[root@puppet soft]# tar -zxvf facter-1.6.8.tar.gz
[root@puppet soft]# cd facter-1.6.8
[root@puppet facter-1.6.8]# ruby install.rb
安装puppet
[root@puppet facter-1.6.8]# cd ..
[root@puppet soft]# wget http://downloads.puppetlabs.com/puppet/puppet-2.7.14.tar.gz
[root@puppet soft]# tar -zxvf puppet-2.7.14.tar.gz
[root@puppet soft]# cd puppet-2.7.14
[root@puppet puppet-2.7.14]# ruby install.rb
复制配置文件
[root@puppet puppet-2.7.14]# cp conf/redhat/fileserver.conf /etc/puppet/
[root@puppet puppet-2.7.14]# cp conf/redhat/puppet.conf /etc/puppet/
[root@puppet puppet-2.7.14]# cp conf/redhat/server.init /etc/init.d/puppetmaster
设置puppetmaster为服务,并自动启动
确认puppetmaster是否有执行权限
[root@puppet puppet-2.7.14]# ll /etc/init.d/puppetmaster
-rwxr-xr-x 1 root root 3936 06-05 18:19 /etc/init.d/puppetmaster
[root@puppet puppet-2.7.14]# chkconfig --add puppetmaster
[root@puppet puppet-2.7.14]# chkconfig --level 35 puppetmaster on
创建puppet帐号
[root@puppet puppet-2.7.14]# puppetmasterd –mkusers
1)确认是否生成清单文件夹
[root@puppet soft]# ll /etc/puppet/
总计 32
-rw-r--r-- 1 root root 2552 06-05 01:29 auth.conf
-rwxr-xr-x 1 root root 381 2012-06-05 fileserver.conf
drwxr-xr-x 2 root root 4096 2012-06-05 manifests
-rwxr-xr-x 1 root root 853 2012-06-05 puppet.conf
manifests目录:用来存放puppet的脚本文件的,必须包含init.pp文件,通常情况下可以把脚本都写入到这个一个文件中,但为了便于管理,最好是按照功能或者项目分开存放
2)确认系统生成puppet用户
[root@puppet puppet-2.7.14]# cat /etc/passwd |grep puppet
puppet:x:500:500::/home/puppet:/bin/bash
3)保证/var/lib/puppet/rrd目录存在且属主是puppet
[root@puppet puppet-2.7.14]# ll /var/lib/puppet/
总计 72
drwxr-x--- 2 puppet puppet 4096 06-05 18:49 bucket
drwxr-xr-x 2 root root 4096 06-05 18:49 facts
drwxr-xr-x 2 root root 4096 06-05 18:49 lib
drwxr-x--- 2 puppet puppet 4096 06-05 18:49 reports
drwxr-x--- 2 puppet puppet 4096 06-05 18:49 rrd
drwxr-x--- 2 puppet puppet 4096 06-05 18:49 server_data
drwxrwx--x 8 puppet root 4096 06-05 18:50 ssl
drwxr-xr-t 2 root root 4096 06-05 18:49 state
drwxr-x--- 2 puppet puppet 4096 06-05 18:49 yaml
查看是否安装成功
[root@server ~]# netstat -ntpl|grep 8140
tcp 0 0 0.0.0.0:8140 0.0.0.0:* LISTEN 2993/ruby [root@server ~]# ps -ef|grep puppet|grep -v grep
puppet 2993 1 0 10:57 ? 00:00:00 /usr/bin/ruby /usr/sbin/puppetmasterd
iptable安全设置
默认iptable配置如下:
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
修改为:
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8140 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
客户端
[root@puppet puppet-2.7.14]# echo "192.168.50.55 server.viong.com" >>/etc/hosts
[root@puppet puppet-2.7.14]#hostname client1.viong.com
[root@puppet puppet-2.7.14]# vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME= client1.viong.com
GATEWAY=192.168.50.1
在客户端安装ruby facter puppet的步骤参考服务端配置
特别说明:请注意客户端和服务器端版本要一致。如果版本不一致的话,那么高版本的只能是puppet server,另一台只能作为puppet客户端,也就是说puppet 服务端的版本可以大于或者等于客户端版本,不可以小于
其中区别一些如下:
[root@puppet puppet-2.7.14]# cp conf/redhat/client.init /etc/init.d/puppet
[root@puppet puppet-2.7.14]# chkconfig --level 35 puppet on
[root@puppet puppet-2.7.14]# puppetd --mkusers
Could not prepare for execution: Got 1 failure(s) while initializing: change from absent to present failed: Could not create user puppet: Execution of '/usr/sbin/useradd -g puppet -M puppet' returned 6: useradd: unknown group puppet
#手动创建puppet用户与组
[root@puppet puppet-2.7.14]# groupadd puppet;useradd -g puppet -M puppet
[root@puppet puppet-2.7.14]# service puppet start
测试解析与puppetmaster端口是否畅通
[root@puppet puppet-2.7.14]# telnet server.viong.com 8140
Trying 192.168.50.55...
Connected to server.viong.com.
Escape character is '^]'.
Connection closed by foreign host
[root@client1 puppet-2.7.14]# puppetd --test --server server.viong.com
info: Creating a new SSL key for client1.viong.com
warning: peer certificate won't be verified in this SSL session
info: Caching certificate for ca
warning: peer certificate won't be verified in this SSL session
warning: peer certificate won't be verified in this SSL session
info: Creating a new SSL certificate request for client1.viong.com
info: Certificate Request fingerprint (md5): 6F:6A:45:D9:14:28:D1:5B:05:3B:4F:0B:9A:51:D1:71
warning: peer certificate won't be verified in this SSL session
warning: peer certificate won't be verified in this SSL session
warning: peer certificate won't be verified in this SSL session
Exiting; no certificate found and waitforcert is disabled
# puppetd --test --server server.viong.com命令是指puppetd 从 server.viong.com去读取
puppet配置文件. 第一次连接,双方会进行ssl证书的验证,这是一个新的客户端,在服务器端那里还没有被认证,因此需要在服务器端进行证书认证
以下这步批准证书是在服务端操作
查看当然待批准证书列表
[root@server ~]# puppetca –l
client1.viong.com (B0:85:72:E6:7D:63:EA:CC:BD:0C:E4:F1:70:89:24:70)
批准当前证书
[root@server ~]# puppetca -s client1.viong.com
notice: Signed certificate request for client1.viong.com
notice: Removing file Puppet::SSL::CertificateRequest client1.viong.com at
'/var/lib/puppet/ssl/ca/requests/client1.viong.com.pem'
查看验证签名,注意前面的+号,说明已经签名
[root@server ~]# puppetca -a --list
+ server.viong.com (43:33:D3:A0:26:C4:E9:89:66:A6:DC:54:20:90:E9:4D) (alt names:
DNS:puppet, DNS:puppet.viong.com, DNS:server.viong.com)
--------------------------------------------------------------------------------------------------
如果要批准全部证书
puppetca -s -a
也可以在puppetmaster端的puppet.conf加入这行:
autosign = true
服务端就自动签证书
--------------------------------------------------------------------------------------------------
回到客户端操作,从服务端取回已批准的证书
[root@client1 puppet-2.7.14]# puppetd --test --server server.viong.com
warning: peer certificate won't be verified in this SSL session
info: Caching certificate for client1.viong.com
info: Caching certificate_revocation_list for ca
info: Caching catalog for client1.viong.com
info: Applying configuration version '1338897814'
info: Creating state file /var/lib/puppet/state/state.yaml
notice: Finished catalog run in 0.02 seconds
验证证书是否正确
服务端:
[root@server ~]# md5sum /var/lib/puppet/ssl/ca/signed/client1.viong.com.pem
8529a6f2d42c1b492c016fe870b744b6 /var/lib/puppet/ssl/ca/signed/client1.viong.com.pem
客户端:
[root@client1 puppet-2.7.14]# md5sum /var/lib/puppet/ssl/certs/client1.viong.com.pem
8529a6f2d42c1b492c016fe870b744b6 /var/lib/puppet/ssl/certs/client1.viong.com.pem
------------------------------------------------------------------------------------------------
出现修改主机名问题引起无法认证,需要重新申请证书,操作以下两个步骤:
服务端:
[root@server ca]# rm -rf /var/lib/puppet/ssl/ca/signed/client1.viong.com.pem
客户端:
[root@client1 puppet-2.7.14]# rm -rf /var/lib/puppet/ssl/
------------------------------------------------------------------------------------------------
功能测试
服务端:
建立pp文件测试
puppet的第一个执行的代码是在/etc/puppet/manifest/site.pp
因此这个文件必须存在,而且其他的代码也要通过代码来调用.
[root@server ~]# vim /etc/puppet/manifests/site.pp
node default {
file {"/tmp/viong.txt":
content=>"good,test pass!\n";}
上面的代码对默认连入的puppet客户端执行一个操作,在/tmp目录生成一个viong.txt文件,内容是good,test pass! 并自动回车换行
初次创建pp文件,需要重启puppetmaster
[root@server ~]# service puppetmaster restart
停止 puppetmaster: [确定]
启动 puppetmaster: [确定]
客户端:
[root@client1 puppet-2.7.14]# puppetd --test --server server.viong.com
info: Caching catalog for client1.viong.com
info: Applying configuration version '1338897814'
notice: /Stage[main]//Node[default]/File[/tmp/viong.txt]/ensure: defined content as '{md5}9a3f5438e1d35a72d853974203de4254'
notice: Finished catalog run in 0.03 seconds
成功的话就会显示puppet反馈一些调试信息,并在/tmp目录下创建了 /tmp/viong.txt文件.
[root@client1 puppet-2.7.14]# cat /tmp/viong.txt
good,test pass!
==============================================================
设置客户端的守护进程
[root@client1 puppet-2.7.14]# puppetd --test --server server.viong.com --verbose --waitforcert 100
info: Caching catalog for client1.viong.com
info: Applying configuration version '1338897814'
notice: Finished catalog run in 0.03 seconds
--server 服务端FQDN –-verbose 输出冗余信息 –-waitforcert 超时100
本人觉得使用cron或者手动运行puppet比较符合需求,或者以脚本方式,当master有改变再执行编写的脚本
先整理到这里,资源管理这块涉及到很多内容需要花时间慢慢咀嚼.
整理一篇文章挺辛苦,这次发布遇到一些问题,谢谢邵程程帮忙解决了,有问题请大家提出来,谢谢
参考文档:
http://www.slideshare.net/myw58/puppet-7159486
爱好puppet研究可以加sky 发布 puppet QQ群:179750784