Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1706204
  • 博文数量: 636
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3950
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-06 21:58
个人简介

博客是我工作的好帮手,遇到困难就来博客找资料

文章分类

全部博文(636)

文章存档

2024年(5)

2022年(2)

2021年(4)

2020年(40)

2019年(4)

2018年(78)

2017年(213)

2016年(41)

2015年(183)

2014年(66)

我的朋友

分类: 系统运维

2017-04-05 20:04:24

【安装背景】
系统centos 6.5

软件地址:


master: 192.168.100.10
agent: 192.168.100.13

因为puppet对ca证书的办法是很对域名了。所以域名一定要解析并且master端最好不要变。

【安装步骤】

时钟同步
ntpdate pool.ntp.org

设置hostname并放入hosts解析

echo "192.168.100.10  master.test.com" >> /etc/hosts
echo "192.168.100.13  agent.test.com" >> /etc/hosts

puppet端master和agent端的安装:

puppet是基于ruby的,所以首先要安装ruby语言环境。

yum install ruby -y

安装facter -->收集系统信息的软件命令

################# master 和agent 共同的步骤 #############

tar xvf facter-1.6.13.tar.gz 
cd facter-1.6.13
ruby install.rb 

## 检查facter安装是否成功

facter 收集系统的信息的命令工具

tar xvf puppet-3.0.0.tar.gz 
cd puppet-3.0.0
ruby install.rb 


创建puppet 用户否则启动失败
useradd  puppet

######################## master 拷贝配置

[root@master puppet-3.0.0]# pwd
/usr/local/src/puppet-3.0.0
[root@master puppet-3.0.0]# cp ext/redhat/server.init /etc/init.d/puppetmaster 
[root@master puppet-3.0.0]# chmod 755 /etc/init.d/puppetmaster 

cp ext/redhat/puppet.conf  /etc/puppet/

######################## agent 拷贝配置

[root@agent puppet-3.0.0]# cp  ext/redhat/client.init  /etc/init.d/puppetclient
[root@agent puppet-3.0.0]# chmod 755 /etc/init.d/puppetclient


##### master启动
/etc/init.d/puppetmaster start


#### master启动检测

[root@master puppet-3.0.0]# ps -ef |grep ruby
puppet   10915     1  1 16:34 ?        00:00:01 /usr/bin/ruby /usr/bin/puppet master
root     11272  6458  0 16:36 pts/0    00:00:00 grep --color=auto ruby

[root@master puppet-3.0.0]# netstat -tulnp |grep ruby
tcp        0      0 0.0.0.0:8140                0.0.0.0:*                   LISTEN      10915/ruby 

##### agent 启动
[root@agent puppet-3.0.0]# /etc/init.d/puppetclient start
Starting puppet agent:     

#### agent 启动检测
[root@agent puppet-3.0.0]# ps -ef |grep ruby
root      5623     1  5 16:40 ?        00:00:02 /usr/bin/ruby /usr/bin/puppet agent


############## agent 请求证书 master签发证书  


agent request   agent端向maser端发起证书请求 puppet agent --test --server master.test.com

master accept    puppet cert sign  agent.test.com  接收指定的 puppet cert sign --all 接收所有的请求
agent fetch 


过程1
[root@agent puppet-3.0.0]# puppet agent --test --server master.test.com 
Info: Caching certificate for ca
Info: Creating a new SSL certificate request for agent.test.com
Info: Certificate Request fingerprint (SHA256): DB:AC:CB:4E:FC:68:E3:71:B5:45:6E:6C:73:C9:EE:27:11:0F:2D:5A:8F:43:21:53:0D:33:7F:85:87:4B:0E:59
Exiting; no certificate found and waitforcert is disabled
[root@agent puppet-3.0.0]# 


过程2:
master端已经有这个请求了 接受就可以了
[root@master puppet]# puppet cert list  或者 puppet cert list --all        
  "agent.test.com" (SHA256) DB:AC:CB:4E:FC:68:E3:71:B5:45:6E:6C:73:C9:EE:27:11:0F:2D:5A:8F:43:21:53:0D:33:7F:85:87:4B:0E:59
[root@master puppet]# 

接收证书:
[root@master puppet]# puppet cert sign  agent.test.com
Signed certificate request for agent.test.com
Removing file Puppet::SSL::CertificateRequest agent.test.com at '/var/lib/puppet/ssl/ca/requests/agent.test.com.pem'


过程3:
agent端测试

puppet agent --test --server master.test.com

############### 出错过程 ##########
[root@agent puppet-3.0.0]# puppet  agent --test                  
Error: Could not request certificate: Connection refused - connect(2)
Exiting; failed to retrieve certificate and waitforcert is disabled


我这里的原因是 agent的使用的都是默认配置 没有知道指定master 解决方法:
puppet agent --test --server master.test.com

排除了网络问题的命令: telnet master.test.com 8140

####### 写一个测试用例 测试 master 和 agnet可以通信了 及 master可以管理agent端了   agent可以同步master端了


master端编写一个测试pp
[root@master manifests]# cat site.pp
file { "/tmp/temp1.txt":
      content => "Hello World\n"; }
[root@master manifests]# pwd
/etc/puppet/manifests


### agent 进行同步
[root@agent puppet-3.0.0]# puppet agent --test --server master.test.com
Info: Retrieving plugin
Error: /File[/var/lib/puppet/lib]: Could not evaluate: Could not retrieve information from environment production source(s) puppet://master.test.com/plugins
Info: Caching catalog for agent.test.com
Info: Applying configuration version '1480151413'
/Stage[main]//File[/tmp/temp1.txt]/ensure: defined content as '{md5}e59ff97941044f85df5297e1c302d260'
Finished catalog run in 0.04 seconds
[root@agent puppet-3.0.0]# cat /tmp/temp1.txt 
Hello World
[root@agent puppet-3.0.0]# 

上面错误的信息原因:
Error: /File[/var/lib/puppet/lib]: Could not evaluate: Could not retrieve information from environment production source(s) puppet://master.test.com/plugins

