手机版
你好,游客 登录 注册
背景:
阅读新闻

自动化运维工具Puppet 快速入门

[日期:2016-12-05] 来源:Linux社区  作者:purify [字体: ]

一、简介

当服务器数量达到一定的规模时,仅依靠人为完成批量部署服务器个资源的配置,运维工作将变得繁琐且容易出错,为了解决这一问题,我们应该怎么办呢?我们可以引入一批工具,这批工具可编写相应的manifests代码,运行它便可以自动完成所有的工作,目前比较流行的运维工具主要有:puppet,ansible, slackstack等,在这我们主要以puppet来展开话题。在一些大型互联网企业中,运维自动化管理着几百甚至上千台服务器,它可以针对多台服务器进行统一操作,例如部署统一软件、进行统一上线维护等,而且能够快速完成上线部署,减少人力及人力误操作风险。

二、Puppet的工作原理   

puppet的目的是让系统管理员只集中于要管理的目标服务器,而忽略实现的细节。puppet既可以在单机上使用,也可以C/S结构使用,在大规模部署puppet的情况下,通常我们会使用C/S结构,在这种结构下,服务端运行puppet-master程序客户端运行puppet-client服务程序

具体的工作流程图如下所示:

对于puppet的的掌握,理解puppet的工作原理是一个必要的的阶段,只有在了解了puppet的工作原理后才能更好应用puppet,下面让我们一起了解学习puppet的工作原理:

说到puppet的工作原理,不得不从以下四个方面来说到,如下所示:

(1)定义:使用Puppet特定的语言定义基础配置信息。通常我们把这些信息写在Modules中。

(2)模板:在配置执行之前检测代码,但并不真正执行。

(3)执行:定义的配置自动部署。检测并记录下所发生变化的部分。

(4)报告:将期待的变化、实际发生的变化及任何修改发送给报告系统。

如下所示为puppet的工作数据流示意图

数据流说明:

1.首先所有的节点(Node)Node节点将Facts和本机信息发送给Master

2.Master告诉Node节点应该如何配置,将这些信息写入Catalog后传给Node。

3.Node节点在本机进行代码解析验证并执行,将结果反馈给Master。

4.Master通过API将数据发给分析工具。报告完全可以通过开放API或与其他系统集成。

整个数据流的走向是基于SSL安全协议的,如下图所示:

模板文件处理过程说明如下:

Puppet通过编译Manifest中的内容 (即模板中内容),将编译好的代码存入Catalog。在执行前先进行代码的验证,再执行,完成最开始所定义好的状态。代码编译过程如图所示:

如下所示为整个puppet自动部署过程中agent和master的详细的交互过程:

过程说明:

1. Puppet客户端Agent将节点名与facts信息发送给Master。

2. Puppet服务端Master通过分类判断请求的客户端是谁,它将要做什么。这个判断是通过site.pp中包含的Node.pp配置文件定义的。

3. Puppet服务端Master将所需要的Class类信息进行编译后存入Catalog并发送给Puppet客户端Agent,到此完成第一次交互。

4. Puppet客户端Agent对Catalog进行代码验证(语法检查及错误检查)并执行。主要是代码的验证,并将执行过程的信息及结果写入日志。

5. Puppet客户端Agent最终达到最开始所定义的状态,并且将结果及任何执行数据通过开放API的形式发送给Puppet服务端Master。

以上就是puppet的工作原理需要注意是:因为整个过程中都是基于ssl实现的,所以首要的是保证agent和master间可以基于ssl通讯!

三、puppet常用资源及配置实例

实例一: 创建CentOS用户为普通用户,且uid为4000,gid为3000,所属组为centos,附加组为mygrp

user{'centos':

  name    => 'centos', #定义用户名

  ensure  => present,  #创建用户

  uid    => 4000,    #定用户id

  groups  => 'mygrp',  #定义其他附加组

  require => Group['mygrp'] #此资源依赖于事先创建mygrps组

}

group {'mygrp':

  name    => 'mygrp',#定义组名

  ensure  => present, #创建组,与此相反的是absent,删除组

  gid    => '3000', #组id

  system  => false,  #是否为系统组,默认为false

}

总结:

user常用属性:

name:用户名,uid:用户id,gid:组id,groups:附加组,comment:注释信息

expiry:过期时间,home:家目录,shell:默认的shell类型,system:是否为系统组,

ensure:创建或删除用户即present、absent,password:加密后的密码

group常用属性: 

ensure:创建或删除组即present、absent,name:组id,system:是否为系统组等

