天天看點

Puppet service資源介紹(二十五)

service資源

service資源主要對服務做啟動、重新開機、關閉,監控程序的狀态等.

service的參數:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<code>service { </code><code>'resource title'</code><code>:</code>

<code>  </code><code>name       =&gt; </code><code># (namevar) The name of the service to run.  This name is...</code>

<code>  </code><code>ensure     =&gt; </code><code># Whether a service should be running.  Valid...</code>

<code>  </code><code>binary     =&gt; </code><code># The path to the daemon.  This is only used for...</code>

<code>  </code><code>control    =&gt; </code><code># The control variable used to manage services...</code>

<code>  </code><code>enable</code>     <code>=&gt; </code><code># Whether a service should be enabled to start at...</code>

<code>  </code><code>flags      =&gt; </code><code># Specify a string of flags to pass to the startup </code>

<code>  </code><code>hasrestart =&gt; </code><code># Specify that an init script has a `restart...</code>

<code>  </code><code>hasstatus  =&gt; </code><code># Declare whether the service's init script has a...</code>

<code>  </code><code>manifest   =&gt; </code><code># Specify a command to config a service, or a path </code>

<code>  </code><code>path       =&gt; </code><code># The search path for finding init scripts....</code>

<code>  </code><code>pattern    =&gt; </code><code># The pattern to search for in the process table...</code>

<code>  </code><code>provider   =&gt; </code><code># The specific backend to use for this `service...</code>

<code>  </code><code>restart    =&gt; </code><code># Specify a *restart* command manually.  If left...</code>

<code>  </code><code>start      =&gt; </code><code># Specify a *start* command manually.  Most...</code>

<code>  </code><code>status     =&gt; </code><code># Specify a *status* command manually.  This...</code>

<code>  </code><code>stop       =&gt; </code><code># Specify a *stop* command...</code>

<code>  </code><code># ...plus any applicable metaparameters.</code>

<code>}</code>

binary:指定二進制程式的系統路徑,用于那些不支援init的作業系統.如果守護程序沒有自啟動腳本,可以通過此屬性啟動服務.

enable:指定服務在開機的時候是否啟動,可以設定true和false值.

ensure:是否運作服務,running表示運作,stopped表示停止服務.

hasrestart:指出管理腳本是否支援restart值,如果不支援,就用stop值和start值實作restart效果.可以設定為true和false.

hasstatus:指出管理腳本是否支援status值.puppet用status值來判斷服務是否已經在運作,如果系統不支援status值,puppet可以利用查找運作程序清單裡面是否有服務名來判斷服務是否在運作.可以設定為true和false.

name:守護程序的名字,如果記得不準确可以在/etc/init.d目錄下面查找.

path:啟動腳本的搜尋路徑,可以用冒号分割多個路徑,或者用數組指定.

pattern:設定比對程序的字元串,當服務停止時,通過程序清單來判斷服務的狀态,主要用于不支援init腳本的系統.

restart:指定用于重新開機服務的腳本,否則隻能手動先停止該服務再啟動該服務.

start:指定啟動服務的指令,通常init模式的管理腳本都支援,不需要手工指定.

status:指定status指令,如果不指定,就從近乎才呢過清單查詢該服務.

stop:指定停止服務的腳本.

provider:作業系統支援的init模式的管理腳本,支援base|daemontools|init,預設為init.

示例一:

啟動httpd服務,設定開機自啟動.

注意:啟動服務的前提是httpd軟體包已經安裝.

<code>service {</code><code>"httpd"</code><code>:</code>

<code>    </code><code>ensure =&gt; running,</code>

<code>    </code><code>enable</code> <code>=&gt; </code><code>true</code><code>,</code>

<code>[root@sh-web1 ~]</code><code># /etc/init.d/httpd status</code>

<code>httpd (pid  121592) is running...</code>

<code>[root@sh-web1 ~]</code><code># chkconfig --list | grep httpd</code>

<code>httpd          0:off1:off2:on3:on4:on5:on6:off</code>

示例二:

之前文章寫過怎麼将源碼包封裝成rpm包,下面是封裝好的mysql rpm包,測試puppet代碼.

<code>[root@sh-web1 ~]</code><code># rpm -ivh mysql-5.6.29-1.x86_64.rpm </code>

<code>Preparing...                </code><code>########################################### [100%]</code>

<code>   </code><code>1:mysql                  </code><code>########################################### [100%]</code>

<code>`</code><code>/usr/include/mysql/include</code><code>' -&gt; `/data/mysql/include'</code>

<code>[root@sh-web1 ~]</code><code># source /etc/profile</code>

安裝目錄在/data目錄下.

<code>[root@sh-web1 ~]</code><code># ls /data/</code>

<code>mysql  mysqldata</code>

啟動腳本是/data的腳本cp一份放到/etc/init.d目錄下.

下面是puppet啟動mysql的代碼.

<code>[root@sh-web1 ~]</code><code># cat mysqld.pp </code>

<code>service {</code><code>"mysqld"</code><code>:</code>

<code>    </code><code>hasrestart =&gt; </code><code>true</code><code>,</code>

<code>    </code><code>hasstatus =&gt; </code><code>true</code><code>,</code>

<code>    </code><code>provider =&gt; init,</code>

<code>    </code><code>path =&gt; </code><code>"/etc/init.d"</code><code>,</code>

<code>    </code><code>restart =&gt; </code><code>"/etc/init.d/mysqld reload"</code><code>,</code>

<code>    </code><code>start =&gt; </code><code>"/etc/init.d/mysqld start"</code><code>,</code>

<code>    </code><code>stop =&gt; </code><code>"/etc/init.d/mysqld stop"</code><code>,</code>

運作結果:

<code>[root@sh-web1 ~]</code><code># puppet apply mysqld.pp </code>

<code>Notice: Compiled catalog </code><code>for</code> <code>sh-web1.localdomain </code><code>in</code> <code>environment production </code><code>in</code> <code>0.06 seconds</code>

<code>Notice: </code><code>/Stage</code><code>[main]</code><code>/Main/Service</code><code>[mysqld]</code><code>/ensure</code><code>: ensure changed </code><code>'stopped'</code> <code>to </code><code>'running'</code>

<code>Notice: Finished catalog run </code><code>in</code> <code>2.44 seconds</code>

檢視結果是否啟動.

<code>[root@sh-web1 ~]</code><code># /etc/init.d/mysqld status</code>

<code>MySQL running (123039)                                     [  OK  ]</code>

本文轉自青衫解衣 51CTO部落格,原文連結:http://blog.51cto.com/215687833/1973746

繼續閱讀