不影响: 原因未知  


【puppet清除证书】
master端:
[root@shzr-vctr001 user]# puppet cert --clean shzr-vapp015.a.pa.com
Notice: Revoked certificate with serial 14
Notice: Removing file Puppet::SSL::Certificate shzr-vapp015.a.pa.com at '/var/lib/puppet/ssl/ca/signed/shzr-vapp015.a.pa.com.pem'
Notice: Removing file Puppet::SSL::Certificate shzr-vapp015.a.pa.com at '/var/lib/puppet/ssl/certs/shzr-vapp015.a.pa.com.pem'

client端:
\rm -r  /var/lib/puppet/ssl/

1.  概述
puppet是一个开源的软件自动化配置和部署工具,它使用简单且功能强大,正得到了越来越多地关注,现在很多大型IT公司均在使用puppet对集群中的软件进行管理和部署,如google利用puppet管理超过6000台地mac桌面电脑(2007年数据)。
本文主要介绍puppet安装方法,设计架构及使用方法。
2.  设计架构
puppet是基于c/s架构的。服务器端保存着所有对客户端服务器的配置代码,在puppet里面叫做manifest. 客户端下载manifest之后,可以根据manifest对服务器进行配置,例如软件包管理,用户管理和文件管理等等。


如上图所示,puppet的工作流程如下:

(1)客户端puppetd调用facter,facter探测出主机的一些变量,例如主机名,内存大小,ip地址等。pupppetd 把这些信息通过ssl连接发送到服务器端;


(2)服务器端的puppetmaster 检测客户端的主机名,然后找到manifest里面对应的node配置, 并对该部分内容进行解析,facter送过来的信息可以作为变量处理,node牵涉到的代码才解析,其他没牵涉的代码不解析。解析分为几个阶段,语法检查,如果语法错误就

报错。如果语法没错,就继续解析,解析的结果生成一个中间的“伪代码”,然后把伪代码发给客户端;


(3)客户端接收到“伪代码”,并且执行,客户端把执行结果发送给服务器;


(4)服务器端把客户端的执行结果写入日志。


puppet工作过程中有两点值得注意,


第一,为了保证安全,client和master之间是基于ssl和证书的,只有经master证书认证的client可以与master通信


第二,puppet会让系统保持在你所期望的某种状态并一直维持下去,如检测某个文件并保证其一直存在,保证ssh服务始终开启,如果文件被删除了或者ssh服务被关闭了,puppet下次执行时(默认30分钟),会重新创建该文件或者启动ssh服务。

3.  软件安装

不推荐使用apt-get命令进行安装,因为该命令下载的puppet存在bug。可直接从源代码进行安装,需要安装的软件有ruby,facter和puppet。

3.1   安装步骤


编辑/etc/host以修改主机名,因为puppet是基于证书的,证书中包含主机名;

在master和slave上依次安装ruby、facter和puppet,安装facter和puppet时,要使用ruby install.rb。

3.2    安装后的目录结构

(1)    安装目录

安装目录默认存为/etc/puppet,该目录下的manifests存放manifest文件

其他可执行文件在/user/sbin下,主要有:


puppet: 用于执行用户所写独立的mainfests文件,如:
puppet -l /tmp/manifest.log manifest.pp
puppetd: 运行在被管理主机上的客户端程序,如:
puppet –server servername –waitforcert 60
puppetmasterd:运行在管理机上的服务器程序,如:
puppetmasterd –debug
puppetca puppet认证程序,主要用于对slave的证书进行认证,如:


查看需认证的slave:puppetca –list


对这些slave进行认证:puppetca -s –a


puppetrun 用于连接客户端,强制运行本地配置文件,如:


puppetrun -p 10 –host host1 –host host2 -t remotefile -t webserver


(2)    配置文件


puppet.conf

Puppet的主配置文件,如果是root用户,配置文件为/etc/puppet/puppet.conf,普通用户,配置文件为:~user/.puppet/puppet.conf


具体配置参数,参见:





puppet文件服务器的配置文件。用path配置文件路径,allow/deny配置访问权限,具体参见:


3.3   验证安装是否成功


选定一个slave与master进行验证,假设slave的host为slave00,master的host为masterhost,在slave00上输入:


puppetd –test –server servername

然后在masterhost上查看待认证的slave:

puppetca –list

如果没问题的话,此时可以看到slave00,对该slave的证书进行签名:

puppetca -s -a

这样slave00通过了证书验证,可以与master进行进一步交互了。


在masterhost的/etc/puppet/manifests目录下编写site.pp文件,内容如下:
node default {
file {
“/tmp/test”:
content=>”hello\n”,
mode => 0644;
}
}
同时在slave00上输入:puppetd –test –server servername, 查看slave00的/tmp文件夹,生成了一个新文件test,里面的内容是hello,该文件的权限是-rw-r—r—。这样,便证明puppet安装成功,如果出现错误,查看第六节。


4.  配置脚本编写


本节介绍puppet的配置脚本编写方法,主要是指puppet的manifest编写方法。puppet把需要管理的内容抽象成为资源,每种资源有不同的属性,因此puppet语言就是描述这些资源的属性以及资源之间关系的语言。
为了便于管理,puppet将资源模块化,即每个功能模块的manifest单独放在一个目录下。每个模块包含一个主要的manifest文件(init.pp,它是模块的入口,类似于C语言中的main函数),里面包含若干个class对该模块的资源进行封装,常见的资源有file,package,service等,每种资源由自己的属性,如file有属性name,owner,mode等。
本节主要介绍puppet中manifest的编写方法,将依次介绍资源属性,资源,节点管理,函数和模块的编写方法。