[root@node1 manifets]# puppet apply -v user.pp #将此manifests清单编译为伪代码catalog,并执行,且返回执行结果至此单机模式的主机上

Notice: Compiled catalog for node1.alren.com in environment production in 0.55 seconds

Info: Applying configuration version '1480561275'

Notice: /Stage[main]/Main/Group[mygrp]/ensure: created

Notice: /Stage[main]/Main/User[centos]/ensure: created

Notice: Finished catalog run in 0.11 seconds

[root@node1 manifets]# id centos

uid=4000(centos) gid=4000(centos) groups=4000(centos),3000(mygrp)

[root@node1 manifets]# vi user.pp

[root@node1 manifets]# puppet apply -v user.pp

Notice: Compiled catalog for node1.alren.com in environment production in 0.58 seconds

Info: Applying configuration version '1480561376'

Notice: /Stage[main]/Main/Group[mygrp]/ensure: removed

Notice: /Stage[main]/Main/User[centos]/ensure: removed

Notice: Finished catalog run in 0.13 seconds

[root@node1 manifets]# id centos

id: centos: no such user

[root@node1 manifets]#

实例二:此manifests代码为安装httpd包,为其提供配置文件,并且启动服务

service{'httpd':

  ensure    => running,

  enable    => true,

  restart  => 'systemctl restart httpd.service',

  require  => Package['httpd'],

}

package{'httpd':

  ensure    => installed,

}

file{'httpd.conf':

  path      => '/etc/httpd/conf/httpd.conf',

  source    => '/root/manifests/httpd.conf',

  ensure    => file,

  notify    => Service['httpd'],

  before    => Service['httpd'],

}

package常用属性:

ensure:installed,present,latest,absent name:包名 

source:程序包来源,仅对不会自动下载的相关程序包的provider有用,例如rpm或dpkg

servicec常用属性:

ensure:running,stopped运行停止 enable:开机是否启动 restart:重启命令

require:被依赖于事先安装程序包,

file常用属性:

path:文件需放置的位置所在处  source:源文件在哪    ensure:file,present,absent,directory,link...

file指的是普通文件,link为连接文件,此需结合target一起使用 directory:类型为目录,可通过source指向的路径复制生成,recuse属性指明是否为递归复制

owner:属主 group:属组 mode:权限

[root@node1 manifets]# puppet apply -v  web.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.48 seconds

Info: Applying configuration version '1480563754'

Info: Computing checksum on file /etc/httpd/conf/httpd.conf

Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 42566ec31df37e3d44429b285d015e1d

Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}42566ec31df37e3d44429b285d015e1d' to '{md5}8b01e334a6e975b659df5dd351923ccb'

Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

Notice: Finished catalog run in 1.95 seconds

[root@node1 manifets]# ss -tnl

State      Recv-Q Send-Q Local Address:Port              Peer Address:Port

LISTEN    0      5      192.168.122.1:53                    *:*

LISTEN    0      128        *:22                    *:*

LISTEN    0      128    127.0.0.1:631                    *:*

LISTEN    0      100    127.0.0.1:25                    *:*

LISTEN    0      32        :::21                    :::*

LISTEN    0      128      :::22                    :::*

LISTEN    0      128      ::1:631                  :::*

LISTEN    0      128      :::8088                  :::*

LISTEN    0      100      ::1:25                    :::*

[root@node1 manifets]#

实例三:每三分钟同步下系统时间,写入定时任务

cron{'synctime':

  command  => '/usr/sbin/ntpdate 10.1.0.1 &>/dev/null', #须执行的命令

  ensure    => present,  #创建任务计划

  minute    => '*/5',    #过多长是时间执行

  user      => root,    #以谁的身份运行

}

[root@node1 testmanifests]# puppet apply -v cron.pp

Notice: Compiled catalog for node1.alren.com in environment production in 0.18 seconds

Info: Applying configuration version '1480593969'

Notice: /Stage[main]/Main/Cron[synctime]/minute: minute changed '*/3' to '*/5'

Notice: Finished catalog run in 0.09 seconds

[root@node1 testmanifests]# crontab -l

# HEADER: This file was autogenerated at 2016-12-01 20:06:10 +0800 by puppet.

# HEADER: While it can still be managed manually, it is definitely not recommended.

# HEADER: Note particularly that the comments starting with 'Puppet Name' should

# HEADER: not be deleted, as doing so could cause duplicate cron jobs.

# Puppet Name: synctime

*/5 * * * * /usr/sbin/ntpdate 10.1.0.1 &>/dev/null  #查看到此任务计划存在

[root@node1 testmanifests]#

实例四:puppet之if条件判断

