puppet学习笔记

 

一、Puppet基础原理:

Puppet是一款使用GPLV2X协议授权的开源管理配置工具,用ruby语言开发,既可以通过客户端服务器的方式运行,也可以独立运行。puppet可以为系统管理员提供方便,快捷的系统自动化管理。

 

二、puppet工作流程

[LZGXC485O2WCC`{HF(V74Y.png

1. 客户端 puppet-client 向 puppet-master 发起认证请求,或使用带签名的证书。

2. puppet-master 告诉 puppet-client 是合法的。

3. puppet-client 调用 facter, Facter 探测出主机的一些变量, 例如主机名、 内存大小、 IP 地址等,puppet-client 将这些信息通过 SSL 连接发送到服务器端。

4. puppet-master 服务器端检测客户端的主机名,然后找到 manifest 对应的 node 配置,并对该部分内容进行解析。facter 送过来的信息可以作为变量处理,node 牵涉到的代码才解析,其他没牵涉的代码不解析。解析分为几个阶段,首先是语法检查,如果语法错误就报错;如果语法没错,就继续解析,解析的结果生成一个中间的“伪代码”(catelog),然后把伪代码发给客户端。

5. puppet-client 端接收到“伪代码”,并且执行。

6. puppet-client 端在执行时判断有没有 file 文件,如果有,则向 fileserver 发起请求。

7. puppet-client 端判断有没有配置 report,如果已配置,则把执行结果发送给服务器。

8. puppet-server 端把 puppet-client 端的执行结果写入日志,并发送给报告系统。

 

三、puppet安装

1、直接通过yum安装老系统自带版本。

yum install puppet -y

2、安装最新版本

sudo rpm -ivh https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm

2.1、安装puppet-server

sudo puppet resource package puppet-server ensure=latest

2.2、安装agent

sudo puppet resource package puppet ensure=latest

四、puppet资源管理

Puppet中的资源是puppet工具的核心,它是通过puppet管理配置系统的最小单位。

1、查看资源类型

puppet describe -l

2、查看资源摘要

puppet describe -s <resource_name>

3、查看资源详细用法

puppet describe <resource_name>

4、资源的基本格式

资源名 { '标题':
       属性1 => '值',
       属性2 => '值',
}
#以安装httpd为例
package { 'httpd':
       ensure => 'present',
       provider => 'rpm',
}

puppet常用资源:file,filebucket,host,group,package,service,exec,cron,notify 等。

5、资源公有属性:

before :指明资源要在某个资源之前运行

require:指明某个资源要在某个资源之后运行。

notify: 主动通知其他资源,本资源的状态

subscibe :被动通知,当它检测到资源状态发生改变的时候,主动更新所在资源状态。

还可以使用

-> 表示资源前后关系

~> 表示资源之间的通知


五、puppet语言

1)、puppet变量:

       1、名称之前必须以$开头,赋值用=,支持追加赋值+=

       2、变量名称有两种格式,简短名称,FQN($scope::variable)

              $webserver = "httpd"

              package {"httpd":

                     ensure => "present",

                     name => $webserver

              }

     3、作用域:top > node > local 作用域越小,优先级越高

2)、数据类型:

       1、直接字串

              可以使用引号,也可以不用。

              换行符为\n,windows\r\n

       2、布尔型

              true,false

              其它类型会自动转换为布尔型。

              所有数字都是true

              空字符串为false,其它字符串为true

       3、数值

              整数

              浮点数

       4、数组,逗号隔开

              $array = ['httpd','mysql','php']

              package {$array:ensure => installed} #依次安装包

       5hash

              { key1 => value1,key2 => value2,…}

       6undef,声明未定义的东西不能加上引号的。

3)、puppet支持的操作符和对应的表达式:

    比较操作符:

       ==

       !=

       <,>,<=,>=,

       =~ 正则匹配

       !~ 正则不匹配

       in

   布尔操作符:

       and

       or

       !

   算术运算

       +

       –

        /

        *

        <<   左移

        >>   右移

       $osfamily == 'CentOS'  
       $kernel in ['Linux','solaris','freebsd']

4)、puppet的条件判断语句:

       if ..elsif..else

       case

       selector语句 #意思是在两个选项中任选其中一个赋值

  if $operationsystem == 'CentOS'{
              notice("welcome to CentOS")
       }
       elsif $operationsystem == 'Redhat' {
              notice("Welcome to Redhat")
       }
       elsif $operationsystem == 'Fedora' {
              notice("Welcome to Fedora")
       }
       else{
              notice('Welcome to ET')
       }
case $operationsystem {
       'Solaris':  { include role::solaris }
       'Redhat','CentOS' : { include role::redhat }
       /^(Debian|Ubuntu)$/ : { include role::debian }
       default : { include role::generic }
}

$webserver = $operatingsystem ? {
        /(?i-mx:'ubuntu'|debian)/ => 'apache',
        /(?i-mx:redhat|centos|fedora)/ => 'httpd',
        default => 'httpd'
}
i:表示忽略大小写
- : 表示不使用某转移符号
m:表示把 "." 当做换行符使用
x :表示互略模式中空白字符和注释。

六、puppet类和模块

类是具有相同特性和行为的集合。就是一组代码块,在需要时可以通过名称进行调用。只定义类,并不会调用,需要声明才可以。

1)、语法:

class class_name [inherits] [base_class] {
       正常的puppet代码
}

如果在同个模块定义了多个类, 可以采双冒号( :: ) 。 例如定义个nginx模块, 模块中 定义三个类:

class nginx { … }
class nginx::config { … }
class nginx::vhost { … }

  2)、类的继承(基类不能有参数):

         1、继承资源属性

         2、覆盖资源属性

               =>

         3、追加资源属性

              +>

3)、 模块

  模块结构

module name
       mainfests
              init.pp  #必须至少声明一个类。类与模块名相同
              *.pp
               # mudule_name::[subdirname]::mainfect_name
       files:包含的是一个静态文件。puppet的agentmaster模型。
              puppet:///modles/module_name/[subdir_name/]file_name
       templates:模板文件 *.erb 用到ruby语言
              template('');
              content => template('模板文件'),
       lib #插件目录。
       tests :当前模块的使用帮助或者实例文件
       spec :为lib目录的插件提供使用说明,范例的。

七、事例,puppet部署LNMP

1、假定已经安装好puppet-server。

2、主机名通信

cat >> /etc/hosts <<EOF
192.168.198.139 puppet-server
192.168.198.160 puppet-client
EOF

3、提供puppet文件

mkdir /etc/puppet/modules/lnmp/{manifests,files,templates,tests} -p
vim /etc/puppet/modules/lnmp/manifests/init.pp
class lnmp {
       include lnmp::nginx
       include lnmp::mysql
       include lnmp::php
}
vim /etc/puppet/modules/lnmp/manifests/nginx.pp
class lnmp::nginx {
    package{'nginx':
        ensure  => present,
        name    => nginx,
    }
    file{'nginx.conf':
        ensure  => file,
        source  => 'puppet:///modules/lnmp/nginx.conf',
        path    => '/etc/nginx/nginx.conf',
        require => Package['nginx'],
    } 
   service{'nginx':
        ensure  => true,
        enable  => true,
        subscribe => File['nginx.conf'],
    }
}
 vim /etc/puppet/modules/lnmp/manifests/php.pp
class lnmp::php {
    package{'php-fpm':
        ensure  => present,
        name    => php-fpm,
    }
 
    file{'www.conf':
        ensure  => file,
        source  => 'puppet:///modules/lnmp/www.conf',
        path    => '/etc/php-fpm.d/www.conf',
        require => Package['php-fpm'],
    }
 
   service{'php-fpm':
        ensure  => true,
        enable  => true,
        subscribe => File['www.conf'],
    }
}

 vim /etc/puppet/modules/lnmp/manifests/mysql.pp
 class lnmp::mysql {
    package{'mysql-server':
        ensure  => present,
        name    => 'mysql-server',
    }
 
    file{'my.cnf':
        ensure  => file,
        source  => 'puppet:///modules/lnmp/my.cnf',
        path    => '/etc/my.cnf',
        require => Package['mysql-server'],
    }
 
   service{'mysqld':
        ensure  => true,
        enable  => true,
        subscribe => File['my.cnf'],
    }
}
vim /etc/puppet/manifests/site.pp
node 'puppet-client' {   
 include lnmp
}

4、提供服务配置文件

cp /root/files/{nginx.conf,www.conf,my.cnf} /etc/puppet/modules/lnmp/files/

5、启动puppet服务

[root@puppet-server modules]# puppet master --verbose --no-daemonize   #第一次启动以便观察信息
Info: Creating a new SSL key for ca
Info: Creating a new SSL certificate request for ca
Info: Certificate Request fingerprint (SHA256): 7B:A9:AB:84:C0:EB:DC:83:0E:EA:8C:81:1E:25:9A:47:5C:3F:10:31:6F:F7:5C:25:BE:B7:41:3C:B8:6B:35:38
.....
[root@puppet-client ~]# puppet agent server --server puppet-server --verbose --no-daemonize  #客户端申请证书
[root@puppet-server ~]# puppet cert sign puppet-client   #服务器签署证书
#稍等一会
[root@puppet-client ~]# ss -tnl | egrep "80|3306|9000"
LISTEN     0      128                       *:9000                     *:*     
LISTEN     0      50                        *:3306                     *:*     
LISTEN     0      128                       *:80                       *:*


八、总结

供自己以后参考。会不断完善

参考:

http://cuchadanfan.blog.51cto.com/9940284/d-11

http://scholar.blog.51cto.com/9985645/1673562

原创文章,作者:艾贺,如若转载,请注明出处:http://www.178linux.com/9095

(0)
上一篇 2015-11-04 09:55
下一篇 2015-11-05 10:15

相关推荐

  • 文本处理工具之AWK

    概述:     在之前的文章中,我们介绍过文本处理三剑客的grep、sed,本篇就简要说明下awk的用法。主要是围绕常见的一些用法进行展开,分为以下几个部分:     1、awk的基础语法     2、awk的进阶语法  &nbs…

    Linux干货 2016-09-20
  • 马哥教育网络第21期-第十三周课程练习

    1、建立samba共享,共享目录为/data,要求:(描述完整的过程)   1)共享名为shared,工作组为magedu;   2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名; &n…

    Linux干货 2016-12-26
  • N24_阿龙弟弟 学习计划/目标/宣言

    嗨,大家好,这是我的第一篇文章。很高兴来到马帮门徒这个大家庭。 学习计划:没有自己的计划,跟着马哥课程的进度来吧,以周为单位,确保每周任务完成; 学习目标:掌握应有的运维能力,做一名合格的Linux运维工程师,提升自我价值,过更好的生活; 学习宣言:Be a better man(Not Only Linux)!

    Linux干货 2016-10-25
  • SQL优化大全

    1. 优化SQL步骤 1. 通过 show status和应用特点了解各种 SQL的执行频率        通过 SHOW STATUS 可以提供服务器状态信息,也可以使用 mysqladmin extende d-status 命令获得。 SHOW STATUS 可以根据需要显示 session 级别的统计结果和 g…

    Linux干货 2015-04-13
  • DNS服务器—-主从服务器搭建

    一、环境准备   1、准备三台测试       主DNS服务器地址:192.168.10.203       从DNS服务器地址:192.168.10.103       测试主机地址:  192.168.10.120 …

    Linux干货 2015-05-18
  • Linux基础知识(五)

    1、显示当前系统上root、fedora或user1用户的默认shell [root@server01 ~]# cat /etc/passwd | grep –color=auto -E "^root|fedora|user1" | cut -d : -f 1,7 2、找出/etc/rc.d/init.d/functions文…

    Linux干货 2016-10-25

评论列表(2条)

  • stanley
    stanley 2015-11-04 09:58

    已置顶,很不错,可以考虑出系列文章。

  • 云中鹤
    云中鹤 2015-11-04 10:02

    过奖了,才提交申请的时候。心里提醒自己,下次不能这样写了,单纯的注意布局,对知识的理解程度不够深,参考别人的东西不少,以后要写出真正有价值的东西。