Linux之puppet

2016-04-15  本文已影响1376人  魏镇坪

puppet简介

puppet是一套IT基础设施自动化管理工具,可以管理其整个生命周期,其官方网站:www.puppetlabs.org。其作者Luke Kanies成立了puppetLabs,于2005年发布0.2版本。puppet基于ruby语言研发,puppet有声明性、基于模型的配置语言,其也有自己的配置编程言。

puppet的优势

puppet的版本

puppet的工作原理

因此,基于此种工作架构,仅在master端提供一个或少量几个Manifest即可实现管理大量的节点,并能提供更加安全的工作过程

puppet的三个层次概念

定义资源的依赖关系

puppet的术语

是一个完整的功能,包含清单和清单所依赖的文件

针对一台客户端,它所需要的所有模块

puppet程序的安装

客户端

服务端

puppet命令

用法格式:puppet <subcommand> [options] <action> [options]

获取所支持的所有资源类型
查看资源的使用帮助

定义资源常见语法

type {"title":
    attribute => value,
    attribute => value,
}

注意:type必须小写, tile在同一类型下必须唯一

puppet资源类型

group资源类型

user资源类型

1、puppet添加组及用户的示例:

group {'hadoop':
        ensure => present,
        gid => 200,
        system => true,
}

user {'docker':
        ensure => present,
        uid => 5000,
        system => false,
        groups => hadoop,
        home => "/home/docker",
        password => "zhenping.com",
        salt => 3kkxxkjfk,
        shell => "/bin/bash",
        comment => "docker virtual teach",
}

file资源类型

2、远程文件复制示例:
file {"/etc/nfs.conf":
        source => [
                "puppet:///modules/nfs/conf.$host",
                "puppet:///modules/nfs/conf.$operatingsystem",
                "puppet:///modules/nfs/conf"
        ]
}


3、向文件中添加文本内容
file {"/tmp/puppet.txt":
        ensure => file,
        content => "This is a puppet write content .",
        owner => docker,
        group => docker,
        mode => 700,
        replace => true,
}

4、创建文件示例
file {"/tmp/test4.txt":
        ensure => present,
}

5、创建目录示例:
file {"/tmp/test":
        ensure => director,
}

6、创建软连接
file {"/tmp/fstab_test.link":
        ensure => link,
        target => "/etc/fstab",
}
    将/tmp/fstab_test.link 连接至/etc/fstab

exec资源类型

使用特性
常用属性
7、判断ext4模块是否被装载,如果未装载执行装载操作
exec {"modprobe ext4":
    path => ["/usr/lib64/qt-3.3/bin","/usr/local/sbin","/usr/local/bin","/usr/sbin","/usr/bin:/root/bin"],   #导出PATH环境变量
    unless => "lsmod | grep ext4",  #当此操作发现未找到ext4模块,返回代码非零,才执行装载操作
}

8、判断ext4模块是否在,如果存在才被装载
exec {"modprobe ext4":
    path => ["/usr/lib64/qt-3.3/bin","/usr/local/sbin","/usr/local/bin","/usr/sbin","/usr/bin","/root/bin"],
    onlyif => "modinfo ext4",  #此命令返回状态码为0时,才执行Modprobe的命令
}


9 当用户创建成功时,才生成weizi.notify的文件
group {"weizi":
    ensure => present,
    gid => 10000,
    notify => User["weizi"],
}
user {"weizi":
    ensure => present,
    uid => 10000,
#   notify => Exec["/bin/echo hello > /tmp/weizi.notify"],
}

exec {"/bin/echo hello > /tmp/weizi.notify":
    subscribe => User["weizi"],
    refreshonly => true,
}


exec {‘/bin/echo hello > /tmp/hello.txt’:
            user => root,
            group => root,
            creates => ‘/tmp/hello.txt’,
        }
            
    
exec {‘/bin/echo hello > /tmp/hello2.txt’:
            user => root,
            group => root,
            unless => ‘/usr/bin/test -e /tmp/hello2.txt’,
        }

notify资源类型

显示通知信息

nofify {"hello world":}

cron资源类型

管理计划任务

常用选项
10、建立系统时间同步任务
cron {"sync time":
    command => "/usr/sbin/ntpdate 172.16.0.1 &> /dev/null",
    hour => "18",
    minute => "28",
}

[root@Centos7 manifests]# crontab -l
# Puppet Name: sync time
28 18 * * * /usr/sbin/ntpdate 172.16.0.1

package资源类型