4.1   资源属性
资源属性有两种,一种是资源专属属性,另一种是资源共同属性,对于资源专属属性,将在下一节介绍;而资源共同属性是所有资源共有的属性,主要有:
before
用于控制不同对象(资源)的执行顺序关系,表示某个对象(资源)在另一个对象之后发生(require与之相反,它表示之前发生)。如:
file { “/var/nagios/configuration”:
source  => “…”,
recurse => true,
before => Exec["nagios-rebuid"]
}
exec { “nagios-rebuild”:
command => “/usr/bin/make”,
cwd => “/var/nagios/configuration”
}
这段代码保证用make编译之前,所有代码都是最新的。也可以before多个资源,如:
before => [ File["/usr/local"], File["/usr/local/scripts"] ]
subscribe
检测某个资源,当它发生变化时,该资源会重新加载,如:
class nagios {
file { “/etc/nagios/nagios.conf”:
source => “puppet://server/module/nagios.conf”,
alias => nagconf # just to make things easier for me
}
service { nagios:
ensure => running,
subscribe => File[nagconf]
}
}
当检测到文件nagconf被修改时,服务nagios会相应的更新。需要注意的是,目前支持subscribe的资源只有exec,service和mount。
更多资料,参见:



4.2   资源
常用的资源主要有以下几个:
file:文件管理
package:软件包管理
service:系统服务管理
cron:配置定期任务
exec:运行shell命令
(1)    file资源
更详细资料,可参见:
(2)    package资源
更详细资料,可参见:
(3)    service资源
更详细资料,可参见:
(4)    exec资源
更详细资料,可参见:
(5)    cron资源
更详细资料,可参见:
4.3   节点管理
puppet如何区分不同的客户端,并且给不同的服务端分配manifest呢?puppet使用node资源做这件事情,node 后面跟客户端的主机名,例如:
node ‘ slave00 ‘ {
include ssh
}
node ‘ slave11 ‘ {
$networktype=”tele”
$nagioscheckport=”80,22,3306″
include apache, mysql, php
}
资源node中可使用变量,也可直接通过include把其他manifest包含进来。
更详细资料,可参见:
4.4   类和函数
类可以把多个相关的资源定义在一起,组成一个类。类可以继承,具体参见:
函数(在puppet中称为“defination”)可以把多个资源包装成一个资源,或者把一个资源包装成一个模型,便于使用。例如,在debian里面管理一个apache虚拟机非常简单,把一个虚拟主机的配置文件放到/etc/sites-available/里面,然后做一个符号链接到/etc/sites-enabled目录。 你可以为你每个虚拟主机复制同样的配置代码,但是如果你使用下面的代码就会更好和更简单:
define virtual_host($docroot, $ip, $order = 500, $ensure = “enabled”) {
$file = “/etc/sites-available/$name.conf”
# The template fills in the docroot, ip, and name.
file { $file:
content => template(“virtual_host.erb”),
notify  => Service[apache]
}
file { “/etc/sites-enabled/$order-$name.conf”:
ensure => $ensure ? {
enabled  => $file,
disabled => absent
}
}
}
然后,你就可以使用这个定义来管理一个apache虚拟主机,如下面代码所示:
virtual_host { “reductivelabs.com”:
order   => 100,
ip      => “192.168.0.100″,
docroot => “/var/www/reductivelabs.com/htdocs”
}
4.5   模块


一个模块就是一个/etc/puppet/modules目录下面的一个目录和它的子目录,在puppet的主文件site.pp里面用import modulename可以插入模块。新版本的puppet可以自动插入/etc/puppet/modules目录下的模块。引入模块,可以结构化代码,便于分享和管理。例如关于apache的所有配置都写到apache模块下面。一个模块目录下面通常包括三个目录:files,manifests,templates。manifests 里面必须要包括一个init.pp的文件,这是该模块的初始(入口)文件,导入一个模块的时候,会从init.pp开始执行。可以把所有的代码都写到init.pp里面,也可以分成多个pp文件,init 再去包含其他文件。files目录是该模块的文件发布目录,puppet提供一个文件分发机制,类似rsync的模块。templates 目录包含erb模型文件,这个和file资源的template属性有关。
puppet安装好以后,modules目录是没有的,自己建立一个就行,然后在里面可以新增加你的模块。




5.  编程实例


