puppet 源码安装

puppet 源码安装

作者: admin 分类: 自动化管理 发布时间: 2013-04-23 10:25 ė 1932次浏览 暂无评论

puppet最新源码详细安装:

在网上找了很多相关puppet的,有很多用yum安装,安装虽然成功了。但是有客户端相关过来请求,还是会有问题。最佳的还是源码安装。

一、下载地址:

puppet下载地址:

http://downloads.puppetlabs.com/puppet/puppet-2.7.13.tar.gz

facter下载地址:

http://downloads.puppetlabs.com/facter/facter-1.6.7.tar.gz

ruby下载地址:

http://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7.tar.gz

二、环境准备:

1、时间必须一致:

crontab -e

  1. */2  *    *    *    *   /usr/sbin/ntpdate clock.nc.fukuoka-u.ac.jp >/dev/null 2>&1

2、首先gcc相关可用yum安装

yum install gcc-c++

yum install openssl*

3、服务器及客户端名字:

  1. [root@server bin]# more /etc/hosts
  2. # Do not remove the following line, or various programs
  3. # that require network functionality will fail.
  4. #127.0.0.1              localhost.localdomain localhost
  5. ::1             localhost6.localdomain6 localhost6
  6. 192.168.1.231      server.cbcv.net        server
  7. 192.168.1.232      client.cbcv.net        client
  8. [root@server bin]# more /etc/sysconfig/network
  9. NETWORKING=yes
  10. NETWORKING_IPV6=no
  11. HOSTNAME=server.cbcv.net

<客户端也是类似一样,略……>
三、安装应用软件
1、首先安装ruby
tar -zxvf ruby-1.8.7.tar.gz
cd ruby-1.8.7
./configure –prefix=/usr/local/ruby
make
make install
操作系统PATH路径:

vi  /etc/profile
export PATH=/usr/local/ruby/bin:$PATH

查看ruby –version版本

2、安装facter
tar -zxvf facter-1.6.7.tar.gz
cd facter-1.6.7
ruby install.rb

3、安装puppet

  1. tar -zxvf puppet-2.7.9.tar.gz
  2. cd puppet-2.7.9/
  3. ruby install.rb
  4. [root@server puppet-2.7.2rc2]# cp conf/redhat/fileserver.conf /etc/puppet/
  5. [root@server puppet-2.7.9]# cp conf/auth.conf /etc/puppet/
  6. [root@server puppet-2.7.9]# cp conf/namespaceauth.conf /etc/puppet/
  7. [root@server puppet-2.7.9]# cp conf/redhat/puppet.conf /etc/puppet/
  8. [root@server puppet-2.7.9]# cp conf/redhat/server.init /etc/init.d/puppetmaster
  9. [root@server puppet-2.7.9]# chmod +x /etc/init.d/puppetmaster
  10. [root@server puppet-2.7.9]# /etc/init.d/puppetmaster start
  11. Starting puppetmaster: /bin/bash: /usr/sbin/puppetmasterd: No such file or directory
  12.                                                            [FAILED]
  13. [root@server puppet-2.7.9]# cp sbin/puppetmasterd /usr/sbin/
  14. [root@server puppet-2.7.9]# chmod -R 777 /usr/sbin/puppetmasterd
  15. [root@server puppet-2.7.9]# cp sbin/puppetmasterd /bin/puppetmasterd
  16. [root@server puppet-2.7.9]# chmod -R 777 /bin/puppetmasterd
  17. [root@server puppet-2.7.9]# /etc/init.d/puppetmaster start
  18. Starting puppetmaster: /usr/bin/env: ruby: No such file or directory
  19.                                                            [FAILED]
  20. [root@server puppet-2.7.9]# ln -s /usr/local/ruby/bin/
  21. erb         filebucket  pi          puppetdoc   rdoc        ruby
  22. facter      irb         puppet      ralsh       ri          testrb
  23. [root@server puppet-2.7.9]# ln -s /usr/local/ruby/bin/ruby /usr/bin/
  24. [root@server puppet-2.7.9]# /etc/init.d/puppetmaster start
  25. Starting puppetmaster: Could not prepare for execution: Got 6 failure(s) while initializing: change from absent to directory failed: Could not set ‘directory on ensure: Could not find group puppet; change from absent to directory failed: Could not set ‘directory on ensure: Could not find group puppet; change from absent to directory failed: Could not set ‘directory on ensure: Could not find group puppet; change from absent to directory failed: Could not set ‘directory on ensure: Could not find group puppet; change from absent to directory failed: Could not set ‘directory on ensure: Could not find group puppet; change from absent to directory failed: Could not set ‘directory on ensure: Could not find group puppet
  26.                                                            [FAILED]
  27. [root@server puppet-2.7.9]# useradd puppet
  28. [root@server puppet-2.7.9]# /etc/init.d/puppetmaster start
  29. Starting puppetmaster:                                     [  OK  ]
  30. [root@server sbin]# pwd
  31. /usr/local/src/puppet-2.7.9/sbin
  32. cp -r * /usr/sbin/
  33. [root@server manifests]# chkconfig –add puppetmaster
  34. [root@server manifests]# chkconfig puppetmaster on

