您的位置:首页 > 其它

puppet学习笔记

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

二、puppet工作流程



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[/code]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} #依次安装包
5、hash
{ key1 => value1,key2 => value2,...}
6、undef,声明未定义的东西不能加上引号的。
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  puppet