5.1    Hello World
本节介绍了一个非常简单的编程实例:一个slave从master中获取其manifest,该maniftest要求slave依次做以下工作:安装gcc,创建文件夹/home/dxc/test,下载文件hello.c程序,编译hello.c。
(1)    代码结构组织
Master上代码的目录结构如下:
|– auth.conf
|– fileserver.conf   #puppet文件服务器配置文件
|– manifests   #puppet主文件所在目录
|   |– modules.pp        #puppet各个模块汇总
|   |– nodes         #各个slave要处理的模块
|   |   `– execHello.pp      #hello模块对应由那些slave处理
|   `– site.pp                  #puppet主文件(入口文件)
|– modules     #puppet的各个模块所在文件
|   `– hello   #hello模块
|       |– files    #该模块对应的文件资源,可能是要发送给slave的配置文件等
|       |   `– hello.c
|       `– manifests   #模块的manifest文件
|           `– init.pp                  #模块入口文件
`– ssl       #puppet的证书文件目录
(2)    程序执行流程
代码调用顺序是:
Slave发起连接请求 à site.pp à nodes àmodules à init.pp
首先,slave向发起master连接请求,进行证书验证;
接着,证书验证通过后,master会直接找到入口文件manifests目录下的site.pp文件,该文件可能包含一些全局变量,参数缺省值(当各个模块没有设置这些参数时,它们的缺省值)以及其它pp文件的调用(在该例子中,会调用modules.pp和nodes下的各个pp文件);
然后,master通过nodes下的各个pp文件定位到该slave要执行的模块(init.pp是各个模块的入口),汇总这些模块代码返回给slave;
最后,slave根据master发过来的manifest,配置信息。
(3)    代码解释
直接在此处下载代码。
5.2   一个更复杂的实例
本节介绍了一个更为复杂的某个公司正在使用实例,puppet代码布局与上一个实例一致,只不过该实例涉及到更多模块,更复杂的依赖管理。代码具体内容本节就不解释了,具体参见代码。
6.  可能遇到的问题
Q: puppet的证书机制
A: puppet证书问题是初学者最容易遇到的问题,这里讲一下怎么处理。puppet服务器端在安装或者首次启动的时候,会自动生产一个根证书和服务器证书,证书和主机名相关,因此如果证书生成后友改了主机名,那就会出问题。 puppet客户端在首次启动的时候,也会自动生成证书;但是这个证书需要得到puppet服务器端的签名才行,因此;puppet客户端第一次连接服务器的时候,会发送一个证书请求;服务器端需要对这个证书进行签名。puppet客户端在下次连接服务器的时候就会下载签名好的证书。
Q:Ubuntu下面的证书出错,怎么解决?
A:本方法是提供给初学者的测试环境,生成环境不建议这么做。首先在puppetmaster(服务器端)删除/var/lib/puppet/ssl目录;然后启动puppetmasterd;然后在客户端也删除/var/lib/puppet/ssl目录。把puppetmaster机器的主机名和对应的ip地址写入客户端机器的/etc/hosts。
然后执行:puppetd –test –server server.example.com. 把server.example.com替
换成你自己的服务器主机名。 执行这个命令,会有提示信息,不用理会。
然后登录到puppetmaster服务器机器,执行puppetca –list 命令,看看是否有客户端的证书请求;如果没有,请检查前面的步骤是执行正确,以及网络连接是否正常。 如果puppetca –list 能看到请求,那么执行puppetca -s -a 命令;对所有的证书请求签名。最后回到puppet客户端机器,执行
puppetd –test –server server.example.com.
就能建立连接了,如果你的site.pp写好了.就可以测试puppet了。
补充:如果客户端和服务器端的时间不一致也会导致证书认证失败,因此出现证书问题的时候需要检查两台机器的时间是否一致,如果不一致用date命令或者ntpdate命令让两台机器的时间一致。
Q:出现错误[Puppet Users] err: Could not retrieve catalog; skipping run
A:可能是由于安装了两个版本的ruby或者facter的原因,解决方案见:

7.  总结
随着服务器集群规模越来越大,自动化配置和部署这些服务器能够使管理变得非常容易并大大减小管理部署成本,因而得到IT公司的高度重视。
本文档介绍了puppet,一种新型的软件自动化配置和部署工具。本文主要内容涉及puppet的架构,安装和使用方法,并给出了两个使用实例。
在大规模的生成环境中,如果只有一台puppetmaster会忙不过来的,因为puppet是用ruby写的,ruby是解析型语言,每个客户端来访问,都要解析一次,当客户端多了就忙不过来,所以需要扩展成一个服务器组。puppetmaster可以看作一个web服务器,实际上也是由ruby提供的web服务器模块来做的。因此可以利用web代理软件来配合puppetmaster做集群设置,具体参见: 。




服务器端:192.168.2.60    master.king.com
客户端:192.168.2.147   slave.king.com

1、服务器端设置用户名及DNS:
[root@master manifests]# more /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=master.king.com
NETWORKING_IPV6=no
PEERNAT=no
GATEWAY=192.168.2.1
[root@master manifests]# more /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.60    master.king.com
192.168.2.147   slave.king.com
[root@master manifests]# more /etc/resolv.conf 
search localdomain CNC
nameserver 8.8.8.8

2、安装puppet-server及puppet:

# rpm -Uvh
# yum -y install puppet-server
# yum -y install puppet

chkconfig puppet on
chkconfig puppetmaster on
service puppetmaster start
service puppet start
lokkit -p 8140:tcp

# puppet cert list --all

3、客户端设置用户名及DNS:

[root@slave tmp]# more /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=slave.king.com
[root@slave tmp]# more /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.60    master.king.com
192.168.2.147   slave.king.com

[root@slave tmp]# more /etc/resolv.conf
# Generated by NetworkManager
search king.com
nameserver 219.141.140.10
nameserver 219.141.136.10
nameserver 8.8.8.8

4、客户端安装puppet
yum install puppet
chkconfig puppet on
service puppet start
编辑 /etc/puppet/puppet.conf, 添加一行,指定master服务器名称。
server=master.king.com


5、证书颁发:
# puppetd --server master.king.com --test

服务器端查看# puppetca -l
服务器端分发# puppetca -s slave.king.com

6、功能测试:
[root@master manifests]# more site.pp 
node default {
file {"/tmp/Puppet_test.txt":
content=>"This is test of PUPPET";}

#service puppetmaster restart

[root@master manifests]# puppet /etc/puppet/manifests/site.pp
warning: Implicit invocation of 'puppet apply' by passing files (or flags) directly
to 'puppet' is deprecated, and will be removed in the 2.8 series.  Please
invoke 'puppet apply' directly in the future.
notice: Finished catalog run in 0.02 seconds

客户端执行:
[root@slave tmp]# puppetd --test --server master.king.com
info: Caching catalog for slave.king.com
info: Applying configuration version '1417576906'
notice: /Stage[main]//Node[default]/File[/tmp/Puppet_test.txt]/ensure: defined content as '{md5}
0d31f0cb46dc51da999298473d3d26a3'
notice: Finished catalog run in 0.03 seconds
[root@slave tmp]# more Puppet_test.txt 
This is test of PUPPET

7、文件分发功能:

服务器端:
[root@master manifests]# more /etc/puppet/fileserver.conf 
[tmp]
        path /tmp
        allow *.king.com
[root@master manifests]# more site.pp 
node default {
file {"/tmp/Puppet_test.txt":
content=>"This is test of PUPPET";}

file {
    ”/tmp/hello.sh”:
    source => “puppet://master.king.com/tmp/hello.sh”,
    owner => root,
    group => root,
    mode => 755
}
[root@master manifests]# more /tmp/hello.sh 
#!/bin/bash
        touch /root/bb
客户端:
[root@slave tmp]# ps -ef |grep puppet
root      2513     1  0 10:17 ?        00:00:03 /usr/bin/ruby /usr/sbin/puppetd
root      3424  2446  0 10:47 pts/0    00:00:00 /usr/bin/ruby /usr/bin/puppet --server master.king.com
root      7885  2446  0 11:50 pts/0    00:00:00 grep puppet
[root@slave tmp]# kill 2513
[root@slave tmp]# puppetd --server master.king.com --test

8、文件执行功能:
服务器端:
[root@master manifests]# more site.pp 
node default {
file {"/tmp/Puppet_test.txt":
content=>"This is test of PUPPET";}

file { "/tmp/hello.sh":
owner => root,
group => root,
source => "puppet://master.king.com/tmp/hello.sh",
mode => 755,
}
exec {
"/tmp/hello.sh":
cwd => "/tmp",
timeout => 7200,
user => root,
path => ["/sbin","/usr/sbin","/usr/local/sbin","/usr/local/bin","/usr/bin","/bin
"],
}
客户端:
[root@slave tmp]# puppetd --server master.king.com --test
info: Caching catalog for slave.king.com
info: Applying configuration version '1417583652'
notice: /Stage[main]//Exec[/tmp/hello.sh]/returns: executed successfully
notice: Finished catalog run in 0.59 seconds
[root@slave tmp]# ls
hello.sh      Puppet_test.txt      
[root@slave tmp]# more hello.sh 
#!/bin/bash
        touch /root/bb
[root@slave tmp]# more /root/bb 

9、自动同步功能:
客户端:
[root@slave tmp]# vi /etc/puppet/puppet.conf 
server=master.king.com
listen=true
runinterval=900
[root@slave tmp]# puppetd
[root@slave tmp]# ps -ef | grep puppet
root      3424  2446  0 10:47 pts/0    00:00:00 /usr/bin/ruby /usr/bin/puppet --server master.king.com
root     10270     1 15 13:17 ?        00:00:01 /usr/bin/ruby /usr/sbin/puppetd
root     10436  2446  0 13:17 pts/0    00:00:00 grep puppet
   
[root@slave tmp]# tail -f /var/log/messages
Dec  3 13:17:39 slave puppet-agent[10270]: Reopening log files
Dec  3 13:17:41 slave puppet-agent[10270]: Starting Puppet client version 2.7.25
Dec  3 13:17:43 slave puppet-agent[10270]: (/Stage[main]//Exec[/tmp/hello.sh]/returns) executed successfully

10、强制同步功能:(服务器端发起)

编辑服务器端和客户端文件/etc/puppet/namespaceauth.conf

[root@master manifests]# more /etc/puppet/namespaceauth.conf
[fileserver]
        allow *.king.com
[puppetmaster]
        allow *.king.com
[pelementserver]
        allow *.king.com
[puppetrunner]
        allow *.king.com
[puppetbucket]
        allow *.king.com
[puppetreports]
        allow *.king.com
客户端:
编辑客户端文件/etc/puppet/auth.conf,将path /run 一段添加到path / 之前
#more /etc/puppet/auth.conf
path /run
method save
allow *
# this one is not stricly necessary, but it has the merit
# to show the default policy which is deny everything else
path /
auth any
重启:
[root@master manifests]# service puppetmaster restart
[root@master manifests]# service puppet restart
服务器端强制某台客户端立即读取site.pp
[root@master manifests]# puppetrun --host slave.king.com
客户端
[root@slave tmp]# tail -f /var/log/messages
Dec  3 13:32:59 slave puppet-agent[10601]: triggered run
Dec  3 13:33:00 slave puppet-agent[10601]: (/Stage[main]//Exec[/tmp/hello.sh]/returns) executed successfully
Dec  3 13:33:00 slave puppet-agent[10601]: Finished catalog run in 1.22 seconds

11、crontab文件管理功能:
服务器端:
[root@master manifests]# more site.pp 
# Add a crontab for puppet client
cron { logrotate:
    command => "/tmp/hello.sh",
    user => root,
    hour => 14,
    minute => 22
}
客户端:
[root@slave tmp]# puppetd --server master.king.com --test
notice: Ignoring --listen on onetime run
info: Caching catalog for slave.king.com
info: Applying configuration version '1417585229'
notice: /Stage[main]//Cron[logrotate]/ensure: created
notice: /Stage[main]//Exec[/tmp/hello.sh]/returns: executed successfully
notice: Finished catalog run in 6.17 seconds
[root@slave tmp]# crontab -l
22 14 * * * /tmp/hello.sh




puppet的三种认证方式:自动注册、手动注册和预签名注册;master和agent的认证关系,是随着认证的复杂程度提升,安全性也会随之提高,下面就是每一种方式的示例解读。


一 手动注册

手动注册是由Agent端先发起证书申请请求,然后由Puppetserver端确认证书方可注册成功,这种注册方式安全系数中等,逐一注册(puppet cert --sign certnmame)在节点数量较大的情况下是比较麻烦的,效率也低,批量注册(puppet cert --sign --all)效率很高,一次性便可注册所有的Agent的请求,但是这种方式安全系数较低,因为错误的请求也会被注册上。


1、节点申请认证

[root@agent1 ~]# puppet agent --test

info: Creating a new SSL key for agent1_cert.puppet.com
info: Caching certificate for ca
info: Creating a new SSL certificate request for agent1_cert.puppet.com
info: Certificate Request fingerprint (md5): 69:D2:86:25:7F:00:RT:55:61:P9:02:34:9E:9B:AF:F9
Exiting; no certificate found and waitforcert is disabled


2、服务器端确定认证

[root@master ~]# puppet cert --list --all                                         #查看认证情况

 "agent1_cert.puppet.com"  (69:D2:86:E4:7F:00:E0:55:61:19:02:34:9E:9B:AF:F9) #未认证
+ "master.puppet.com" (C0:E3:6B:76:36:EC:92:93:4D:BF:F0:8F:77:00:91:C8)


[root@master ~]# puppet cert --sign agent1_cert.puppet.com     #注册agent1

notice: Signed certificate request for agent1_cert.puppet.com #将请求的证书正式注册
notice: Removing file Puppet::SSL::CertificateRequest agent1_cert.puppet.com at '/var/lib/puppet/ssl/ca/requests/agent1_cert.puppet.com.pem' #删除请求

[root@master ~]# puppet cert --list --all                                 #再次查看认证情况

+ "agent1_cert.puppet.com"  (3E:46:4E:75:34:9A:5A:62:A6:3C:AE:BD:49:EE:C0:F5)
+ "master.puppet.com" (C0:E3:6B:76:36:EC:92:93:4D:BF:F0:8F:77:00:91:C8) 

[root@master ~]# tree /var/lib/puppet/ssl/                         #另外一种查看认证的方式
/var/lib/puppet/ssl/
├── ca
│   ├── ca_crl.pem
│   ├── ca_crt.pem
│   ├── ca_key.pem
│   ├── ca_pub.pem
│   ├── inventory.txt
│   ├── private
│   │   └── ca.pass
│   ├── requests
│   ├── serial
│   └── signed
│       ├── agent1_cert.puppet.com.pem  #已经注册成功
│       └── master.puppet.com.pem
├── certificate_requests
├── certs
│   ├── ca.pem
│   └── master.puppet.com.pem
├── crl.pem
├── private
├── private_keys
│   └── master.puppet.com.pem
└── public_keys
   └── master.puppet.com.pem


3、test模块测试

[root@agent1 ~]# puppet agent --test  #测试节点agent1
info: Caching catalog for agent1_cert.puppet.com
info: Applying configuration version '1385309582'
notice: /Stage[main]/test/File[/etc/test]/content: 
--- /etc/test    2016-12-13 07:18:52.000000000 +0800
+++ /tmp/puppet-file20161213-4571-1vqc18j-0    2016-12-13 02:51:47.000000000 +0800
@@ -0,0 +1,3 @@
+--                       --
+--------puppet test---------
+--                       --
info: FileBucket adding {md5}d41d8cd98f00b204e9800998ecf8427e
info: /Stage[main]/test/File[/etc/test]: Filebucketed /etc/test to puppet with sum d41d8cd98f00b204e9800998ecf8427e
notice: /Stage[main]/test/File[/etc/test]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}87ea3a1af8650395038472457cc7f2b1'
notice: Finished catalog run in 0.40 seconds


二 自动注册


这种注册方式简单来讲是通过master端的ACL列表进行控制的,安全系统较低,也就是说符合预先定义的ACL列表中的所有节点请求不需要确认都会被自动注册上,也就是说你只需要知道ACL列表要求,其次能和Master端通信便可轻易注册成功。当然,它的最大优点就是效率非常高。 


1、清除Master端已经注册的agent1的证书

[root@master ~]# puppet cert --clean agent1_cert.puppet.com

notice: Revoked certificate with serial 3
notice: Removing file Puppet::SSL::Certificate agent1_cert.puppet.com at '/var/lib/puppet/ssl/ca/signed/agent1_cert.puppet.com.pem'
notice: Removing file Puppet::SSL::Certificate agent1_cert.puppet.com at '/var/lib/puppet/ssl/certs/agent1_cert.puppet.com.pem'

[root@master ~]# puppet cert --list  --all             #agent1证书已经删除
+ "agent2_cert.puppet.com"      
+ "agent3_cert.puppet.com"       
+ "master.puppet.com"      
+ "master_cert.puppet.com" )


2、在agent1端删除注册过的证书

[root@agent1 ~]# rm -rf /var/lib/puppet/ssl/*


3、在master端编写ACL列表

[root@master ~]# vim /etc/puppet/autosign.conf    
*.puppet.com
[root@master ~]# /etc/init.d/master restart
Stopping master:                                     [  OK  ]
Starting master:                                     [  OK  ]
[root@master ~]# puppet cert --list  --all


4、自动注册

[root@agent1 ~]# puppet agent --test #申请证书
info: Creating a new SSL key for agent1_cert.puppet.com
info: Caching certificate for ca
info: Creating a new SSL certificate request for agent1_cert.puppet.com
info: Certificate Request fingerprint (md5): ED:C9:C7:DF:F1:0E:53:1C:D3:73:5D:B7:D3:94:1F:60
info: Caching certificate for agent1_cert.puppet.com
info: Caching certificate_revocation_list for ca
info: Caching catalog for agent1_cert.puppet.com
info: Applying configuration version '1394359075'
notice: Finished catalog run in 1.39 seconds
[root@agent1 ~]# cat /etc/test
--                       --
--------puppet test---------
--                       --

5、服务器端查看

[root@master ~]# puppet cert --list  --all      #查看agent已经自动注册成功

+ "agent1_cert.puppet.com"       
+ "agent2_cert.puppet.com"      
+ "agent3_cert.puppet.com"      
+ "master.puppet.com"     
+ "master_cert.puppet.com" 

三 预签名认证

预签名注册是在agent端未提出申请的情况下,预先在master端生成agent端的证书,然后复制到节点对应的目录下即可注册成功,这种方式安全系数最高,但是操作麻烦,需要提前预知所有节点服务器的certname名称,其次需要将生成的证书逐步copy到所有节点上去。不过,如果你的系统中安装了kickstart或者cobbler这样的自动化工具,倒是可以将证书部分转换成脚本集成到统一自动化部署中 注:生产环境中建议此方式进行注册,既安全又可靠!


1、清除master端已经注册的agent1的证书

[root@master ~]# puppet cert --clean agent1_cert.puppet.com
notice: Revoked certificate with serial 3
notice: Removing file Puppet::SSL::Certificate agent1_cert.puppet.com at '/var/lib/puppet/ssl/ca/signed/agent1_cert.puppet.com.pem'
notice: Removing file Puppet::SSL::Certificate agent1_cert.puppet.com at '/var/lib/puppet/ssl/certs/agent1_cert.puppet.com.pem'

2、在agent1端删除注册的所有信息,包括证书

[root@agent1 ~]# rm -rf /var/lib/puppet/*


3、删除自动注册ACL列表

[root@master ~]# mv /etc/puppet/autosign.conf{,.bak}


4、puppetserver端预先生成agent1证书

[root@master ~]# puppetca --generate agent1_cert.puppet.com
notice: agent1_cert.puppet.com has a waiting certificate request
notice: Signed certificate request for agent1_cert.puppet.com
notice: Removing file Puppet::SSL::CertificateRequest agent1_cert.puppet.com at '/var/lib/puppet/ssl/ca/requests/agent1_cert.puppet.com.pem'
notice: Removing file Puppet::SSL::CertificateRequest agent1_cert.puppet.com at '/var/lib/puppet/ssl/certificate_requests/agent1_cert.puppet.com.pem'


5、节点生成目录结构

[root@agent1 ~]# puppet agent --test --server=abc.com     #随便指定server端,生成目录结构
info: Creating a new SSL key for agent1_cert.puppet.com
err: Could not request certificate: getaddrinfo: Temporary failure in name resolution
Exiting; failed to retrieve certificate and waitforcert is disabled

[root@agent1 ~]# tree /var/lib/puppet/ssl/
/var/lib/puppet/ssl/
|-- certificate_requests
|-- certs
|-- private
|-- private_keys
|   `-- agent1_cert.puppet.com.pem
`-- public_keys
   `-- agent1_cert.puppet.com.pem


