Puppet入门教程

    Puppet是一个为实现数据中心自动化管理而设计的配置管理软件。Puppet经过使用自有的puppet描述语言(与Ruby相似),可管理配置文件、用户、cron任务、软件包、系统服务等。在puppet中,这些实体统称为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。 html

     Puppet采用C/S星状的结构,全部的客户端和一个或几个服务器交互。每一个客户端周期的(默认半个小时)向服务器发送请求,得到其最新的配置信息,保证和该配置信息同步。当puppet服务启动时(如#service puppet start),每一个puppet客户端每半小时(能够设置,见本文“设置客户端自动同步时间间隔”一节)链接一次服务器端,下载最新的配置文件,而且严格按照配置文件来配置服务器。配置完成之后puppet客户端能够反馈给服务器端一个结果消息。 node

    Puppet的服务器端保存着全部的对客户端服务器的配置代码,在puppet里面叫作manifest. 客户端下载manifest以后,能够根据manifest对服务器进行配置,例如软件包管理、用户管理和文件管理等等。此外,puppet也能够单机方式工做,这时,至关于对一台机器的配置进行即时快照,便于后来还原各类配置(软件包、文件等),相似于系统还原、GHOST,只是更灵活些。
    相对于CFEngine、LCFG、Bcfg2,Puppet的优点是,免费而且开源、语法及功能丰富、用户群广大。
    Puppet采用Ruby语言编写,源码库: https://github.com/puppetlabs/puppet
    Puppet主页:http://puppetlabs.com/ linux

安装篇

OS:CentOS Linux 6.0 64-bit git

主机信息: github

角色 主机名称 IP
服务端 server.puppet 192.168.1.42
客户端 client.puppet 192.168.1.43

准备工做:

1.Puppet使用ssl认证来确保客户端与服务端的通讯,因此没用经过认证,双方没法工做。要求主机上有SSL软件包,这里是openssl。 shell

2.听说要在安装puppet软件前先设置主机名,由于生成证书的时候要把主机名写入证书,若是证书生成好了再改主机名就连不上。 另外主机名必须使用FQDN格式。
           (1)server.puppet上以下设置
            编辑/etc/sysconfig/network,把那行HOSTNAME=server.puppet
    
            编辑/etc/hosts,把那行127.0.0.1 以后加上server server.puppet
            添加一行 192.168.1.43 client.puppet
服务器

           (2)client.puppet上以下设置
            编辑/etc/sysconfig/network,把那行HOSTNAME=client.puppet
    
            编辑/etc/hosts,把那行127.0.0.1 以后加上client client.puppet
            添加一行 192.168.1.42 server.puppet session

    设置完成后两台主机互PING检查一下。 app

3.安装puppet软件 ide

# rpm -ivh /root/epel-release-6-5.noarch.rpm
warning: /root/epel-release-6-5.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
# yum install -y puppet
程序安装后主目录为/var/lib/puppet,而且默认生成配置目录:
/etc/puppet/auth.conf
/etc/puppet/puppet.conf 
/etc/sysconfig/puppet


测试一下安装状况:

# service puppet status
 puppetd 已停
  代表puppet安装成功。


4.在服务端和客户端分别执行时间同步

    #ntpdate time.nist.gov

5.防火墙与SELinux

   服务端防火墙TCP的8140端口须开放
   服务端须要关闭SELinux
   

运行篇

方式一:单机模式

直接在命令行执行一个写好的puppet文件,例如,新建一个文件在/puppet-demo/helloworld.pp(这种以pp后缀的文件在puppet中称做manifest文件),内容以下:
node default{
        file { "/puppet-demo/msrdok.txt":
                content => "hello,world",
                mode => 0644; }
}

上面代码的含义是在/puppet-demo/msrdok.txt位置建立文件,文件内容为"hello,world",文件权限为644。

运行

#puppet /puppet-demo/helloworld.pp
 notice: Finished catalog run in 0.01 seconds

而后查看/puppet-demo/msrdok.txt是否是被成功建立。
     采用这种方式,能够把一些经常使用的配置操做保存成puppet脚本,当有新的环境须要配置时,直接在新机器上执行 puppet脚本便可。

方式二:C/S模式

  1. server.puppet主机上

    确保puppet服务端正在运行

    # service puppet start

    下面安装puppet-server

    # yum install -y puppet-server 
    
    ......
    
    Installed:
      puppet-server.noarch 0:2.6.18-3.el6
    
    Complete!
     测试一下安装后效果
    # service puppetmaster status
    puppetmasterd 已停
    # service puppetmaster start

    安装完成后会在文件系统生成/etc/puppet/manifests目录和/etc/puppet/fileserver.conf文件,注意puppet的第一个执行的代码是在/etc/puppet/manifest/site.pp,当客户端链接到服务端时默认会去执行这个site.pp文件。所以这个文件必须存在。
    如今,创建一个最简单的site.pp文件,内容以下:

    node default{
        file {"/tmp/eye.txt": 
                    content => "1111,22";}
    }

    上面的代码对默认连入的puppet客户端执行一个操做,在/tmp目录生成一个eye.txt文件,内容是"1111,22"

  2. client.puppet主机上

    执行

# puppetd --server server.puppet --test
    info: Caching certificate for ca
    info: Creating a new SSL certificate request for server.puppet 
    info: Certificate Request fingerprint (md5): 3B:16:1D:11:01:3B:DE:2D:CE:B5:07:65:29:25:29:33
    Exiting; no certificate found and waitforcert is disabled

       出现这个错误缘由是:客户端未被服务端(主机server.puppet上)认证。 上面的命令让puppetd 从 server.puppet 去读取puppet配置文件. 第一次链接,双方会进行ssl证书的验证,这是一个新的客户端,在服务器端那里尚未被认证,所以须要在服务器端进行证书认证.在服务器端的机器上执行下面的命令来认证客户端的证书 

/**执行这句会获得待认证的客户端,与puppet cert list等效,若是不存在待认证的客户端,会出现 err: Could not call sign: Could not find certificate request for client.puppet**/ # puppetca -l  //等价于puppetca --list "client.puppet" (3B:16:1D:11:01:3B:DE:2D:CE:B5:07:65:29:25:29:33) # puppetca -s client.puppet //等价于puppetca --sign,对全部待认证客户端签名可以使用puppetca -s -a命令 

而后再从新在客户端执行  

# puppetd --server server.puppet --test
info: Caching certificate for client.puppet
info: Caching certificate_revocation_list for ca
info: Caching catalog for client.puppet
info: Applying configuration version '1383189866'
notice: /Stage[main]//Node[default]/File[/tmp/eye.txt]/ensure: defined content as '{md5}bd869b69868f59b21be375383a51841c'
info: Creating state file /var/lib/puppet/state/state.yaml
notice: Finished catalog run in 0.03 seconds
    
       也能够在服务端执行如下命令对客户端进行认证:

# puppet cert list
  "client.puppet" (3B:16:1D:11:01:3B:DE:2D:CE:B5:07:65:29:25:29:33)
  /**代表client.puppet主机未得到认证,下面的命令将进行认证。**/
  # puppet cert sign client.puppet
  notice: Signed certificate request for client.puppet
  notice: Removing file Puppet::SSL::CertificateRequest client.puppet at '/var/lib/puppet/ssl/ca/requests/client.puppet.pem'

        有时须要agent从新申请认证,以下操做:
        在服务端
        puppet cert clean client.puppet
        在客户端:
        rm -f /var/lib/puppet/ssl/ca/requests/client.puppet.pem
        puppetd --test --trace --debug

        看配置是否有异常 

此次终于执行成功,是客户端主机上查看/tmp目录,看是否是已经有个eye.txt文件了。

设置客户端自动同步时间间隔


默认状况下,客户端agent 每 30分钟会向 master 请求一次。若是咱们不但愿 agent 自动更新。
可采起如下任意方法:
(1)#puppet agent --server=192-9-117-162-app.com --test --no-client --listen
    #puppet agent --server=192-9-117-162-app.com --test --no-daemonize --onetime


(2)打开puppet服务所在文件,/etc/rc.d/init.d/puppet,找到
 [ -n "${PUPPET_SERVER}" ] && PUPPET_OPTS="--server=${PUPPET_SERVER} "
这一行,而后在最末尾添加--no-client 
添加后的效果:
[ -n "${PUPPET_SERVER}" ] && PUPPET_OPTS="--server=${PUPPET_SERVER} --no-client"
而后重启客户端puppet,/etc/init.d/puppet restart ,这样客户端就不会主动跟服务端同步了(记得把/etc/puppet/puppet.conf里runinterval注释掉)。
而后去服务端执行
#puppet kick -d --host 客户端主机名
便可实现只想推送功能。


(3)在/etc/sysconfig/puppet文件中设置
PUPPET_EXTRA_OPTS=--no-client
这种方式与(2)中方法殊途同归,但更简洁、方便。


(4)在 /etc/puppet/puppet.conf 的 [main] 段加上以下的指令:

runinterval=3m  #间隔3分钟


Troubleshootings


1.客户端执行test出错
# puppetd  --server server.puppet --test
err: Could not request certificate: Connection refused - connect(2)
Exiting; failed to retrieve certificate and waitforcert is disabled


解决办法:
            (1)确保设置好hosts文件,
            (2)#service puppetmaster start)


2.服务器启动puppetmaster失败
# service puppetmaster start
启动 puppetmaster:Could not prepare for execution: Cannot save ca; parent directory /var/lib/puppet/ssl/ca does not exist
                                                           [失败]
解决办法:关闭 SELinux


3.客户端执行test出错
# puppetd  --server server.puppet --test
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


缘由:客户端和服务器端的时间不一样步。
解决办法:客户端和服务器端运行/usr/sbin/ntpdate time.nist.gov

参考文献

http://docs.puppetlabs.com/references/stable/configuration.html

http://docs.puppetlabs.com/guides/configuring.html

http://369369.blog.51cto.com/319630/785895

http://puppet.wikidot.com/file

http://bbs.linuxtone.org/thread-22751-1-1.html

相关文章
相关标签/搜索