一、簡介
當站群伺服器數量達到一定的規模時,僅依靠人為完成批次部署站群伺服器個資源的配置,運維工作將變得繁瑣且容易出錯,為了解決這一問題,我們應該怎麼辦呢?我們可以引入一批工具,這批工具可編寫相應的 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)WordPress 模板:在配置執行之前檢測程式碼,但並不真正執行。
(3)執行:定義的配置自動部署。檢測並記錄下所發生變化的部分。
(4)報告:將期待的變化、實際發生的變化及任何修改傳送給報告系統。
如下所示為 puppet 的工作資料流示意圖
資料流說明:
首先所有的節點(Node)Node 節點將 Facts 和本機資訊傳送給 Master
Master 告訴 Node 節點應該如何配置,將這些資訊寫入 Catalog 後傳給 Node 。
Node 節點在本機進行程式碼解析驗證並執行,將結果反饋給 Master 。
Master 透過 API 將資料發給分析工具。報告完全可以透過開放 API 或與其他系統整合。
整個資料流的走向是基於 SSL 安全協議的,如下圖所示:
WordPress 模板檔案處理過程說明如下:
Puppet 透過編譯 Manifest 中的內容 (即 WordPress 模板中內容),將編譯好的程式碼存入 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
user{‘centos’:name=>’centos’,#定義使用者名稱ensure=>present,#建立使用者uid=>4000,#定使用者idgroups=>’mygrp’,#定義其他附加組require=>Group[‘mygrp’]#此資源依賴於事先建立mygrps組}group{‘mygrp’:name=>’mygrp’,#定義組名ensure=>present,#建立組,與此相反的是absent,刪除組gid=>’3000′,#組idsystem=>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@node1manifets]#puppetapply-vuser.pp#將此 manifests 清單編譯為虛擬碼 catalog,並執行,且返回執行結果至此單機模式的 WordPress 主機上 Notice:Compiledcatalogfornode1.alren.cominenvironmentproductionin0.55secondsInfo:Applyingconfigurationversion’1480561275’Notice:/Stage[main]/Main/Group[mygrp]/ensure:createdNotice:/Stage[main]/Main/User[centos]/ensure:createdNotice:Finishedcatalogrunin0.11seconds[root@node1manifets]#idcentosuid=4000(centos)gid=4000(centos)groups=4000(centos),3000(mygrp)[root@node1manifets]#viuser.pp[root@node1manifets]#puppetapply-vuser.ppNotice:Compiledcatalogfornode1.alren.cominenvironmentproductionin0.58secondsInfo:Applyingconfigurationversion’1480561376’Notice:/Stage[main]/Main/Group[mygrp]/ensure:removedNotice:/Stage[main]/Main/User[centos]/ensure:removedNotice:Finishedcatalogrunin0.13seconds[root@node1manifets]#idcentosid:centos:nosuchuser[root@node1manifets]#
例項二:此 manifests 程式碼為安裝 httpd 包,為其提供配置檔案,並且啟動服務
service{‘httpd’:ensure=>running,enable=>true,restart=>’systemctlrestarthttpd.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,absentname: 包名 source:程式包來源,僅對不會自動下載的相關程式包的 provider 有用,例如 rpm 或 dpkgservicec 常用屬性: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@node1manifets]#puppetapply-vweb.ppNotice:Compiledcatalogfornode1.alren.cominenvironmentproductionin1.48secondsInfo:Applyingconfigurationversion’1480563754’Info:Computingchecksumonfile/etc/httpd/conf/httpd.confInfo:/Stage[main]/Main/File[httpd.conf]:Filebucketed/etc/httpd/conf/httpd.conftopuppetwithsum42566ec31df37e3d44429b285d015e1dNotice:/Stage[main]/Main/File[httpd.conf]/content:contentchanged'{md5}42566ec31df37e3d44429b285d015e1d’to'{md5}8b01e334a6e975b659df5dd351923ccb’Info:/Stage[main]/Main/File[httpd.conf]:SchedulingrefreshofService[httpd]Notice:/Stage[main]/Main/Service[httpd]:Triggered’refresh’from1eventsNotice:Finishedcatalogrunin1.95seconds[root@node1manifets]#ss-tnlStateRecv-QSend-QLocalAddress:PortPeerAddress:PortLISTEN05192.168.122.1:53*:*LISTEN0128*:22*:*LISTEN0128127.0.0.1:631*:*LISTEN0100127.0.0.1:25*:*LISTEN032:::21:::*LISTEN0128:::22:::*LISTEN0128::1:631:::*LISTEN0128:::8088:::*LISTEN0100::1:25:::*[root@node1manifets]#
例項三:每三分鐘同步下系統時間, 寫入定時任務
cron{‘synctime’:command=>’/usr/sbin/ntpdate10.1.0.1&>/dev/null’,#須執行的命令ensure=>present,#建立任務計劃minute=>’*/5′,#過多長是時間執行user=>root,#以誰的身份執行}[root@node1testmanifests]#puppetapply-vcron.ppNotice:Compiledcatalogfornode1.alren.cominenvironmentproductionin0.18secondsInfo:Applyingconfigurationversion’1480593969’Notice:/Stage[main]/Main/Cron[synctime]/minute:minutechanged’*/3’to’*/5’Notice:Finishedcatalogrunin0.09seconds[root@node1testmanifests]#crontab-l#HEADER:Thisfilewasautogeneratedat2016-12-0120:06:10+0800bypuppet.#HEADER:Whileitcanstillbemanagedmanually,itisdefinitelynotrecommended.#HEADER:Noteparticularlythatthecommentsstartingwith’PuppetName’should#HEADER:notbedeleted,asdoingsocouldcauseduplicatecronjobs.#PuppetName:synctime*/5****/usr/sbin/ntpdate10.1.0.1&>/dev/null#檢視到此任務計劃存在 [root@node1testmanifests]#
例項四: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=>’systemctlrestarthttpd.service’,subscribe=>File[‘httpd.conf’],#通知其他的資源進行重新整理操作}[root@node1testmanifests]#puppetapply-vif.ppNotice:Compiledcatalogfornode1.alren.cominenvironmentproductionin1.53secondsInfo:Applyingconfigurationversion’1480594920’Info:Computingchecksumonfile/etc/httpd/conf/httpd.confInfo:FileBucketgotaduplicatefile{md5}8b01e334a6e975b659df5dd351923ccbInfo:/Stage[main]/Main/File[httpd.conf]:Filebucketed/etc/httpd/conf/httpd.conftopuppetwithsum8b01e334a6e975b659df5dd351923ccbNotice:/Stage[main]/Main/File[httpd.conf]/content:contentchanged'{md5}8b01e334a6e975b659df5dd351923ccb’to'{md5}42566ec31df37e3d44429b285d015e1d’Info:/Stage[main]/Main/File[httpd.conf]:SchedulingrefreshofService[httpd]Notice:/Stage[main]/Main/Service[httpd]:Triggered’refresh’from1eventsNotice:Finishedcatalogrunin1.97seconds[root@node1testmanifests]#ss-tnlStateRecv-QSend-QLocalAddress:PortPeerAddress:PortLISTEN05192.168.122.1:53*:*LISTEN0128*:22*:*LISTEN0128127.0.0.1:631*:*LISTEN0100127.0.0.1:25*:*LISTEN0128:::80:::*LISTEN032:::21:::*LISTEN0128:::22:::*LISTEN0128::1:631:::*LISTEN0100::1:25:::*[root@node1testmanifests]#
例項五: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=>’systemctlrestarthttpd.service’,subscribe=>File[‘httpd.conf’],}[root@node1testmanifests]#puppetapply-vcase.ppNotice:Compiledcatalogfornode1.alren.cominenvironmentproductionin1.61secondsInfo:Applyingconfigurationversion’1480595667’Info:Computingchecksumonfile/etc/httpd/conf/httpd.confInfo:FileBucketgotaduplicatefile{md5}42566ec31df37e3d44429b285d015e1dInfo:/Stage[main]/Main/File[httpd.conf]:Filebucketed/etc/httpd/conf/httpd.conftopuppetwithsum42566ec31df37e3d44429b285d015e1dNotice:/Stage[main]/Main/File[httpd.conf]/content:contentchanged'{md5}42566ec31df37e3d44429b285d015e1d’to'{md5}3dfa14b023127a3766bddfe15fe14b9a’Info:/Stage[main]/Main/File[httpd.conf]:SchedulingrefreshofService[httpd]Notice:/Stage[main]/Main/Service[httpd]:Triggered’refresh’from1eventsNotice:Finishedcatalogrunin1.91seconds[root@node1testmanifests]##可根據自行的語言習慣選擇適合的語法,如下所示同樣可達到上訴的目的 $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=>’systemctlrestarthttpd.service’,subscribe=>File[‘httpd.conf’],}
四、 puppet 類與繼承
puppet 中命名的程式碼模組,經常需要被使用,如如重寫則程式碼冗餘,使用定義一組通用目標的資源,可在 puppet 全域性呼叫,就能解決這類問題,類可以被繼承,也可以包含子類。
類的語法格式有兩種,呼叫類的方法常用的有三種,可以在類中傳遞引數等靈活的操作,如以下例項:
classwebserver{#類申明後續呼叫後才生效,呼叫的方法常用的有三種 $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,}}includewebserver#使用include進行呼叫 #申明呼叫類的第二種方法classweb($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=>’systemctlrestarthttpd.service’,subscribe=>File[‘httpd.conf’],}}class{‘web’:webserver=>’apache2′,#呼叫類}[root@node1~]#puppetapply-v–noopclass2.ppNotice:Compiledcatalogfornode1.alren.cominenvironmentproductionin1.53secondsInfo:Applyingconfigurationversion’1480596978’Notice:/Stage[main]/Web/Package[apache2]/ensure:current_valueabsent,shouldbepresent(noop)Notice:/Stage[main]/Web/File[httpd.conf]/content:current_value{md5}3dfa14b023127a3766bddfe15fe14b9a,shouldbe{md5}8b01e334a6e975b659df5dd351923ccb(noop)Info:/Stage[main]/Web/File[httpd.conf]:SchedulingrefreshofService[httpd]Notice:/Stage[main]/Web/Service[httpd]:Wouldhavetriggered’refresh’from1eventsNotice:Class[Web]:Wouldhavetriggered’refresh’from3eventsNotice:Stage[main]:Wouldhavetriggered’refresh’from1eventsNotice:Finishedcatalogrunin0.65seconds[root@node1~]#
類的繼承:子類可繼承父類的資源屬性,同時可定義父類不存在的額資源屬性,一個父類可同時被多個子類所繼承
classnginx{#定義父類資訊package{‘nginx’:ensure=>installed,#安裝程式包}service{‘nginx’:#啟動程式ensure=>running,enable=>true,require=>Package[‘nginx’],}}classnginx::webinheritsnginx{#繼承父類的原有屬性,同時增加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’),#此WordPress模板需事先存在方可使用require=>Package[‘nginx’],}Service[‘nginx’]{subscribe=>[File[‘ngx-web.conf’],File[‘nginx.conf’]],#通知其他資源進行重新整理操作}}includenginx::web#呼叫宣告的類
五、 puppetWordPress 模板(此內容不過多解釋,需自行加強)
WordPress 模板是一個按照約定的、預定的結構存放了多個檔案或子目錄的目錄,目錄裡面的這些檔案或子目錄必須遵循一定的格式的命名規範,puppet 會在配置的路徑下查詢所需的資源模組。模組的名稱通常是隻能以小寫字母開頭,可以包含小寫字母,數字下劃線,但是不能使用 main 和 settings 。
模組的組成部分:
manifests/: 資源清單
init.pp: 必須定義一個類,類名必須與模組名相同;
files/: 靜態檔案
templates/:WordPress 模板檔案
lib/: 外掛外掛目錄,常用於儲存自定義的 facts 以及自定義型別
spec/:類似於 tests 目錄,儲存 lib/目錄下外掛外掛的使用幫助和範例;
tests/:當前模組的使用幫助或使用範例檔案;
總結:運維工具有很多例如:Puppet,Ansible,slackstack 等,puppet 還是一個很好用的自動化運維工具,大大減輕的運維人員的重複操作,提高了工作效率,在運維過程中可根據業務需求選擇不同的運維工具,在站群伺服器數量不是很大的情形下可使用輕量級的 ansible,在站群伺服器數量達到一定的規模時使用重量級的 puppet 相對來說效率更高。當面臨有得選擇的時候想起一句話:籮卜白菜各有所愛,適合自己專注、精通一個運維工具比全會那麼一點,解決問題更有優勢。
原文地址:http://www.92to.com/bangong/2016/12-09/14172517.html