客户端其它安装和服务器安装相同,除了用cp conf/redhat/client.init /etc/init.d/puppet

  • tar -zxvf puppet-2.7.9.tar.gz
  • cd puppet-2.7.9/
  • ruby install.rb
  1. [root@localhost puppet-2.7.9]# cp conf/auth.conf /etc/puppet/
  2. [root@localhost puppet-2.7.9]# cp conf/namespaceauth.conf /etc/puppet/
  3. [root@localhost puppet-2.7.9]# cp conf/redhat/puppet.conf /etc/puppet/
  4. [root@localhost puppet-2.7.9]# cp conf/redhat/client.init /etc/init.d/puppet
  5. [root@localhost puppet-2.7.9]# chmod +x /etc/init.d/puppet
  6. [root@localhost puppet-2.7.9]# service puppet start
  7. Starting puppet: /bin/bash: /usr/sbin/puppetd: No such file or directory
  8.                                                            [FAILED]
  9. [root@localhost puppet-2.7.9]# cp sbin/puppetd /usr/sbin/puppetd
  10. [root@localhost puppet-2.7.9]# service puppet start
  11. Starting puppet: /usr/bin/env: ruby: No such file or directory
  12.                                                            [FAILED]
  13. [root@localhost puppet-2.7.9]# chmod +x /usr/sbin/puppetd
  14. [root@localhost puppet-2.7.9]# service puppet start
  15. Starting puppet: /usr/bin/env: ruby: No such file or directory
  16.                                                            [FAILED]
  17. [root@localhost puppet-2.7.9]# ln -s /usr/local/ruby/bin/ruby /usr/bin/
  18. [root@localhost puppet-2.7.9]# /etc/init.d/puppet start
  19. Starting puppet:                                           [  OK  ]

安装完毕后进行测试:

在服务器端执行:
puppetca –list
应该是无
在客户端执行 puppetd –server server.cbcv.net  –test
再到服务器端执行:
puppetca –s client.cbcv.net
clinet.cbcv.net (BF:5D:58:D0:F2:06:8C:FB:5C:AA:08:80:87:DC:92:20)

如果看到了客户端的证书请求,
针对客户端请求为
puppetca –s client.cbcv.net
用下面的命令对所有证书请求签名:
puppetca -s –a

在主服务器上/etc/puppet/manifests

vi /etc/puppet/manifests/site.pp
node default{
file { “/tmp/test.txt”:
content=> “hello, Test”;
}
}

再重启一下服务器service puppetmaster restart 和客户端service puppet restart
再执行puppetd –server server.cbcv.net  –test
看/tmp下面是不是有test.txt文件了
增加多个site.pp

可以先在site.pp中增加

import “test.pp”

然后再在里面执行需要操作的内容。

可用puppetd –server server.cbcv.net  –test进行测试。

如果有报错:notice: Ignoring –listen on onetime run
notice: Skipping run of Puppet configuration client; administratively disabled; use ‘puppet Puppet configuration client –enable’ to re-enable.

那么需要在客户端执行puppet agent –enable即可。