管理程序包的安装、卸载、升级等

常用选项
    vim test7.pp
package {‘zsh’:
            ensure => latest,
        }

package {‘jdk’:
            ensure => installed,
            source => ‘/usr/local/src/jdk-8u25-linux-x64.rpm’,
            provider => rpm,
        }

service资源类型

实现服务管理

常用选项
11、安装Nginx软件包,且定义配置文件及服务启动设置
package {"nginx":
    ensure => latest,
} ->
file {"/etc/nginx/nginx.conf":
    source => "/tmp/nginx.conf",
    owner => "nginx",
    group => "nginx",
    notify => Service["nginx"],
}
service {"nginx":
    ensure => running,      #定义服务运行状态
    enable => true, #定义是否开机自启
    hasrestart => true, #nginx支持restart参数
    hasstatus => true,
    path => "/usr/lib/systemd/system",  #服务脚本搜索路径
    restart => "systemctl reload nginx.service",    #定义restart的方式
}

三种特殊属性之metaparameters

定义依赖关系
定义通知关系
资源引用
12、定义资源的依赖关系

group {"linux":
    gid => 2001,
    ensure => present,
    before => User["linux"],
}

user {"linux":
    ensure => present,
    uid => 2001,
    gid => 2001,
    shell => "/bin/bash",
    home => "/home/linux",
    system => false,
    password => "zhenping.com”,
    salt => "zhenping",
    require => Group["linux"],
}


13、定义通知关系和依赖关系
package {"nginx":
    ensure => latest,
    allow_virtual => false,
    before => File["/etc/nginx/nginx.conf"],
}

file {"/etc/nginx/nginx.conf":
    source => "/tmp/nginx.conf",
    owner => nginx,
    group => nginx,
    require => Package["nginx"],
    notify => Service["nginx"],
    ensure => file,
}
service {"nginx":
    ensure => running,
    enable => true,
    hasrestart => true,
    hasstatus => true,
    restart => "systemctl reload nginx",
    path => "/usr/lib/systemd/system",
    require => [Package["nginx"],File["/etc/nginx/nginx.conf"]],
    subscribe => File["/etc/nginx/nginx.conf"],
}

14、定义链式依赖关系:
package {"nginx":
    ensure => latest,
    allow_virtual => false,
    before => File["/etc/nginx/nginx.conf"],
} ->

file {"/etc/nginx/nginx.conf":
    source => "/tmp/nginx.conf",
    owner => nginx,
    group => nginx,
    ensure => file,
} ->
service {"nginx":
    ensure => running,
    enable => true,
    hasrestart => true,
    hasstatus => true,
    restart => "systemctl reload nginx",
    path => "/usr/lib/systemd/system",
}


示例二、
$webserver = $operatingsystem ? {
    /^(?i-mx:ubuntu|debian)/ => "apache2",
    /^(?i-mx:centos|redhat)/ => "httpd",
}

package {$webserver:
    ensure => latest,
    allow_virtual => false,
}
file {"/etc/httpd/conf/httpd.conf":
    source => "/tmp/httpd.conf",
    ensure => file,
}
service {$webserver:
    ensure => running,
    enable => true,
    restart => "systemctl restart httpd",
    path => "/usr/lib/systemd/system",
    subscribe => File["/etc/httpd/conf/httpd.conf"],
}

puppet变量及作用域

变量

puppet变量名称必须以$开头,赋值操作符为"=",任何非正则表达式类型的数据均可赋值变量,puppet的每个变量都有两个名字,简短名称和长格式完全限定名称

作用域

定义代码的生效范围,以实现代码单隔离

每个变量两种引用路径
变量的赋值符号
变量中的种类
变量的使用示例
$webserver=nginx
package {$webserver:
    ensure => latest,
}

puppet支持的数据类型

可以不用引用,支持单引号(强引用,不做变量替换),双引号(弱引用 )

其值为true,false

未声明的变量,直接定义使用的

整数和浮点数

