|
puppet是一种Linux、Unix平台的集中配置管理系统,使用ruby语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。
Puppet是一个C/S架构的配置管理工具,在中央服务器上安装puppet-server软件包(被称作Puppetmaster)。在需要管理的目标主机上安装puppet客户端软件(被称作PuppetClient)。当客户端连接上Puppetmaster后,定义在Puppetmaster上的配置文件会被编译,然后在客户端上运行。每个客户端默认每半个小时和服务器进行一次通信,确认配置信息的更新情况。如果有新的配置信息或者配置信息已经改变,配置将会被重新编译并发布到各客户端执行。也可以在服务器上主动触发一个配置信息的更新,强制各客户端进行配置。如果客户端的配置信息被改变了,它可以从服务器获得原始配置进行校正。
puppet与其他手工操作工具有一个最大的区别就是 puppet的配置具有稳定性,因此你可以多次执行puppet, 一旦你更新了你的配置文件,puppet就会根据配置文件来更改你的机器配置,通常每30分钟检查一次. puppet会让你的系统状态同配置文件所要求的状态保持一致. 比如你配置文件里面要求ssh服务必须开启. 假如不小心ssh服务被关闭了,那么下一次执行puppet的时候,puppet会发现这个异常,然后会开启 ssh 服务. 以使系统状态和配置文件保持一致.puppet就象一个魔术师,会让你的混乱的系统收敛到puppet配置文件所想要的状态.
puppet工作流程:
1、客户端通过facter收集客户端信息并发送至服务端
2、连接服务端并请求catalog日志
3、请求节点(node)的信息
4、从服务器端接收节点(node)的实例
5、编译代码(包括语法检查等工作)
6、查询是否有exported 虚拟资源.如有,则从数据库接收虚拟资源
7、接收完整的catalog日志
8、存储catalog日志到数据库
9、客户端接收完整的catalog日志
下面是puppet的一些简单配置:(系统环境为rhel6.5,iptables and selinux disable)
master端:172.25.254.1 server1.example.com
client端:172.25.254.2 server2.example.com
client端:172.25.254.3 server3.example.com
master端安装:
yum install puppet-server-3.8.1-1.el6.noarch.rpm puppet-3.8.1-1.el6.noarch.rpm -y 所需的依赖性:
facter-2.4.4-1.el6.x86_64.rpm hiera-1.3.4-1.el6.noarch.rpm rubygem-json-1.5.5-3.el6.x86_64.rpm ruby-shadow-2.2.0-2.el6.x86_64.rpm ruby-augeas-0.4.1-3.el6.x86_64.rpm rubygems-1.3.7-5.el6.noarch.rpm -y client端安装:
yum install puppet-3.8.1-1.el6.noarch.r pm -y #所需的依赖性与master端一样 在master端启动master服务
/etc/init.d/puppetmaster start #在master端启动puppet服务 用puppet客户端连接puppet master:
puppet agent --server=server1.example.com --no-daemonize -vt #--server服务端主机 --no-daemonize不再后台运行 -v显示详细日志,-t测试, 提示client向master端发出证书验证请求,需要master端签名并返回
在master端进行操作:
puppet cert list #显示等待签名的证书
手动签名证书:
puppet cert sign server2.example.com
自动验证签名:
vim /etc/puppet/puppet.conf #puppet的主配置文件
autosign = true #允许所有客户端的认证,要添加在main下面
vim /etc/puppet/autosign.conf #创建autosign.conf目录用于添加白名单
*.example.com #允许所有example.com 域的主机
puppet cert list --all #显示所有已经签名的证书
此时在客户端执行
puppet agent --server=server1.example.com --no-daemonize -vt 可以看到如下输出:
pupet的资源定义:
在master端:
cd /etc/puppet/manifests/ #文件存储目录(puppet 会先读取该目录的.PP 文件 ) 1.创建文件:
vim site.pp #定义 puppet 相关的变量和默认配置,这个文件必须存在,puppet执行的第一个代码就在这个文件中,而且其他的代码也要通过该文件调用
file {
'/mnt/test': #要创建的文件
content => 'hello world' #文件内容
mode => 600, #文件权限
owner => puppet, #文件所有人
group => puppet #文件所有组
}
在客户端
puppet agent --server=server1.example.com --no-daemonize -vt #因为加了-t参数所以puppet agent不会处于运行状态,需要再次执行这个指令去同步master端的资源更改
[root@server2 ~]# cat /mnt/test
hello world[root@server2 ~]#
[root@server2 ~]# ll /mnt/test
-rw------- 1 puppet puppet 11 Jul 4 09:51 /mnt/test
2.创建master端有的文件
mkdir /etc/puppet/files
cp /etc/passwd /etc/puppet/files/
vim etc/puppet/fileserver.conf #文件服务器配置文件
[files]
path /etc/puppet/files
allow *.example.com
/etc/init.d/puppetmaster reload
vim /etc/puppet/manifests/site.pp
file {
'/mnt/passwd':
source => 'puppet:///files/passwd'
}
3.软件包定义:
vim /etc/puppet/manifests/site.pp
package {
'httpd': #软件包称
ensure => present
}
4.服务定义:
service {
'httpd':
ensure =>running,
require => Package['httpd'] #设置启动要求有,必须要有否则会出现先启动服务后安装软件包的情况,从而报错。
}
5.添加用户:
user {
'test':
uid => 900,
home =>'/home/test',
shell =>'/bin/bash',
provider =>useradd,
managehome =>true,
ensure =>present,
}
exec {
'echo westos | passwd --stdin test': #为test用户添加密码
path => "/usr/bin:/bin", #所用脚本的路径
onlyif => 'id test' #需要有test用户
}
6.自动挂在文件系统并同步fstab文件:(这里用的是nfs共享的文件)
package {
'httpd':
ensure => present;
'nfs-utils':
ensure => present
}
file {
"/public":
ensure =>directory
}
mount {
"/public":
device => "172.25.254.55:/mnt",
fstype => "nfs",
options => "defaults",
ensure => mounted #如需卸载改为absent
}
7.创建计划任务
cron { echo:
command => "/bin/echo `/bin/date` >> /tmp/echo",
user => root,
hour => ['2-4'],
minute => '*/10'
}# 任务会在 client 上/var/spool/cron 目录中生成。
不同节点的定义
mkdir /etc/puppet/manifests/nodes 定义server2主机的资源
vim /etc/puppet/manifests/nodes/server2.example.com.pp
node 'server2.example.com'{
package {
'httpd':
ensure => present
}
service {
'httpd':
ensure =>running,
require => Package['httpd']
}
}
定义server3主机的资源
vim /etc/puppet/manifests/nodes/server3.example.com.pp
node 'server2.example.com'{
package {
'httpd':
ensure => present
}
service {
'httpd':
ensure =>stopped,
require => Package['httpd']
}
}
vim /etc/puppet/manifests/site.pp
import 'nodes/*.pp'
注意各节点的资源不要在site.pp中重复出现以免发生冲突在client端出现如下错误:
编写模块:
cd /etc/puppet/modules/ #模块目录
mkdir httpd #httpd模块
cd httpd
mkdir files #用于放置httpd的配置文件
cp /etc/httpd/conf/httpd.conf files/
mkdir manifests #用于放置模块的伪代码
cd manifests
vim install.pp
class httpd::install {
package{
'httpd':
ensure => present
}
}
vim config.pp
class httpd::config {
file {
'/etc/httpd/conf/httpd.conf': #客户端httpd的配置文件存放处
source =>'puppet:///modules/httpd/httpd.conf' #puppet会自动到htppd类的files里找配置文件。
require =>Class['httpd::install'],
notify =>Class['httpd::service']
}
}
vim service.pp
class httpd::service{
service{
'httpd':
ensure =>running
require =>Class ['httpd::install','httpd::config']
}
}
vim init.pp
class httpd {
include httpd::install,httpd::config,httpd::service
}
/etc/puppetmaster reload
重新加载服务后就可以使用这个模块了
vim /etc/puppet/manifests/nodes/server2.example.com.pp
node 'server2.example.com'{
include httpd
}
给模块添加模版应用
cd /etc/puppet/modules/httpd/
mkdir templates #用于存放模版应用
vim httpd_vhost.erb #添加模版,必须以*.erb结尾,这里添加的是httpd的虚拟主机模版
<VirtualHost *:80>
ServerName <%= domainname %>
DocumentRoot /var/www/<%= domainname %>
ErrorLog logs/<%= domainname %>_error.log
CustomLog logs/<%= domainname %>_access.log common
</VirtualHost>
vim /etc/puppet/modules/httpd/files/httpd.conf #开启httpd中的虚拟主机功能
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName server2.example.com
</VirtualHost>
vim /etc/puppet/modules/httpd/manifests/init.pp
define httpd::vhost($domainname) {
file { "/etc/httpd/conf.d/${domainname}_vhost.conf":
content => template("httpd/httpd_vhost.erb"),
require => Class["httpd::install"],
notify => Class["httpd::service"]
}
file { "/var/www/$domainname":
ensure => directory
}
file { "/var/www/$domainname/index.html":
content => $domainname
}
}
在server2节点上应用这个模版
vim /etc/puppet/manifests/nodes/server2.example.com.pp
node 'server2.example.com'{
include httpd
httpd::vhost {'www.example.com':
domainname => "www.example.com", }
httpd::vhost {'www.linux.com':
domainname => "www.linux.com", }
}
在server2主机上同步后就可以使用虚拟主机了
|
|