6、master端copy证书到agent1上

[root@master ~]# scp /var/lib/puppet/ssl/private_keys/agent1_cert.puppet.com.pem  agent1.puppet.com:/var/lib/puppet/ssl/private_keys/ 
agent1_cert.puppet.com.pem                                                         100% 3243     3.2KB/s   00:00    
[root@master ~]# scp /var/lib/puppet/ssl/certs/agent1_cert.puppet.com.pem  agent1.puppet.com:/var/lib/puppet/ssl/certs/
agent1_cert.puppet.com.pem                                                         100% 1944     1.9KB/s   00:00    
[root@master ~]# scp /var/lib/puppet/ssl/certs/ca.pem  agent1.puppet.com:/var/lib/puppet/ssl/certs/
ca.pem                                                                                 100% 1915     1.9KB/s   00:00    

7、agent1测试

[root@agent1 ~]# >/etc/test 
[root@agent1 ~]# puppet agent --test
info: Caching certificate_revocation_list for ca
info: Caching catalog for agent1_cert.puppet.com
info: Applying configuration version '1394359075'
notice: /Stage[main]/test/File[/etc/test]/content: 
--- /etc/test    2016-12-13 18:10:10.000000000 +0800
+++ /tmp/puppet-file20161213-4071-1gypudk-0    2016-12-13 18:10:17.000000000 +0800
@@ -0,0 +1,3 @@
+--                       --
+--------puppet test---------
+--                       --
info: FileBucket adding {md5}d41d8cd98f00b204e9800998ecf8427e
info: /Stage[main]/test/File[/etc/test]: Filebucketed /etc/test to puppet with sum d41d8cd98f00b204e9800998ecf8427e
notice: /Stage[main]/test/File[/etc/test]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}87ea3a1af8650395038472457cc7f2b1'
info: Creating state file /var/lib/puppet/state/state.yaml
notice: Finished catalog run in 0.41 seconds
[root@agent1 ~]# cat /etc/test 
--                       --
--------puppet test---------
--                       --