元素可为任意可用数据类型,包括数据和hash,索引从0开始,还可以使用负数,表示方式为`[item1,item2,...]

键为字符串,而值可以是任意数据类型,表示方式为{key => value,key => value,...}

非标准数据类型,不能赋值给变量,其语法格式表示如下:

(?<ENABLE-OPTION>:<SUBPATTERN>)
(?-<ENABLE-OPTION>:<SUBPATTERN>)

OPTIONS
    i : 忽略字符大小写
    m : 把 . 当换行符
    x : 忽略模式中的空白和注释

puppet表达式

puppet条件判断语句

if判断语句

使用格式:

单分支:
if CONDITION {
    ....
} 

双分支:
if CONDITION {
    ....
} else {
    ....
}

多分支:
if CONDITION {
    ....
} elsif CONDITION {
    ....
} else {
    ....
}

配置示例
if $operatingsystem =~ /^(?i-mx:(redhat|centos))/ {
    package {"nginx":
        ensure => latest,
        allow_virtual => false,
    }

    service {"nginx":
        ensure => running,
        enable => true,
        hasrestart => true,
        hasstatus => true,
        restart => "systemctl reload nginx",
        path => "/usr/lib/systemd/system/",
    }
}


if $processorcount >1 {
    notice (“SMP Host.”)
} else {
    notice (“pool Guy.”)
}

if $operatingsystem =~ /^(?i-mx:(redhat|centos|fedora))/ {
    notice("welcom to $1 distribution linux.")
}

注意:此处的$1表示后向引用 ,引用括号中匹配到的内容

case判断语句

语句格式:
case CONTROL_EXPRESSION {
    case1, case2: { statement }
    case3, case4: { statement }
    default:      { statement }

}

CONTROL_EXPRESSION可以是以下格式:
1、表达式
2、变量
3、函数(有返回值)

case可以是:
1、字符串
2、变量
3、正则表达式
4、函数(有返回值)
5、default

selector语句

语句格式:

CONTROL_VARIABLE ? {
    case1 => value1
    case2 => value2
    ...
}

注意:selector不能用于一个已经嵌套于selector的case中,也不能用于一个已经嵌套于case的case语句中

类似于case,但分支的作用不在于执行代码片段,而是返回一个直接值

配置示例
$webserver = $operatingsystem ? {
    /^(?i-mx:ubuntu|debian)/ => "apache2",
    /^(?i-mx:centos|redhat)/ => "httpd",
}

puppet类(class)

用于公共目的的一组资源,是命名的代码块,创建后可在puppet全局进行调用

类的特性

类的声明方式

定义好的类需要声明后才会被执行

类的声明方式一
示例:
class webserver {
    $webserver = $operatingsystem ? {
        /^(?i-mx:ubuntu|debian)/ => "apache2",
        /^(?i-mx:centos|redhat)/ => "httpd",
    }

    package {$webserver:
        ensure => latest,
        allow_virtual => false,
    }
    file {"/etc/httpd/conf/httpd.conf":
        source => "/tmp/httpd.conf",
        ensure => file,
    }
    service {$webserver:
        ensure => running,
        enable => true,
        restart => "systemctl restart httpd.service",
        path => "/usr/lib/systemd/system/",
        subscribe => File["/etc/httpd/conf/httpd.conf"],
    }

}
include webserver  #直接调用webserver类
类的声明方式二
class {"class_name":
    variable => value,
}
示例
class httpd($webserver='nginx') {   #此处的变量定义为默认值,当未在声明中设定变量值,就使用默认的变量值

    package {$webserver:
        ensure => latest,
        allow_virtual => false,
    }
    file {"/etc/httpd/conf/httpd.conf":
        source => "/tmp/httpd.conf",
        ensure => file,
    }
    service {$webserver:
        ensure => running,
        enable => true,
        subscribe => File["/etc/httpd/conf/httpd.conf"],
    }

}
class {"httpd":
    webserver => "httpd", #在类声明的时候,给类传递一个参数
}

类的继承

通常将公共功能定义为基类,需要增加的功能定义为子类,其继承一个已有的类,并实现覆盖资源属性,或向资源属性追加额外值

    class apache {
      service {'apache':
        require => Package['httpd'],
      }
    }

    class apache::ssl inherits apache {
      Service['apache'] {
        require +> [ File['httpd.pem'], File['httpd.conf'] ],
      }
    }
继承类的定义方式
class base_class {
    ....puppet code....
}

class base_class::class_name inherits base_class {
    ....puppet code....
}
使用示例
class nginx {
    package {"nginx":
        ensure => latest,
        allow_virtual => false,
    }
}

class nginx::webserver inherits nginx {
    file {"/etc/nginx/nginx.conf":
        source => "/tmp/nginx.conf",
        owner => nginx,
        group => nginx,
        mode => 0644,
        ensure => file,
        require => Package["nginx"],
    }

    service {"nginx":
        ensure => running,
        enable => true,
        restart => "systemctl reload nginx",
        path => "/usr/lib/systemd/system/",
        hasstatus => true,
        hasrestart => false,
        subscribe => File["/etc/nginx/nginx.conf"],
    }
}

class nginx::proxy inherits nginx {
    file {"/etc/nginx/nginx.conf":
        source => "/tmp/proxy/nginx.conf",
        owner => nginx,
        group => nginx,
        ensure => file,
        mode => 0644,
        require => Package["nginx"],
    }

    service {"nginx":
        ensure => running,
        enable => true,
        restart => "systemctl reload nginx",
        path => "/usr/lib/systemd/system",
        hasstatus => true,
        hasrestart => false,
        subscribe => File["/etc/nginx/nginx.conf"],
    }
}

include nginx::webserver

puppet模板

基于ERB(扩展ruby语言)模板语言,在静态文件中使用变量等编程元素生成适用于多种不同环境的文本文件(配置文件),主要用于实现在文本文件中嵌入ruby代码,原来的文件信息不会被改变,但ruby代码会被执行,执行结果将直接替换原来代码

模板代码的添加方式
在模板中可以使用变量,包括puppet的任意可用变量,但变量名需以@字符开头
模板中的条件判断
模板中的迭代实现

模板的中的迭代是为了实现某配置文件中批量添加生成配置信息,如配置nginx中的Location时,当有多个Location段需要添加,可以将location赋值给一个数组,利用迭代将值引用到配置文件中。

<% @ArrayName.echo do | Variable_name | -%>
    some text with <%= Variable_name %>
<% end %>
模板文件的定义示例:
1、配置模板配置文件
vim /tmp/nginx.conf
worker_processes <%= @processorcount %>;  #此处变量为facter中的变量,可以直接引用

2、puppet脚本配置语言
vim nginx.pp
class nginx {
    package {"nginx":
        ensure => latest,
        allow_virtual => false,
    }
}

class nginx::webserver inherits nginx {
    file {"/etc/nginx/nginx.conf":
  #如果定义好的配置文件,使用了ERB的配置语法,此处文件复制不能使用source,而是需要使用content参数,利用template函数生成对应的配置文件,此为固定格式用法(将使用template函数生成文本内容,再导入至/etc/nginx/nginx.conf文件中)
        content => template("/tmp/nginx.conf"), 
        owner => nginx,
        group => nginx,
        mode => 0644,
        ensure => file,
        require => Package["nginx"],
    }

    service {"nginx":
        ensure => running,
        enable => true,
        restart => "systemctl reload nginx",
        path => "/usr/lib/systemd/system/",
        hasstatus => true,
        hasrestart => false,
        subscribe => File["/etc/nginx/nginx.conf"],
    }
}
include nginx::webserver

puppet模块

在puppet中,模块本身用一个目录来表示,其需要存放于puppet的modulepath参数所定义的目录中,如/etc/puppet/modules。在manifests/init.pp需定义一个与模块名相同的类名,以完成自包含、自装载、自识别

模块目录组成格式

    ```
    示例:
    [root@Centos7 manifests]# tree /etc/puppet/modules/
    /etc/puppet/modules/
    `-- nginx
    |-- files
    |   `-- nginx.conf
    |-- lib
    |-- manifests
    |   `-- init.pp
    |-- spec
    |-- templates
    |   `-- nginx.conf.erb
    `-- tests
    
    [root@Centos7 manifests]# cat init.pp
    class nginx {
        package {"nginx":
            ensure => latest,
            allow_virtual => false,
        }
    }

    class nginx::webserver inherits nginx {
        file {"/etc/nginx/nginx.conf":
            content => template("nginx/nginx.conf.erb"),  #模板文件定义的路径为“模块名/erb文件”
            owner => nginx,
            group => nginx,
            mode => 0644,
            ensure => file,
            require => Package["nginx"],
        }

        service {"nginx":
            ensure => running,
            enable => true,
            restart => "systemctl reload nginx",
            path => "/usr/lib/systemd/system/",
            hasstatus => true,
            hasrestart => false,
            subscribe => File["/etc/nginx/nginx.conf"],
        }
    }

    class nginx::proxy inherits nginx {
        file {"/etc/nginx/nginx.conf":
            source => "puppet:///modules/nginx/nginx.conf", #配置文件路径路径为"协议:///modules/模块名/静态配置文件"
            owner => nginx,
            group => nginx,
            ensure => file,
            mode => 0644,
            require => Package["nginx"],
        }

        service {"nginx":
            ensure => running,
            enable => true,
            restart => "systemctl reload nginx",
            path => "/usr/lib/systemd/system",
            hasstatus => true,
            hasrestart => false,
            subscribe => File["/etc/nginx/nginx.conf"],
        }
    }
    ```