if $osfamily =~ /(?i-mx:debian)/ {  $osfamily为facter中取得的内嵌变量

          $webserver = 'apache2' #自定义变量

} else {

          $webserver = 'httpd'

}

package{"$webserver":

  ensure    => installed,

  before    => [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

  path      => '/etc/httpd/conf/httpd.conf',

  source    => '/root/testmanifests/httpd.conf',

  ensure    => file,

}

service{'httpd':

  ensure    => running,

  enable    => true,

  restart  => 'systemctl restart httpd.service',

  subscribe => File['httpd.conf'], #通知其他的资源进行刷新操作

}

[root@node1 testmanifests]# puppet apply -v if.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.53 seconds

Info: Applying configuration version '1480594920'

Info: Computing checksum on file /etc/httpd/conf/httpd.conf

Info: FileBucket got a duplicate file {md5}8b01e334a6e975b659df5dd351923ccb

Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 8b01e334a6e975b659df5dd351923ccb

Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}8b01e334a6e975b659df5dd351923ccb' to '{md5}42566ec31df37e3d44429b285d015e1d'

Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

Notice: Finished catalog run in 1.97 seconds

[root@node1 testmanifests]# ss -tnl

State      Recv-Q Send-Q Local Address:Port              Peer Address:Port

LISTEN    0      5      192.168.122.1:53                    *:*

LISTEN    0      128        *:22                    *:*

LISTEN    0      128    127.0.0.1:631                    *:*

LISTEN    0      100    127.0.0.1:25                    *:*

LISTEN    0      128      :::80                    :::*

LISTEN    0      32        :::21                    :::*

LISTEN    0      128      :::22                    :::*

LISTEN    0      128      ::1:631                  :::*

LISTEN    0      100      ::1:25                    :::*

[root@node1 testmanifests]#

实例五:puppet之case语句

case $osfamily {

"RedHat": { $webserver='httpd' }

/(?i-mx:debian)/: { $webserver='apache2' }

default: { $webserver='httpd' }

}

package{"$webserver":

ensure=> installed,

before=> [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

ensure=> file,

path => '/etc/httpd/conf/httpd.conf',

source=> '/root/testmanifests/httpd.conf',

}

service{'httpd':

ensure=> running,

enable=> true,

restart => 'systemctl restart httpd.service',

subscribe=> File['httpd.conf'],

}

[root@node1 testmanifests]# puppet apply -v case.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.61 seconds

Info: Applying configuration version '1480595667'

Info: Computing checksum on file /etc/httpd/conf/httpd.conf

Info: FileBucket got a duplicate file {md5}42566ec31df37e3d44429b285d015e1d

Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 42566ec31df37e3d44429b285d015e1d

Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}42566ec31df37e3d44429b285d015e1d' to '{md5}3dfa14b023127a3766bddfe15fe14b9a'

Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

Notice: Finished catalog run in 1.91 seconds

[root@node1 testmanifests]# #可根据自行的语言习惯选择适合的语法,如下所示同样可达到上诉的目的

$webserver = $osfamily ? {

"Redhat" => 'httpd',

/(?i-mx:debian)/ => 'apache2',

default => 'httpd',

}