四 常见错误情况及解决办法



1.  连接master的时候出现如下报错:

dnsdomainname: Unknown host

解决办法:检查机器主机名的设置,以及是否添加进hosts。


2.   连接master的时候出现如下报错:

err: Could not request certificate: getaddrinfo: Name or service not known

解决办法:服务器端没有配置hosts域名绑定,在hosts中添加。


3.  连接master的时候出现如下报错:

warning: peer certificate won't be verified in this SSL session

解决办法:服务端还没有返回签发证书,使用puppet cert --list查看


4.  连接master的时候出现如下报错:

err: Could not retrieve catalog from remote server: certificate verify failed

解决办法:客户端和服务器端时间不同步,用ntpdate命令同步时间



5. Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not parse for environment production: Syntax error at ';'; expected '}' at /etc/puppet/manifests/site.pp:3 on node server-02
解决办法:修改site.pp ,脚本中出现语法错误


6. Error: /File[/var/lib/puppet/lib]: Could not evaluate: Could not retrieve information from environment production source(s) puppet://server-01/plugins

解决办法:分别在客户端和服务器修改puppet.conf文件,添加“pluginsync=false",并分别重启服务


7. 执行 puppet agent --test --server=server-01 提示:
Notice: Run of Puppet configuration client already in progress; skipping (/var/lib/puppet/state/agent_catalog_run.lock exists)
解决办法:出现此提示为 配置文件不正确,或者服务器端的.pp 文件的语法错误,请去修改.pp 文件