模块管理工具

puppet module 命令

puppet Master/Agent模式

master/agent工作原理

master/agent强依赖于DNS服务(证书签署是对FQDN做证书颁发的),由master端定义好功能模块,再到/etc/puppet/manifests/定义site.pp文件,定义站点所需要的资源。master端通过自建CA并签发证书给各站点,使用证书验证客户端的身份,当站点发出请求时Master端将查找site.pp文件中定义的资源,编译成catalog,发送给客户端。 agent默认每隔30分钟向Master发送node_name和facts,并请求catalog,在本地执行catalog代码。master与agent二者之间基于https协议通信,其远程过程调用方式为xmlrpc机制。

master/agent程序安装
master/agent监听端口
/etc/puppet/puppet.conf配置文件
显示或配置参数
注意:此些值的修改是对/etc/puppet/puppet.conf文件生效
手动生成配置文件
        puppet master —genconfig > /etc/puppet_default.conf 
        mv /etc/puppet/puppet.conf /etc/puppet/puppet.conf.bak
        mv /etc/puppet_default.conf /etc/puppet.conf
puppet agent —genconfig >> /etc/puppet.conf (可以追加,在agent端使用覆盖)
1、生成新的配置之前不能删除或移动原有的puppet.conf
2、生成的配置中,有的参数已经被废弃,与现有puppet版本可能不兼容
3、有的参数的默认值与现在的版本所支持值可能不相兼容
4、配置信息可以不使用此工具生成,可以直接在配置文件中添加
Master/agent服务管理
(puppet cert命令)证书管理工具
site.pp定义站点调用的模块
node 'node_name' {
    ....puppet code....
    include nginx::webserver
}
node /^web\d+\.magedu\.com/ {
    ....puppet code...
    include nginx::webserver
}
node "basenode" {
    include ntp
}