puppet agent –disable为关闭。

记得重启puppetmaster和puppet

以下为参考内容,感觉非常不错:
默认时间vim /etc/puppet/puppet.con
runinterval =1800 ###默认是30分钟,可以修改此处的值,单位为秒
有的时间修改了这个没有用,是什么原因呢:
我也曾修改过多次,没效果,后在客户端的配置文件再加了一句server = server.cbcv.net
然后重启
在客户端修改即可!最好重启一下server puppet restart

puppet 如何全客户端自动签名 
a.vim /etc/puppet/puppet.conf 
[puppetmaster]
autosign=true      #增加这两行
autosing=/etc/puppet/autosign.conf  
#增加这两行添加 
* 表示所有,或者添加域名,IP或者网段。举例:
b.再编辑 /etc/puppet/autosign.conf**.test.com192.168.0.1/24

错误总结<此错误是因为用了yum去安装。看到别人写的OK......>:
第一次认证的时候报下边的错误:
[root@client ~]# puppetd --server server.cbcv.net  --test
err: Could not retrieve catalog from remote server: certificate verify failed
我做测试发现的原因有三种:
第一:是hostname没有设置好,在安装前一定要把hostname设置好,设置好之后尽量重启机器。实在不行就删了重新安装。
第二:时间不同步,时间不同步也会报认证失败的错误,可以用date设置时间,只要不是差得太多就没事。
第三:是ssl的问题,在你用的这个客户端puppet已经做过其他的机器的客户端的情况下,因为已经生成的有证书,可能会和现在的冲突,把/var/lib/puppet/ssl这个文件夹删掉之后就行了。

1.3配置c/s模式的puppet的实验环境
Puppet的的客户端和服务端是靠ssl链接的,在服务端有一个自签名的根证书,在安装软件的时候自动生成。每个客户端的证书要经过根证书签名才能和服务器连接。所以首先要在客户端执行下面的命令来请求服务器签名证书。
puppetd --server server.cbcv.net --test

下面是一个完整的例子

 NOTE:puppet的客户端和服务器通过ssl连接,在服务器端上一个自签名的根证书,在安装软件的时候自动生成.因此在安装软件以前须设置好主机名,生成证书的时候要把主机名写入证书,如果证书生成后再改主机名,会导致无法通信.每个客户端的证书要经过根证书的签名才能和服务器连接.所以要执行以上命令,请求证书=>签发证书
   NOTE:自动签名:创建/etc/puppet/autosign.conf文件,添加*.puppet.com一行。根据实际情况,修改客户机对应的域名.puppet会检查证书请求是否匹配该配置文件的任何一行,客户端端主机如匹配*.puppet.com,puppetmaster都会自动签名。(因只要主机名匹配,master都会信任,此为潜在的安全问题,不建议使用)

Puppet目录结构(/etc/puppet):

  1. |-auth.conf                     #认证配置文件
  2. |-puppet.conf                   #主配置文件
  3. |-fileserver.conf            #文件服务器配置文件
  4. |-autosign.conf              #自动验证配置文件
  5. |‘-manifests                    #puppet代码文件存储目录
  6.     |‘-nodes                    #节点配置目录
  7.          |-node1.pp           #节点1配置文件
  8.     |-site.pp                   #入口文件,定义变量,默认配置等
  9.     |-modules.pp                #加载模块
  10. |‘-modules                      #模块配置目录,其下一个目录相当于一个模块,如mysql
  11.     |‘-mysql                    #mysql模块,一般有files,manifests,templates三个子目录
  12.          |‘-files             #文件存储目录,如my.cnf
  13.               |-my.cnf
  14.          |‘-manifests         #模块配置代码所在目录,其下必须有init.pp文件
  15.               |-init.pp
  16.          |‘-templates         #针对此模块模板目录
  17. |‘-templates                    #模板目录

Puppet的几个常见资源:

