堕落不振功业废,勤耕不辍日月新

推荐-自动化运维之puppet小试牛刀

Linux/Unix hailen 1060℃

一、puppet介绍

puppet是IT基础设施自动化管理工具,“她”能够帮助系统管理员管理基础设施的整个生命周期:供应(provisioning、配置(configuration)、联动(orchestration)及报告(reporting)。

基于puppet,可实现自动化重复任务、快速部署关键行应用以及在本地或云端完成主动管理变更和快速扩招架构规模等。遵循GPL协议(2.7.0-),基于ruby语音开发, 2.7.0以后使用(Apache 2.0 license),对应系统管理员是抽象的,只依赖于ruby于facter,能管理多达40多种资源,列如:file、user、grup、host、package、service、cron、exec、yum repo等,适合整个软件的生命周期管理。puppet适用开发环境、测试环境、生产环境。

 

二、puppet工作方式:

 

三、puppet的工作模型有单机(agent)和master/agent模型,基于SSL认证机制。

puppet-work.jpg

 

三、puppet工作模型:

   puppet通过声明性、基于模型的方法进行IT自动化管理

        定义:通过puppet的生命性配置语言定义基础设置配置的目标状态;

        模拟:强制应用改变的配置之前先进行模拟性应用;dry-run

        强制:自动、强制部署达成目标状态,纠正任何偏离的配置;

        报告:报告当下状态及目标状态的不同,以及达成目标状态多进行的任何强制性改变;

 

四、puppet管理的常用类型:package, user, group, file, service, exec, cron, notify

        

notify {‘title’:
message  => ‘something here’,
}

package的常用属性:
ensure:present, installed, absent, latest
name: 包名
source:包文件的路径,可以是本地文件系统路径,也可以网络文件路径;
puppet:///modules/MODULE_NAME/FILENAME
package{‘mysql’:
ensurce  => installed,
provider  => msi,
source   => “D:mysqlmysql-5.5.45.msi”,
install_options =>  {‘INSTALLDIR’ => ‘C:mysql’},
}

file的常用属性:
content:直接生成的文件内容;
生成方式有三种:
ensurce: file, directory, link, absent
source: 通过指定的URL下载文件至目标位置;
target: 符号链接文件的链接目标;
owner: 属主
group: 属组
mode: 权限
path:文件路径
force: 强制删除;仅用于ensure为absent时;
purge: 清除指定目录中存在的,但是在puppet中未定义的文件;
replace:是否替换已经存在的文件;

 

 service的常用属性:
ensure:
true, running
false, stopped
enable: 是否开机自动启动
name:服务名称
path: 服务脚本路径

start:
stop:
restart: reload
status:

exec的常用属性:
用于执行命令;但通常仅应该用于必须执行命令才能完成操作时(puppet本身无法完成此功能);

command: 要执行的命令,通常应该使用绝对路径;
path:为非绝对路径的命令指定搜索路径;
user:
group:

refresh: 默认为收到依赖的资源的事件时会重新执行此命令;但此属性可以修改此默认行为;
refreshonly: 此资源不会自动被执行,除非被依赖的资源发生了改变;
returns: 指定状态返回值;
group的常用属性:
ensure: present, absent
name: 组名
gid: GID
system: 是否为系统组;true, yes; false, no

user的常用属性:
ensure: present, absent
name: 用户名
uid: UID
system: 是否为系统用户;
gid: 基本组;
groups: 附加组;
home: 家目录;
password: 密码;
shell: 默认shell

cron的常用属性:
ensure: present, absent
command: 周期性执行的命令;
environment: 定义PATH环境变量;
hour:
minute:
month:
monthday:
weekday:

 

 资源的特殊属性:
元参数:用于定义资源间的关系
before => Type[‘title’]
require => Type[‘title’]

 

五、centos7安装配置puppet

 

1、分别修改master/agent里的/etc/hosts

     192.168.139.128         agent.puppet.com

192.168.139.131         master.puppet.com

vim /etc/hostname (分别执行)

agent.puppet.com

master.puppet.com

2、关闭防火墙selinux确定2台机器时间一致

3、安装epel源,下载地址https://fedoraproject.org/wiki/EPEL/zh-cn   (2台都安装)

   安装: rpm -Uvh epel-release-7.no

   yum -y install ruby ruby-libs ruby-shadow(puppet需要)

4、安装master端 

yum -y  install puppet(client端) puppet-server facter(相当于  资产管理的一个东西,能自动汇报或者获取客户端信息(如,软件环  境,操作系统等))

   配置master端:puppet的主配置文件为:/etc/puppet/puppet.conf   

 

添加如下内容:

[master]

certname=master.puppet.com  //服务端的名字

改 证书保存地址,建议放在 /var/puppet/ssl    (/var/puppet/  文件夹需要手动创建,并将拥有者改为:puppet用户和组)

[main]

ssldir=/var/puppet/ssl

mkdir -pv /var/puppet   chown -R puppet:puppet /var/puppet

创建 touch /etc/puppet/manifests/site.pp 文件,如果有就不用创建,如果没有请创建,这个文件关系到puppetmaster能否启动。

启动: service puppetmaster start       service puppet start

 

 

查看puppet支持命令:puppet help

查看支持的资源类型:
puppet describe -l
puppet describe -s TYPE
puppet describe TYPE

 

定义资源的要点:
1、同一种类型中,资源名称必须惟一;
2、资源名称是否区分大小与取决于OS;

 

六、证书申请

 

Puppet注册方式基本上有三种:自动注册、手动注册和预签名注册,不同的注册方式决定了注册的难易程度,当然安全性也会不一样。

手动注册:

   手动注册是由Agent端先发起证书申请请求,然后由Puppetserver端确***方可注册成功,这种注册方式安全系数中等,逐一注册(puppet cert --sign certnmame)在节点数量较大的情况下是比较麻烦的,效率也低,批量注册(puppet cert --sign --all)效率很高,一次性便可注册所有的Agent的请求,但是这种方式安全系数较低,因为错误的请求也会被注册上。

 

1、节点申请注册

 

[root@agent ~]# puppet agent –test

info: Creating a newSSL key foragent_cert.puppet.com

info: Caching certificate forca

info: Creating a newSSL certificate request foragent_cert.puppet.com

info: Certificate Request fingerprint (md5): 69:D2:86:E4:7F:00:E0:55:61:19:02:34:9E:9B:AF:F9

Exiting; no certificate found and waitforcert isdisabled
2、服务器端确定认证

[root@master ~]# puppet cert –list –all #查看认证情况

“agent_cert.puppet.com”  (69:D2:86:E4:7F:00:E0:55:61:19:02:34:9E:9B:AF:F9) #未认证

+ “master.puppet.com”(C0:E3:6B:76:36:EC:92:93:4D:BF:F0:8F:77:00:91:C8) (alt names: “DNS:puppet”, “DNS:puppet.puppet.com”, “DNS:master.puppet.com”)

[root@master ~]# puppet cert –sign agent_cert.puppet.com #注册agent

notice: Signed certificate request foragent_cert.puppet.com #将请求的证书正式注册

notice: Removing file Puppet::SSL::CertificateRequest agent_cert.puppet.com at ‘/var/lib/puppet/ssl/ca/requests/agent_cert.puppet.com.pem’#删除请求

[root@master ~]# puppet cert –list –all #再次查看认证情况

+ “agent_cert.puppet.com”  (3E:46:4E:75:34:9A:5A:62:A6:3C:AE:BD:49:EE:C0:F5)

+ “master.puppet.com”(C0:E3:6B:76:36:EC:92:93:4D:BF:F0:8F:77:00:91:C8) (alt names: “DNS:puppet”, “DNS:puppet..com”, “DNS:tmaster.puppet.com”)

[root@master ~]# tree /var/lib/puppet/ssl/ #另外一种查看认证的方式

/var/lib/puppet/ssl/

├── ca

│   ├── ca_crl.pem

│   ├── ca_crt.pem

│   ├── ca_key.pem

│   ├── ca_pub.pem

│   ├── inventory.txt

│   ├── private

│   │   └── ca.pass

│   ├── requests

│   ├── serial

│   └── signed

│       ├── agent_cert.puppet.com.pem  #已经注册成功

│       └── master.puppet.com.pem

├── certificate_requests

├── certs

│   ├── ca.pem

│   └── master.kisspuppet.com.pem

├── crl.pem

├── private

├── private_keys

│   └── master.puppet.com.pem

└── public_keys

└── master.puppet.com.pem

9directories, 14files

[root@agent ssl]# puppet agent –test   //agent测试是否授权成功

Info: Caching certificate for agent.puppet.com

Info: Caching certificate_revocation_list for ca

Info: Caching certificate for agent.puppet.com

Info: Retrieving pluginfacts

Info: Retrieving plugin

Info: Caching catalog for agent.puppet.com

Info: Applying configuration version ‘1458054475’

Notice: Finished catalog run in 0.01 seconds

出现以上信息说明授权成功

重新授权:

a、删除agent  rm -rf /var/puppet/ssl/*

b、删除master rm -rf  /var/puppet/ssl/ca/signed/agent1_cert.kisspuppet.com.pem

c、agent重新请求证书 puppet agent –test

d、master授权证书    puppet cert –list  puppet cert –sign agenet.puppet.com

 

七、puppet操作file、cront、user、group

 

1、创建文件测试(先授权好agent证书)

master端:

vim /etc/puppet/manifests/site.pp

node default {

file    { “/tmp/test.txt” :

content=>”Is jude puppet test
” ;

}

}

agent端:   注意:第一次编site.pp后需要重启master

puppet agenet –test

 

2.基于用户创建日志清理脚步:

vim /etc/puppet/manifests/site.pp

clearlog.sh default {

file    { “/tmp/clearlog.sh” :

content=>”find /log/ -type f -size 100KB |xargs rm -f
 ,

mode=>”0777″,

group=>”puppet”,

owner=>”puppet”,

}

}

puppet agenet –test

 

3、puppet文件推送resolv.conf

    a、添加共享模块  vim /etc/puppet/fileserver.conf

           [system_conf]

path /etc/puppet/system_conf/

allow *

cp /etc/resolv.conf   /etc/puppet/system_conf/

    b、添加文件资源

vim /etc/puppet/manifests/site.pp

file { “/etc/resolv.conf”:

mode=>644,

source=>”puppet://master.puppet.com/system_conf/resolv.conf

}

可以配置: 系统文件  hosts resolv.conf  i18n  yum配置文件、脚本等

 

4、puppet管理cron

vim /etc/puppet/manifests/site.pp

cron { “ntp togbu –by jude 20160316″:

command =>”/usr/sbin/ntpdate pool.ntp.ort >/dev/null 2>&1”,

minute =>’*/10′,

hour =>[‘2-4’],

monthday =>[2,4],

ensure=> present,

user =>puppet,

environment => “PATH=/bin:usr/bin:/usr/sbin”

}

ensure=> absent,第二次修改删除用

 

 

 

 

 

转载请注明:linux运维部落 » 推荐-自动化运维之puppet小试牛刀

转载请注明:我是IT » 推荐-自动化运维之puppet小试牛刀

喜欢 (0)or分享 (0)