node "web.zhenping.com" inherits basenode {
    include nginx::proxy
}

对节点配置分段管理
/etc/puppet/manifests/
    vim site.pp
    import "webserver/*.pp"
    
    webservers/
        unicom.pp
        telecom.pp
    cacheservers/
    appservers/
master/agent配置示例
1、配置master端
# puppet master --no-daemonize -v #首次以前台模式启动,确认无误再运行为后端
# systemctl start puppetserver.service
# systemctl enable puppetserver.service
# ss -tnlp : 8140/tcp

2、定义站点配置文件
# cd /etc/puppet/manifests/
# vim site.pp  #必须先为站点定义好站点文件,不然agent端启动时会报错
node /^centos7.pc\d+/ {
    include nginx::webserver
}

3、配置agent端(发送证书签署请求给Master)
# puppet agent --server=master_hostname --no-daemonize --noop --test -v #建议首次启动时以前台模式运行,确认OK后,再将运行为后端


4、在master端为客户端签署证书
# puppet cert list #首先查看未签署的证书列表
# puppet cert sign node_name 
    或者
# puppet cert sing -all

5、以守护进程方式启动agent
# systemctl start puppet

####必要是清除客户端请求
#puppet cert list -all : 查看已经签署的客户端证书
# puppet cert clean node_name : 清除一个Node的签署证书

# rm -rf /var/lib/puppet/ssl : 移除agent端的ssl证书

master/agent的多环境支持
puppet的多环境支持:
    master端环境配置段:为不同的主机配置不同的配置
        [master]
        enviroment = production, testing, development   #声明master支持那些环境配置

        [production](生产环境)
        manifest = /etc/puppet/environments/production/manifests/site.pp
        modulepath = /etc/puppet/enviroments/production/modules/
        fileserverconfig = /etc/puppet/fileserver.conf
        
        [testing]
        manifest = /etc/puppet/environments/testing/manifests/site.pp
        modulepath = /etc/puppet/enviroments/testing/modules/
        fileserverconfig = /etc/puppet/fileserver.conf

        [development]
        manifest = /etc/puppet/environments/development/manifests/site.pp
        modulepath = /etc/puppet/enviroments/development/modules/
        fileserverconfig = /etc/puppet/fileserver.conf  
    
    agent端配置文件:
        [agent]
        enviroment = testing
上一篇 下一篇

猜你喜欢

热点阅读