(1)文件:管理系统文件
  1. file {
  2.     “/etc/mysql/my.cnf”:
  3.     source => “puppet:///mysql-server/my.cnf”,
  4.     owner => root,
  5.     group => root,
  6.     mode => 0644;
  7. }
   NOTE:上面的代码是让/etc/mysql/my.cnf这个文件权限保持644,并且属于root用户root组,第二行为资源的title,默认和文件的名字相同,source是指定文件复制到客户端并覆盖已有的文件,上面代码定义的source代码也可这样写source=>"puppet://$fileserver/mysql-server/my.cnf",此处的mysql-server指的是定义在fileserver.conf中的一个模块所对应的全路径.这里需要知道fileserver.conf的配置:
  1. $ vim fileserver.conf    #添加如下行:
  2. [mysql-server]
  3.   path /etc/puppet/modules/mysql-server/files #指定文件所在的目录
  4.   allow *            #可配置allow,deny做限制,支持使用主机名,IP,
  5. 及通配符,此处为方便,允许所有主机
   通过fileserver.conf的定义,代码source =>"puppet:///mysql-server/my.cnf"所指定的路径可以解析为/etc/puppet/modules/mysql-server/files/my.cnf,代码source => "puppet:///mysql-server/my.cnf"中的mysql-server可简单理解为等价于/etc/puppet/modules/mysql-server/files
(2)软件包:管理软件包的安装升级和删除。
  1. package {
  2.       ["nginx","mysql-server"]:
  3.       ensure => installed;
  4.       ["vim"]:
  5.       ensure => absent;
  6. }
   NOTE:该资源的主要是ensure,表示该软件包应该在什么状态。installed 表示要安装该软件,也可以写成present; absent表示卸载该软件
(3)服务:管理系统服务(启用,禁用,重启等)
  1. service {
  2.     “mysql”:
  3.     enable => true,
  4.     ensure => running;
  5.     “nginx”:
  6.     ensure => stopped;
  7. }
   NOTE:enable表示服务在开机的时候是否启动,可以设置的值是true和false;
        ensure表示是否运行服务, running表示运行服务,stopped 表示停止服务
(4)Exec:执行外部命令
  1. exec {
  2.     “set mysql root password”:
  3.     path => “/usr/bin”,
  4.     unless => “mysql -uroot -p$root_mysql_password”,
  5.     command => “mysqladmin -u root password $root_mysql_password”;
  6. }
   NOTE:path:命令执行的搜索路径。如果path没有被定义,命令需要使用绝对路径。路径可以以数组或以冒号分隔的形式来定义。
        unless:如果unless所设定的命令返回0,此定义的资源将不被执行
        command:将会被执行的命令,必须为被执行命令的绝对路径,或者得提供该命令的搜索路径更多资源介绍请访问puppet官网或者puppet中文维基:http://puppet.wikidot.com/

Puppet资源间的关系:

 (1)puppet 资源之间简单依赖关系:
   before:在某个资源之前执行
   after:在某个资源之后执行
   require:某个资源必须存在或者正确执行后,才执行相应的资源。
 (2)puppet 资源之间触发更新:
   notify:用来通知某个资源进行更新
   subscribe:该资源有更新时,通知另一个资源执行相应的动作

Puppet类:

作用:把一组资源收集到一个盒子里面,一起使用,其他地方要用到直接包含此个类就可以,便于维护。例:
  1. class ssh {
  2.         package {
  3.                 [ "openssh-server" ]:
  4.                 ensure => installed;
  5.         }
  6.         file {
  7.                 “/etc/ssh/sshd_config”:
  8.                 mode => 0644,
  9.                 owner => root,
  10.                 group => root,
  11.                 source => “puppet://$fileserver/ssh/sshd_config”,
  12.                 require => Package["openssh-server"];  #在指定的Packgae资源执行成功后再执行此File资源
  13.         }
  14.         service {
  15.                 “ssh”:
  16.                 ensure => running,
  17.                 require => Package["openssh-server"],
  18.                 subscribe => File["/etc/ssh/sshd_config"]; #sshd_config文件有变动,重启ssh服务,使其生效
  19.         }
  20. }

Puppet节点:

 puppet用来区分不同的客户端,并且给不同的服务器端分配manifest.例:
  1. node ‘host1.puppet.com’ {
  2.            include ssh  #此处的ssh指的是定义的ssh类
  3. }
  4. node ‘host2.puppet.com’ {
  5.          include apache,mysql,ssh
  6. }

