puppet学习笔记
puppet优势:容易理解、用户较多、门槛低、简单、安装配置文件较少
puppet使用Ruby语言开发,安装puppet需要安装Ruby
puppet运行环境:Redhat、Centos、Windows、Ubuntu、Fedora、Debian 对于系统版本是有要求的,这里不叙述
puppet硬件要求很低,最小配置 双核、1G内存即可
puppet工作流程:
(1)Agent访问Master建立访问关系----Master对Agent证书授权签名允许Agent访问自己
(2)Master调用Agent的Facter来获取Agent的机器变量,例如:内存、CPU、IP、主机名等--Agent将信息SSL加密传输给Master,Master以变量形式获取这些信息
(3)Master接收Agent的主机的主机请求,把他们发送到本地manifests或者ENC然后进行配置查询
(4)根据Agent的HOSTNAME匹配到相应的Node节点,最终编译成Catalog
(5)Agent接受到Catalog后在本机应用Puppet的配置信息
(6)根据接受到的Catalog中的信息判断Agent在执行时有没有文件要从Master推送到Agent,如果有则想Master发起请求获取文件
(7)将Agent的信息以报告的形式上报Master,puppet 2.6或者2.6以下的版本不会推送,需要自己在配置文件中设置才可以开启,2.7以后默认开启此功能。
以上数据参考资料摘下来的。。。下面开始动手搭建
-安装环境搭建:
1.关闭iptables
2.关闭selinux
3.服务端与客户端时间同步 ntp
4.服务端&&客户端 主机名配置,并相互写入hosts (我的分别为:server.puppet.com 和 agent.puppet.com)
[root@server]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain VM_9_233_centos 192.168.10.2 server.puppet.com 192.168.10.3 agent.puppet.com [root@server]#
-安装puppet服务端与客户端
server:
[root@server]#wget http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-1.noarch.rpm #(如果下载不了,那就说明你网络无法连接过去,自行解决吧)
[root@server]#yum install puppetlabs-release-6-1.noarch.rpm
[root@server]#yum install puppet-server -y
[root@server]#/etc/init.d/puppetmaster restart
[root@server ]# ll /etc/puppet/ total 28 -rw-r--r-- 1 root root 4178 Apr 26 2016 auth.conf drwxr-xr-x 3 root root 4096 Dec 8 13:45 environments -rw-r--r-- 1 root root 1462 Apr 26 2016 fileserver.conf drwxr-xr-x 2 root root 4096 Dec 8 15:02 manifests drwxr-xr-x 2 root root 4096 Apr 26 2016 modules -rw-r--r-- 1 root root 853 Apr 26 2016 puppet.conf [root@server ]#
agent:
[root@server]#wget http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-1.noarch.rpm
[root@server]#yum install puppetlabs-release-6-1.noarch.rpm
[root@server]#yum install puppet -y
-agent证书的申请
1.agent发起请求 (agent会寻找带puppet的服务器,此时环境准备的hosts就起到作用 了,会引导到server端去申请证书)
[root@agent ~]# puppet agent --server server.puppet.com --test Info: Caching certificate for ca Info: csr_attributes file loading from /etc/puppet/csr_attributes.yaml Info: Creating a new SSL certificate request for 10-74-82-16.oss.letvcdn.com.puppet.com Info: Certificate Request fingerprint (SHA256): 0C:3C:94:57:05:40:88:43:2C:B3:C9:C5:65:5D:3A:B2:74:90:A8:31:6A:77:8B:5F:9D:E4:6A:07:8C:09:47:6F Info: Caching certificate for ca Exiting; no certificate found and waitforcert is disabled
2.server端查看是否接收到请求,并颁发证书
[root@server manifests]# puppet cert --list #查看是否有未颁发的证书请求 "192-168-10-3.agent.puppet.com" (SHA256) CA:52:54:66:2A:CC:A5:F1:2C:BA:6D:6F:78:F5:A6:D0:42:03:95:AE:0D:AA:B6:65:F6:00:64:EC:38:4A:D6:39
[root@server manifests]#puppet cert -s 192-168-10-3.agent.puppet.com #puppet cert -s 指定客户端名称颁发证书
可能出现错误的地方解析:
err: Could not request certificate: The certificate retrieved from the master does not match the agent's private key.
Exiting; failed to retrieve certificate and waitforcert is disabled
处理办法:
hosts有问题,请检查主机名与ip地址的映射关系,必须准确才可以。
随后在server删除刚才颁发的证书,
On the master:
puppet cert clean 主机名.oss.letvcdn.com
On the agent:
rm -f /var/lib/puppet/ssl/*
puppet agent -t
然后重启server和agent的服务,重新申请证书并颁发
3.agent查看是否已经通过证书验证,如下,已经通过server的认证,可以进行通信了。
[root@agent tmp]# puppet agent --server server.puppet.com --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for 10-74-82-16.oss.letvcdn.com.puppet.com Info: Applying configuration version '1481180055' Notice: Finished catalog run in 0.06 seconds
-先执行个例子让server来下发任务给agent
1. server端创建个默认节点的默认配置,puppet客户端会首先来执行/etc/puppet/manifests/site.pp文件的代码,“pp结尾代表就是puppet的后缀”
下面是创建一个文件,并在文件内输入内容“hello” //server操作
[root@server manifests]# cd /etc/puppet/manifests/ [root@server manifests]# touch site.pp [root@server manifests]# cat site.pp #default 代表的是全部客户端,如果给单个客户端下指令可以用客户端的主机名替代default node default{ file { "/tmp/lq.txt": content => "hello"; } } [root@server manifests]#
2.客户端来获取一下看看server有没有指令 //agent操作
[root@agent tmp]# puppet agent --server server.puppet.com --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for 10-74-82-16.oss.letvcdn.com.puppet.com Info: Applying configuration version '1481180578' Notice: /Stage[main]/Main/Node[default]/File[/tmp/lq.txt]/ensure: defined content as '{md5}5d41402abc4b2a76b9719d911017c592' Notice: Finished catalog run in 0.03 seconds [root@agent tmp]#
3.查看一下是否成功,ok已经执行 //agent操作
[root@agent tmp]# ll /tmp/ total 12 -rw-r--r-- 1 root root 5 Dec 8 15:02 lq.txt -rw-r--r-- 1 root root 33 Dec 8 14:30 rsyslog_md5sum.tmp -rw-r--r-- 1 root root 12 Dec 8 14:30 test.txt [root@agent tmp]# cat lq.txt hello [root@agent tmp]#
配置实例:
node default{
file {
"/tmp/llq.txt":
content => "hello";
}
##########创建一个文件,并写入内容"hello"###########################
package {
["httpd","mysql","mysql-server","php"]:
ensure=>"installed";
}
###############用yum 安装 mysql,mysql-server,php软件 ########################
service {
["mysqld","httpd"]:
ensure=>"running";
}
############启动mysqld,httpd服务########################
cron {
"ntpdate":
command=> "/usr/sbin/ntpdate pool.ntp.org >>/tmp/ntp.log 2>&1",
user => root,
hour =>"*",
minute => '*/5',
}
##############写入计划任务用户root 每5分钟执行一次######################################
file {
"/root/init.sh":
source =>"puppet://server.puppet.com/files/init.sh",
group => root,
owner => root,
mode => "755";
}
#################推送本地Init.sh文件到客户端/root目录, #############
exec {
"/root/init.sh":
cwd => "/root",
user =>root,
path => ["/usr/bin","/usr/sbin", "/bin/sh","/bin"],
}
}