package{"$webserver":

ensure  => installed,

before  => [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

path    => '/etc/httpd/conf/httpd.conf',

source  => '/root/testmanifests/httpd.conf',

ensure  => file,

}

service{'httpd':

ensure  => running,

enable  => true,

restart => 'systemctl restart httpd.service',

subscribe => File['httpd.conf'],

}

四、puppet类与继承

puppet中命名的代码模块,经常需要被使用,如如重写则代码冗余,使用定义一组通用目标的资源,可在puppet全局调用,就能解决这类问题,类可以被继承,也可以包含子类。

类的语法格式有两种,调用类的方法常用的有三种,可以在类中传递参数等灵活的操作,如以下实例:

class webserver { #类申明后续调用后才生效,调用的方法常用的有三种

$webpkg = $operatingsystem ? {

/(?i-mx:(centos|redhat|Fedora))/        => 'httpd',

/(?i-mx:(Ubuntu|debian))/      => 'apache2',

default => 'httpd',

}

package{"$webpkg":

ensure  => installed,

}

file{'/etc/httpd/conf/httpd.conf':

ensure  => file,

owner  => root,

group  => root,

source  => '/root/testmanifests/httpd.conf',

require => Package["$webpkg"],

notify  => Service['httpd'],

}

service{'httpd':

ensure  => running,

enable  => true,

}

}

include webserver  #使用include进行调用

#申明调用类的第二种方法

class web($webserver='httpd') { #向类中传递参数

package{"$webserver":

ensure  => installed,

before  => [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

path    => '/etc/httpd/conf/httpd.conf',

source  => '/root/testmanifests/httpd.conf',

ensure  => file,

}

service{'httpd':

ensure  => running,

enable  => true,

restart => 'systemctl restart httpd.service',

subscribe => File['httpd.conf'],

}

}

class{'web':

webserver      => 'apache2', #调用类

}

[root@node1 ~]# puppet apply -v --noop class2.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.53 seconds

Info: Applying configuration version '1480596978'

Notice: /Stage[main]/Web/Package[apache2]/ensure: current_value absent, should be present (noop)

Notice: /Stage[main]/Web/File[httpd.conf]/content: current_value {md5}3dfa14b023127a3766bddfe15fe14b9a, should be {md5}8b01e334a6e975b659df5dd351923ccb(noop)

Info: /Stage[main]/Web/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Web/Service[httpd]: Would have triggered 'refresh' from 1 events

Notice: Class[Web]: Would have triggered 'refresh' from 3 events

Notice: Stage[main]: Would have triggered 'refresh' from 1 events

Notice: Finished catalog run in 0.65 seconds

[root@node1 ~]#

   

类的继承:子类可继承父类的资源属性,同时可定义父类不存在的额资源属性,一个父类可同时被多个子类所继承

class nginx { #定义父类信息

package{'nginx':

ensure  => installed, #安装程序包

}

service{'nginx': #启动程序

ensure  => running,

enable  => true,

require => Package['nginx'],

}

}

class nginx::web inherits nginx {  #继承父类的原有属性,同时增加file资源属性

file{'ngx-web.conf':

path    => '/etc/nginx/conf.d/ngx-web.conf',

ensure  => file,

require => Package['nginx'],

source  => '/root/testmanifests/nginx/ngx-web.conf',

}

file{'nginx.conf':

path    => '/etc/nginx/nginx.conf',

ensure  => file,

content => template('/root/testmanifests/nginx.conf.erb'), #此模板需事先存在方可使用

require => Package['nginx'],

}

Service['nginx'] {

subscribe => [ File['ngx-web.conf'], File['nginx.conf'] ], #通知其他资源进行刷新操作

}

}

include nginx::web #调用声明的类

五、puppet模板(此内容不过多解释,需自行加强)

模板是一个按照约定的、预定的结构存放了多个文件或子目录的目录,目录里面的这些文件或子目录必须遵循一定的格式的命名规范,puppet会在配置的路径下查找所需的资源模块。模块的名称通常是只能以小写字母开头,可以包含小写字母,数字下划线,但是不能使用main和settings。 

模块的组成部分:

manifests/:资源清单

init.pp:必须定义一个类,类名必须与模块名相同;

files/:静态文件

templates/:模板文件

lib/: 插件目录,常用于存储自定义的facts以及自定义类型 

spec/:类似于tests目录,存储lib/目录下插件的使用帮助和范例;

tests/:当前模块的使用帮助或使用范例文件;

总结:运维工具有很多例如:Puppet,Ansible,slackstack等,puppet还是一个很好用的自动化运维工具,大大减轻的运维人员的重复操作,提高了工作效率,在运维过程中可根据业务需求选择不同的运维工具,在服务器数量不是很大的情形下可使用轻量级的ansible,在服务器数量达到一定的规模时使用重量级的puppet相对来说效率更高。当面临有得选择的时候想起一句话:箩卜白菜各有所爱,适合自己专注、精通一个运维工具比全会那么一点,解决问题更有优势。

Puppet 学习系列:

Puppet 学习一:安装及简单实例应用 http://www.linuxidc.com/Linux/2013-08/88710.htm

Puppet学习二:简单模块配置和应用 http://www.linuxidc.com/Linux/2013-08/88711.htm

相关阅读:

有关Puppet agent端三种备份恢复方案探讨研究 http://www.linuxidc.com/Linux/2013-07/87885.htm
选择更安全的方式注册你的Puppet节点 http://www.linuxidc.com/Linux/2013-07/87884.htm
通过配置SSH深刻理解Puppet的语法及工作机制 http://www.linuxidc.com/Linux/2013-07/87882.htm
Puppet利用Nginx多端口实现负载均衡 http://www.linuxidc.com/Linux/2013-02/79794.htm
CentOS(5和6)下Puppet的C/S模式实例 http://www.linuxidc.com/Linux/2011-12/50502.htm

Puppet 的详细介绍请点这里
Puppet 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-12/137893.htm

linux
相关资讯       Puppet  Puppet入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款