Puppet是一种Linux,Unix平台的集中配置管理系统,此系统能够管理机器里面诸如文件,用户,进程,软件包等资源,其设计目标就是简化对这些资源的管理以及妥善处理资源之间的依赖关系。 使用puppet,可以运行一个服务器端,然后每个客户端通过ssl证书连接服务器,得到本机的服务配置列表,然后更新配置列表来完成本机的配置工作,在大规模的生产环境中,如果只有一台puppetmaster是忙不过来的,因为puppet是用ruby语言写的,而ruby是解析型语言,每个客户端来访问,都要解析一次,客户端多了就忙不过来了,可以通过利用web代理软件来配合做puppetmaster作集群配置,扩展成一个服务器组。
工作原理如下: 1.客户端 Puppetd 向 Master 发起认证请求,或使用带签名的证书 2.Master 签名证书确认 Client 端是合法的。 3.客户端 Puppetd 调用 Facter,Facter 探测出主机的一些变量,例如主机名、内存大小、IP 地址等。Puppetd 将这些信息通过 SSL 连接发送到服务器端。 4.服务器端的 Puppet Master 检测客户端的主机名,然后找到 manifest 对应的 node 配置,并对该部分内容进行解析。Facter 送过来的信息可以作为变量处理,node 牵涉到的代码才解析,其他没牵涉的代码不解析。解析分为几个阶段,首先是语法检查,如果语法错误就报错;如果语法没错,就继续解析,解析的结果生成一个中间的“伪代码”(catelog),然后把伪代码发给客户端。 5.客户端接收到“伪代码”,并且执行,在执行时判断有没有 File 文件,如果有,则向 fileserver 发起请求。判断有没有配置 Report,如果已配置,则把执行结果发送给服务器。 6.服务器端把客户端的执行结果写入日志,并发送给报告系统。
Puppet服务的配置:
系统环境:RHEL6.5 Selinux and iptables disabled Server:172.25.45.15 pupmaster.example.com puppet master Client:172.25.45.16slave1.example.compuppet slave Client:172.25.45.17slave1.example.compuppet slave 注意点:server与所有client之间需要解析,以及时间同步,不然会验证失败。
Server端: #yum install -y puppet-server-3.8.1-1.el6.noarch.rpm puppet-3.8.1-1.el6.noarch.rpm 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 # /etc/init.d/puppetmaster start #启动puppetmaster服务。
Agent端: #yum install -y puppet-3.8.1-1.el6.noarch.rpm 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 #puppet agent --server pupmaster.example.com --no-daemonize -vt # --server服务器端主机,--no-daemonize : 客户端运行在前台 -v : 显示详细的日志,-t : 仅仅测试
Client向master端发出证书验证请求,然后等待master签名并返回证书,在master端: # cd /var/lib/puppet/ssl # puppet cert list #显示所有等待签名的证书 #puppet cert sign slave1.example.com #签名证书 (如果同时签名所有证书,执行以下命令:) #puppet cert sign --all #puppet cert clean slave1.example.com #删除签名证书 在对证书签名后,在slave1端上执行命令: #puppet agent --server pupmaster.example.com --no-daemonize -vt #可以看到如下输出;
上面是手工签名证书,还有一种自动签名证书,配置方法如下:
@@在 server 端, 编辑 puppet.conf 文件: #vim /etc/puppet/puppet.conf [main] autosign = true #允许所有客户端的认证。 #vim /etc/puppet/autosign.conf #最后一行添加: *.example.com #表示允许所有.example.com域的主机 #/etc/init.d/puppetmaster reload #重新导入服务。 在client端只需执行:# puppet agent或者 #/etc/init.d/puppet start 注意:在实际中有时可能会修改 client 端的主机名,这样就需要重新生成证书: 1)在 server 端执行: puppet cert --clean slave1.example.com #你要删除的原 client 端主机名 2)在 client 端执行:#rm -fr /var/lib/puppet/ssl/* #puppet agent --server puppet.example.com 重新生成认证证书。
#############puppet 资源定义########################### Puppet的第一个执行的代码是在/etc/puppet/manifest/site.pp,因此这个文件必须要存在。一般将资源均定义在/etc/puppet/manifest/site.pp 文件中,在没有指定节点的情况下,对所有已经经过验证的 client 都生效。 一.创建文件 #vim /etc/puppet/manifests/ site.p file { '/tmp/hello': #创建/tmp/helo文件 mode => 777, #更改文件的权限为777 owner => postfix, #更改文件所有者为postfix group => postfix #更改文件所有组为postfix }
另外一种创建文件方式,在master上新建立文件,添加上相应的内容,然后将此文件放到/etc/puppet/files 目录里面,编辑fileserver.conf配置文件,如下: #vim /etc/puppet/fileserver.conf [files] path /etc/puppet/files #访问的路径 allow *.example.com#允许的主机, #/etc/init.d/puppetmaster reload #重起服务 #vim site.pp file { '/mnt/passwdmin':#在slave上建立passwdmin文件 source => "puppet:///files/passwd", #内容来自于files目录里的passwd内容。 mode => 666, owner => postfix }
2.用户和组的建立 #vim /etc/puppet/site.pp group { 'liumin':#组名 gid => 1001 #指定gid=1001 } user { #用户的建立 'liumin': uid => 1000, gid => 1001, home => '/home/minmin', #指定用户家目录 shell => '/bin/bash', #指定shell类型 password => 'westos' #用户密码; } 建立用户家目录的文件, file { '/home/liumin': owner => liumin, group => liumin, mode => 700, ensure => directory } file { '/home/liumin/.bash_logout': source => '/etc/skel/.bash_logout', owner => liumin, group => liumin } file { '/home/liumin/.bash_profile': source => '/etc/skel/.bash_profile', owner => liumin, group => liumin } file { '/home/liumin/.bashrc': source => '/etc/skel/.bashrc', owner => liumin, group => liumin } 还有另外一种方式建立用户:这种方式简洁; user { 'test': uid => 900, home => '/home/test', shell => '/bin/bash', provider => useradd, managehome => true, ensure => present } exec { 'echo westos | passwd --stdin test': path => '/usr/bin:/usr/sbin:/bin', onlyif => 'id test' }
2.软件包及服务定义 #vim site.pp package { “httpd”: ensure => present; #表示安装软件包 “vsftpd”: ensure => absent #表示卸载软件包 } service { "httpd": ensure => running; #运行httpd服务 “vsftpd”: ensure => stopped #停止httpd服务 } 4. crontab 定时任务 cron { echo: command => "/bin/echo `/bin/date` >> /tmp/echo", user => root, hour => ['2-4'], minute => '*/10' }##任务会在 client 上/var/spool/cron 目录中生成。
二.不同节点的定义:这样的好处就是不同的节点可以进行不同的操作。具有非常好的扩展性和灵活性。 1. 在 puppetmaster 上编辑 site.pp # vim /etc/puppet/manifests/site.pp import "nodes/*.pp" #导入节点的资源配置文件 2. 建立节点文件 #mkdir etc/puppet/manifests/nodes #建立节点目录; #touch slave1.pp slave2.pp #建立节点的文件 #vim slave1.pp node 'slave1.example.com' { file { "/var/www/html/index.html": content => "slave1.example.com" } } 节点二依次类推:注意,也可以把上面的软件包及服务也加如今去,例如可以使得slave1装httpd服务,slave2装vsftpd服务等操作;
三.编写模块: #cd /etc/puppet/modules #mkdir htttpd vsftpd #创建两个模块,分别为httpd,vsftpd。 #cd httpd ---> mkdir files manifests templates #cd /etc/puppet/modules/httpd/manifests #touch install.pp init.pp config.pp service.pp #vim install.pp class httpd::install { package { 'httpd': ensure => present } } #vim config.pp class httpd::config { file { '/etc/httpd/conf/httpd.conf': source => 'puppet:///modules/httpd/httpd.conf', #####实际路径在/etc/puppet/modules/httpd/files/httpd.conf mode => 600, 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 } #vim /etc/puppet/manifests/nodes/slave1.pp include httpd :添加httpd模块。 client端进行验证: # puppet agent --server pupmaster.example.com --no-daemonize -vt
四.模板应用(添加虚拟主机配置):文件存放在 templates目录中,以*.erb 结尾。 # vim /etc/puppet/modules/httpd/templates/httpd_vhost.erb <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/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 } } # vim /etc/puppet/manifests/nodes/slave1.pp node 'slave1.example.com' { include httpd httpd::vhost { 'www.example.com': domainname => "www.example.com", } httpd::vhost { 'www.linux.com': domainname => "www.linux.com", } } Slave1客户端上进行验证: # puppet agent --server pupmaster.example.com --no-daemonize -vt 然后在FIREFOX中输入slave1的ip 地址进行验证,前提是真机要对slave1进行解析. #vim /etc/hosts
|