一、Puppet基礎原理:
Puppet是一款使用GPLV2X協議授權的開源管理配置工具,用ruby語言開發,既可以通過客戶端—服務器的方式運行,也可以獨立運行。puppet可以為系統管理員提供方便,快捷的系統自動化管理。
二、puppet工作流程
1. 客戶端 puppet-client 向 puppet-master 發起認證請求,或使用帶簽名的證書。
2. puppet-master 告訴 puppet-client 是合法的。
3. puppet-client 調用 facter, Facter 探測出主機的一些變量, 例如主機名、 內存大小、 IP 地址等,puppet-client 將這些信息通過 SSL 連接發送到服務器端。
4. puppet-master 服務器端檢測客戶端的主機名,然后找到 manifest 對應的 node 配置,并對該部分內容進行解析。facter 送過來的信息可以作為變量處理,node 牽涉到的代碼才解析,其他沒牽涉的代碼不解析。解析分為幾個階段,首先是語法檢查,如果語法錯誤就報錯;如果語法沒錯,就繼續解析,解析的結果生成一個中間的“偽代碼”(catelog),然后把偽代碼發給客戶端。
5. puppet-client 端接收到“偽代碼”,并且執行。
6. puppet-client 端在執行時判斷有沒有 file 文件,如果有,則向 fileserver 發起請求。
7. puppet-client 端判斷有沒有配置 report,如果已配置,則把執行結果發送給服務器。
8. puppet-server 端把 puppet-client 端的執行結果寫入日志,并發送給報告系統。
三、puppet安裝
1、直接通過yum安裝老系統自帶版本。
yum install puppet -y
2、安裝最新版本
sudo rpm -ivh https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm
2.1、安裝puppet-server。
sudo puppet resource package puppet-server ensure=latest
2.2、安裝agent
sudo puppet resource package puppet ensure=latest
四、puppet資源管理
Puppet中的資源是puppet工具的核心,它是通過puppet管理配置系統的最小單位。
1、查看資源類型
puppet describe -l
2、查看資源摘要
puppet describe -s <resource_name>
3、查看資源詳細用法
puppet describe <resource_name>
4、資源的基本格式
資源名 { '標題': 屬性1 => '值', 屬性2 => '值', } #以安裝httpd為例 package { 'httpd': ensure => 'present', provider => 'rpm', }
puppet常用資源:file,filebucket,host,group,package,service,exec,cron,notify 等。
5、資源公有屬性:
before :指明資源要在某個資源之前運行
require:指明某個資源要在某個資源之后運行。
notify: 主動通知其他資源,本資源的狀態
subscibe :被動通知,當它檢測到資源狀態發生改變的時候,主動更新所在資源狀態。
還可以使用
-> 表示資源前后關系
~> 表示資源之間的通知
五、puppet語言
1)、puppet變量:
1、名稱之前必須以$開頭,賦值用=,支持追加賦值+=;
2、變量名稱有兩種格式,簡短名稱,FQN($scope::variable)。
$webserver = "httpd"
package {"httpd":
ensure => "present",
name => $webserver
}
3、作用域:top > node > local 作用域越小,優先級越高
2)、數據類型:
1、直接字串
可以使用引號,也可以不用。
換行符為\n,windows中\r\n
2、布爾型
true,false
其它類型會自動轉換為布爾型。
所有數字都是true
空字符串為false,其它字符串為true
3、數值
整數
浮點數
4、數組,逗號隔開
$array = ['httpd','mysql','php']
package {$array:ensure => installed} #依次安裝包
5、hash
{ key1 => value1,key2 => value2,…}
6、undef,聲明未定義的東西不能加上引號的。
3)、puppet支持的操作符和對應的表達式:
比較操作符:
==
!=
<,>,<=,>=,
=~ 正則匹配
!~ 正則不匹配
in
布爾操作符:
and
or
!
算術運算
+
–
/
*
<< 左移
>> 右移
$osfamily == 'CentOS' $kernel in ['Linux','solaris','freebsd']
4)、puppet的條件判斷語句:
if ..elsif..else
case
selector語句 #意思是在兩個選項中任選其中一個賦值
if $operationsystem == 'CentOS'{ notice("welcome to CentOS") } elsif $operationsystem == 'Redhat' { notice("Welcome to Redhat") } elsif $operationsystem == 'Fedora' { notice("Welcome to Fedora") } else{ notice('Welcome to ET') }
case $operationsystem { 'Solaris': { include role::solaris } 'Redhat','CentOS' : { include role::redhat } /^(Debian|Ubuntu)$/ : { include role::debian } default : { include role::generic } }
$webserver = $operatingsystem ? { /(?i-mx:'ubuntu'|debian)/ => 'apache', /(?i-mx:redhat|centos|fedora)/ => 'httpd', default => 'httpd' } i:表示忽略大小寫 - : 表示不使用某轉移符號 m:表示把 "." 當做換行符使用 x :表示互略模式中空白字符和注釋。
六、puppet類和模塊
類是具有相同特性和行為的集合。就是一組代碼塊,在需要時可以通過名稱進行調用。只定義類,并不會調用,需要聲明才可以。
1)、語法:
class class_name [inherits] [base_class] { 正常的puppet代碼 }
如果在同個模塊定義了多個類, 可以采雙冒號( :: ) 。 例如定義個nginx模塊, 模塊中 定義三個類:
class nginx { … } class nginx::config { … } class nginx::vhost { … }
2)、類的繼承(基類不能有參數):
1、繼承資源屬性
2、覆蓋資源屬性
=>
3、追加資源屬性
+>
3)、 模塊
模塊結構
module name mainfests init.pp #必須至少聲明一個類。類與模塊名相同 *.pp # mudule_name::[subdirname]::mainfect_name files:包含的是一個靜態文件。puppet的agentmaster模型。 puppet:///modles/module_name/[subdir_name/]file_name templates:模板文件 *.erb 用到ruby語言 template(''); content => template('模板文件'), lib #插件目錄。 tests :當前模塊的使用幫助或者實例文件 spec :為lib目錄的插件提供使用說明,范例的。
七、事例,puppet部署LNMP
1、假定已經安裝好puppet-server。
2、主機名通信
cat >> /etc/hosts <<EOF 192.168.198.139 puppet-server 192.168.198.160 puppet-client EOF
3、提供puppet文件
mkdir /etc/puppet/modules/lnmp/{manifests,files,templates,tests} -p
vim /etc/puppet/modules/lnmp/manifests/init.pp class lnmp { include lnmp::nginx include lnmp::mysql include lnmp::php }
vim /etc/puppet/modules/lnmp/manifests/nginx.pp class lnmp::nginx { package{'nginx': ensure => present, name => nginx, } file{'nginx.conf': ensure => file, source => 'puppet:///modules/lnmp/nginx.conf', path => '/etc/nginx/nginx.conf', require => Package['nginx'], } service{'nginx': ensure => true, enable => true, subscribe => File['nginx.conf'], } }
vim /etc/puppet/modules/lnmp/manifests/php.pp class lnmp::php { package{'php-fpm': ensure => present, name => php-fpm, } file{'www.conf': ensure => file, source => 'puppet:///modules/lnmp/www.conf', path => '/etc/php-fpm.d/www.conf', require => Package['php-fpm'], } service{'php-fpm': ensure => true, enable => true, subscribe => File['www.conf'], } }
vim /etc/puppet/modules/lnmp/manifests/mysql.pp class lnmp::mysql { package{'mysql-server': ensure => present, name => 'mysql-server', } file{'my.cnf': ensure => file, source => 'puppet:///modules/lnmp/my.cnf', path => '/etc/my.cnf', require => Package['mysql-server'], } service{'mysqld': ensure => true, enable => true, subscribe => File['my.cnf'], } }
vim /etc/puppet/manifests/site.pp node 'puppet-client' { include lnmp }
4、提供服務配置文件
cp /root/files/{nginx.conf,www.conf,my.cnf} /etc/puppet/modules/lnmp/files/
5、啟動puppet服務
[root@puppet-server modules]# puppet master --verbose --no-daemonize #第一次啟動以便觀察信息 Info: Creating a new SSL key for ca Info: Creating a new SSL certificate request for ca Info: Certificate Request fingerprint (SHA256): 7B:A9:AB:84:C0:EB:DC:83:0E:EA:8C:81:1E:25:9A:47:5C:3F:10:31:6F:F7:5C:25:BE:B7:41:3C:B8:6B:35:38 .....
[root@puppet-client ~]# puppet agent server --server puppet-server --verbose --no-daemonize #客戶端申請證書
[root@puppet-server ~]# puppet cert sign puppet-client #服務器簽署證書
#稍等一會 [root@puppet-client ~]# ss -tnl | egrep "80|3306|9000" LISTEN 0 128 *:9000 *:* LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:80 *:*
八、總結
供自己以后參考。會不斷完善
參考:
http://cuchadanfan.blog.51cto.com/9940284/d-11
http://scholar.blog.51cto.com/9985645/1673562
原創文章,作者:艾賀,如若轉載,請注明出處:http://www.www58058.com/9095
已置頂,很不錯,可以考慮出系列文章。
過獎了,才提交申請的時候。心里提醒自己,下次不能這樣寫了,單純的注意布局,對知識的理解程度不夠深,參考別人的東西不少,以后要寫出真正有價值的東西。