Puppet模块:

  (1):一个模块包含/etc/puppet/modules目录下的一个目录和它的子目录,通常一个模块目录下面含三个目录files,manifests,templates.
  (2):在puppet的主文件site.pp里面可以用import modulename可以插入模块。新版本的puppet可自动插入modules目录下的模板。引入模块,可以结构化代码,便于分享,保存,及管理
  (3):manifests里面必须包含一个init.pp的文件,为此模块的初始文件.
  (4):files目录是该模块的文件发布目录,
  (5):templates包含erb模型文件。

一个完整示例:

(1)修改主机名,hosts文件,同步时间
(2)服务器端安装好puppet,puppetmaster;客户端安装好puppet
(3)客户端请求签名,服务器端签名
(4)/etc/puppet下代码的配置
  1. $ cd /etc/puppet
  2. #modules目录配置:
  3. $ sudo mkdir -pv modules/mysql-server/{files,manifests,templates} #创建模块目录及其子目录
  4. files
  5. $ sudo vim my.cnf #此文件配置好,放在files目录下即可
  6. $ cd /etc/puppet/modules/mysql-server/manifests/
  7. $ sudo vim init.pp  #模块的主配置文件,添加如下行:
  8.  #mysql-server install and set psaaword
  9.  #
  10.  class mysql-server {
  11.         package {
  12.                 [ "mysql-server","mysql-client" ]:
  13.                 ensure => installed;
  14.         }
  15.         file {
  16.                 “/etc/mysql/my.cnf”:
  17.                 source => “puppet://$fileserver/mysql-server/my.cnf”,
  18.                 owner => root,
  19.                 group => root,
  20.                 mode => 0644,
  21.                 require => Package[ "mysql-server","mysql-client" ];
  22.         }
  23.         service {
  24.                 “mysql”:
  25.                 ensure => running,
  26.                 require => File[ "/etc/mysql/my.cnf" ],
  27.                 subscribe => File[ "/etc/mysql/my.cnf" ];
  28.         }
  29.         exec {
  30.                 “set mysql root password”:
  31.                 path => “/usr/bin”,
  32.                 unless => “mysql -uroot -p$root_mysql_password”,
  33.                 command => “mysqladmin -u root password $root_mysql_password”,
  34.                 require => Service['mysql'];
  35.         }
  36.  }
  37. #manifests目录下配置:
  38. $ cd /etc/puppet/manifests
  39. $ sudo vim site.pp  #puppet入口文件配置,添加如下行:
  40.  $fileserver = “server.puppet.com”  #puppet变量定义,注意$
  41.  $root_mysql_password = “123456″
  42.  node default {
  43.         file {
  44.                 “/tmp/test1.txt”:
  45.                 content => “hello”;
  46.         }
  47.  }
  48.  import “modules.pp”
  49.  import “node*.pp”
  50. $ sudo vim modules.pp   #导入模块配置,添加如下行:
  51.  import “mysql-server”
  52. $ sudo vim node1.pp  #节点配置文件,添加如下行:
  53.  node ‘client.puppet.com’ {
  54.         include mysql-server
  55.  }
  56. #文件服务器配置:
  57. $ cd /etc/puppet
  58. $ sudo vim fileserver.conf  #文件服务器配置文件,添加如下:
  59.  [mysql-server]
  60.     path /etc/puppet/modules/mysql-server/files
  61.     allow *

(5)在客户端执行如下命令:mysql就会在客户端下安装(如想让puppet以守护进程运行,去掉–test参数即可,puppetd命令的详细参数请参照:puppetd –help)

  1. $ sudo puppetd –test –server server.puppet.com

至此puppet已配置完毕,此篇内容,主要针对入门级简单应用,更多的puppet的语法,如puppet函数(puppet称之为define),数组,以及puppet的更高级的内容,如和nginx结合puppet做集群等等,本文并未涉及,有兴趣的同学可参考puppet官方文档。

原文地址:https://www.cnblogs.com/L-H-R-X-hehe/p/3957090.html