10分钟带你光速入门运维工具之-Puppet
一、简介
当服务器数量达到一定的规模时,仅依靠人为完成批量部署服务器个资源的配置,运维工作将变得繁琐且容易出错,为了解决这一问题,我们应该怎么办呢?我们可以引入一批工具,这批工具可编写相应的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的工作数据流示意图
数据流说明:
首先所有的节点(Node)Node节点将Facts和本机信息发送给Master
Master告诉Node节点应该如何配置,将这些信息写入Catalog后传给Node。
Node节点在本机进行代码解析验证并执行,将结果反馈给Master。
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通讯!
以上内容参考:http://blief.blog.51cto.com/6170059/1760439
三、puppet常用资源及配置实例
实例一: 创建centos用户为普通用户,且uid为4000,gid为3000,所属组为centos,附加组为mygrp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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包,为其提供配置文件,并且启动服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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] #
|
实例三:每三分钟同步下系统时间,写入定时任务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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条件判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
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全局调用,就能解决这类问题,类可以被继承,也可以包含子类。
类的语法格式有两种,调用类的方法常用的有三种,可以在类中传递参数等灵活的操作,如以下实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
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 ~] #
|
类的继承:子类可继承父类的资源属性,同时可定义父类不存在的额资源属性,一个父类可同时被多个子类所继承
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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相对来说效率更高。当面临有得选择的时候想起一句话:箩卜白菜各有所爱,适合自己专注、精通一个运维工具比全会那么一点,解决问题更有优势。
本文转自chengong1013 51CTO博客,原文链接:http://blog.51cto.com/purify/1878681,如需转载请自行联系原作者
10分钟带你光速入门运维工具之-Puppet相关推荐
- 自动化运维工具之puppet简单实用
一.简介 什么是puppet编辑 puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等. ...
- 10分钟带你了解python_10分钟Python入门系列教程及学习资源分享
本期分享笔记内容 归档此前入门教程文章,方便查看 10分钟带你Python入门的特点 简单谈下如何寻找Python学习资源 关于分享Python学习资源的分享问题 本人对于Python学习创建了一个小 ...
- 运维工具 Ansible 快速入门教程
Ansible 简介 Ansible 是新出现的自动化运维工具,基于 Python 开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配 ...
- ansible——自动化运维工具
目录 ansible 简介 ansible 是什么? ansible 特点 ansible 架构图 ansible 任务执行 ansible 任务执行模式 ansible 执行流程 ansible 命 ...
- 自动化运维工具——ansible详解(一)
ansible 简介 ansible 是什么? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统 ...
- 自动化运维工具-Ansible实战指南
Ansible实战 前言 一.Ansible简介 1.ansible是什么? 2.ansible特点 3.ansible架构 主要模块 工作流程 命令执行过程 二.Ansible 配置 1 安装ans ...
- 自动化运维工具Ansible
ansible简介: ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批 ...
- 自动化运维工具Ansible详细部署
一.基础介绍 ================================================================================= 1.简介 ansibl ...
- 自动化运维工具Ansible连续剧之--介绍安装与连接
介绍 ansible官方文档里写道 Ansible is a radically simple IT automation engine 即:Ansible是一款极其简单的IT自动化工具 它基于Pyt ...
- 自动化运维工具Ansible实战(一)简介和部署
一.Ansible的介绍 Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点.实现了批量系统配置 ...
最新文章
- 阿里披露AI完整布局,飞天AI平台首次亮相
- JavaScript事件详解-jQuery的事件实现(三)
- blender2.8与2.7X的一些不同点
- java 页面 传送参数,Struts2的action接收JSP页面传输的参数
- CF949 简要题解
- markdown语法手册完整版
- 模型转换、压缩、加速工具
- 微信小程序与公众号区别PHP,微信小程序和微信公众号的区别是什么?
- 给toB创业程序员的建议
- android动画结束保持,【Android】 动画
- PMO和PM有哪些区别 谁管谁
- Cf252中子发射能谱模型
- Shader学习2——兰伯特
- P1291 SHOI2002 百事世界杯之旅
- Skype 协议分析(2006版)
- VPX显示计算机学习资料第711篇:飞腾1500A-4+8860 6UVPX显示计算机
- 在读大学生创业历程,350元起家到现在将近40万元的积蓄(1)
- [Tomcat] server.xml配置appBase与docBase的用法
- 安卓注册登录界面示例
- 光谱仪光谱测试数据离线校准计算软件
热门文章
- node_modules/css-loader?{sourceMap:true}!./node_modules/vue-loader/lib/style-compiler?报错问提解决方案
- jlink v9可升级固件‘_在rt-thread下实现OTA在线固件更新功能
- 判断数组、集合list、string、int、double等是否为空,判断是否为值类型
- windows环境安装python
- undefined reference to `gdk_monitor_get_scale_factor/gtk_widget_get_scale_factor‘
- MAC编译:fatal error: ‘endian.h‘ file not found
- Please create pull requests instead of asking for help on Homebrew‘s GitHubError: macOS 10.13
- 管理新语:别人反映问题,你要形成自己的材料
- 全网首发:怎样制作CDKEY(7)-新思路
- CDKEY制作:为什么不能使用RSA?