8. Error: Could not request certificate: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed: [self signed certificate in certificate chain for /CN=Puppet CA: server-01]
解决办法:删除客户端证书、删除puppetmaster上的客户端证书、客户端重新生成证书请求、puppetmaster重新签名
1)、rm /var/lib/puppet/ssl/ 证书
2)、puppet cert clean 客户端主机名
3)、puppet agent --test --server puppetmaster
4)、puppet cert --sign 客户端主机名


9.   certificate verify failed: [CRL is not yet valid for /CN=server.minunix.com]
解决方法:master和agent 把时间同步下就OK 了
ntpdate -u 210.72.145.44


10. Error: Host server01 failed: Error 403 on SERVER: Forbidden request: server01(172.16.8.250) access to /run/server01 [save] authenticated at :119
在puppet 服务端尝试推送数据来更新客户端的时候提示这样的问题;
解决办法:
# vim /etc/puppet/auth.conf
path /
auth any
allow *







【准备工作】

首先至少准备两台机器如果可以三台虚拟机是很好的
192.168.1.100服务端
192.168.1.101 客户端

两台机器关闭selinux清空iptables规则并保存设置hostname

编辑/etc/sysconfig/network  定义hostname

100上hostname   master.teng.com
101上hostname   client.teng.com

安装ntpdate并建立自动同步时间的任务计划

yum install -y ntp
crontab  -e   //加入
*/10  * * * * ntpdate time.windows.com          #微软的时间服务器

【安装服务】
服务端
rpm -ivh  
yum install -y    puppet-server     #安装服务端
service      puppetmaster  start
chkconfig  puppetmaster  on

客户端
rpm -ivh
yum install -y puppet          #安装客户端

vi /etc/puppet/puppet.conf

在最后面添加
listen = true
server = master.teng.com
runinterval = 30   //主动更新每隔30s


/etc/init.d/puppet start    #启动puppet服务

手动生成ssl证书

puppet agent  --test  --server  master.teng.com

【签发证书】

服务端签发所有ssl证书

puppet cert list --all      
      
服务端签发指定客户端证书

puppet cert --sign client.teng.com

签发成功会看到client.teng.com 的key并且在行首有一个 +如果没有说明还没有签发

服务端可以删除指定客户端证书

puppet cert clean client.teng.com

删除所有证书
puppet cert clean --all

服务端创建自动签发的配置文件 

vim /etc/puppet/puppet.conf   //在[main]下面加一行 autosign = true

vim /etc/puppet/autosign.conf  //加入如下内容 *.teng.com        重启服务端

注意当删除客户端证书后也要删除客户端的ssl相关文件  rm -rf /var/lib/puppet/ssl/*

puppet更新方式  
 
【简单测试】

服务端上

vi /etc/puppet/manifests/site.pp   //加入如下内容


node default {
file {
  "/tmp/123.txt": 
 content => "test,test";
}
}

客户端上

puppet agent --test --server  master.teng.com

这样会在客户端上生成一个 /tmp/123.txt的文件并且内容为 test,test

此时即配置连接成功了。
【模块化管理】

首先要理解几个概念模块、类、资源。 

模块是puppet的最大单元模块里面有类类下面有资源。

puppet管理的文件、用户、服务、任务计划等全部由这些单元组成。


下面我们来定义一个模块

在服务端上做如下操作

mkdir /etc/puppet/modules/testm  //模块名字就是testmmoudules下存放模块

cd !$
mkdir {files,manifests,templates}   

//一个模块下需要这三个目录files存一些文件可以为空manifests存配置文件templates存模板可以留空

touch manifests/init.pp  //这个是必须的
vi  manifests/init.pp   //内容如下


class testm{     // 这个testm是类
file {"/tmp/2.txt":
owner => "root",
group => "root",
mode => 0400,
source => "puppet://$puppetserver/modules/testm/1.txt"
}
}
说明类名字也叫做testm, 类下面定义了一个资源file文件名字叫做/tmp/2.txt ownergroupmode定义文件的属主、数组以及权限source定义这个文件从哪里获取。 $puppetserver一会也要定义一下这里指的是puppet server服务器上/etc/puppet/modules/testm/files/1.txt  
继续定义一个很关键的配置文件
vim  /etc/puppet/manifests/site.pp   //内容如下


$puppetserver = 'master.teng.com'    //或试试IP
node 'client.teng.com'{
    include testm            //模块名
}
说明$puppetserver 定义服务端的主机名node后面为客户端的主机名这里面定义该客户端要加载的模块
配置完成后在客户端执行命令
puppet agent --test  --server=master.aming.com   //如果客户端上启动了puppet服务不用执行这命令它也会自动同步的
上面的模块其实只是同步了一个文件而已那么要想同步一个目录如何做
mkdir /etc/puppet/modules/apache   并在目录下添加文件或目录

vi  manifests/init.pp  //添加以下内容


class apache{         
file {"/usr/local/apache2":
owner => "root",
group => "root",
source => "puppet://$puppetserver/modules/apache/apache2",
recurse => true,
purge => true
}
}
vim  /etc/puppet/manifests/site.pp   //内容如下


$puppetserver = 'master.teng.com'    //或试试IP
node 'client.teng.com'{
    include testm 
    include apache               //模块名
}
注其中recurse=>true 这个参数很关键它表示递归的意思没有这个不能同步目录。purge参数可以保证当服务端删除某个文件客户端可以跟着删除。

【远程执行命令】
vi  manifests/init.pp  //添加以下内容


class apache{         
file {"/usr/local/apache2":
owner => "root",
group => "root",
source => "puppet://$puppetserver/modules/apache/apache2",
recurse => true,
purge => true
}
exec {"123":     //exec是资源123是资源名字
unless => "test -f /tmp/teng.txt",
path => ["/bin", "/sbin", "/usr/bin", "/usr/sbin"],   //定义环境变量
command => "/bin/touch /tmp/teng.txt"
}
}
注unless后面的命令作为一个条件当条件成立时不会执行下面的命令如果想要条件成立时执行下面的命令用 onlyif。要注意的是我们一定要给执行的这条命令加个条件使用unless就可以必须满足这个条件才能执行命令否则这个命令会一直执行不太 妥当。
可以等30秒让客户端自动同步也可以手动同步
puppet agent --test  --server=master.aming.com
可以客户端查看日志看输出了什么
tail -f /var/log/messages

【添加cron】
vi  manifests/init.pp  //添加以下内容


class apache{         
file {"/usr/local/apache2":
owner => "root",
group => "root",
source => "puppet://$puppetserver/modules/apache/apache2",
recurse => true,
purge => true
}
exec {"123":     //exec是资源123是资源名字
unless => "test -f /tmp/teng.txt",
path => ["/bin", "/sbin", "/usr/bin", "/usr/sbin"],   //定义环境变量
command => "/bin/touch /tmp/teng.txt"
}
cron {"time":
command => "/sbin/ntpdate time.windows.com",
user => "root",
minute => "*/10",
# ensure => "absent"  //当增加了这行配置则会把该cron删除掉
}
}
注分时日月周分别对应puppet里面的minutehourmonthdaymonthweekday
查看客户端的日志和cron观察日志输出的内容和crontab中是否已添加。

阅读(7695) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~