Ambari自定義Service
一、Ambari基本架構

img016.jpg
Ambari Server 會讀取 Stack 和 Service 的配置檔案。當用 Ambari 建立服務的時候,Ambari Server 傳送 Stack 和 Service 的配置檔案以及 Service 生命週期的控制指令碼到 Ambari Agent。Agent 拿到配置檔案後,會下載安裝公共源裡軟體包(Redhat,就是使用 yum 服務)。安裝完成後,Ambari Server 會通知 Agent 去啟動 Service。之後 Ambari Server 會定期傳送命令到 Agent 檢查 Service 的狀態,Agent 上報給 Server,並呈現在 Ambari 的 GUI 上。
二、建立Ambari自定義Service
#AmbariServer資原始檔在/var/lib/ambari-server/resources目錄 #cd到Ambari Stack目錄下 (目前最新版為2.6) cd /var/lib/ambari-server/resources/stacks/HDP/2.6/services #建立自定義Service目錄 (以大寫ServiceName命令,這裡以My Service為例) mkdir MYSERVICE cd MYSERVICE
1.編輯metainfo.xml
<?xml version="1.0"?> <metainfo> <schemaVersion>2.0</schemaVersion> <services> <service> <!----> <!-- 編寫Service名稱和Service資訊 --> <name>MYSERVICE</name> <displayName>My Service</displayName> <comment>this is comment</comment> <version>1.0</version> <components> <component> <!-- 編寫Master元件 --> <name>MYMASTER</name> <displayName>My Master</displayName> <category>MASTER</category> <cardinality>1</cardinality> <commandScript> <script>scripts/master.py</script> <scriptType>PYTHON</scriptType> <timeout>5000</timeout> </commandScript> </component> <component> <!-- 編寫Slave元件 --> <name>MYSALVE</name> <displayName>My Slave</displayName> <category>SLAVE</category> <cardinality>1+</cardinality> <commandScript> <script>scripts/slave.py</script> <scriptType>PYTHON</scriptType> <timeout>5000</timeout> </commandScript> </component> </components> <osSpecifics> <osSpecific> <osFamily>any</osFamily> </osSpecific> </osSpecifics> </service> </services> </metainfo>
- components 下編寫多個元件。
- category 為元件的角色,支援Master、Slave、和Client
- cardinality 為節點數量,可以為1、1+、或數值範圍1-2
- commandScript 為元件生命週期回撥的指令碼
2.編寫Master元件生命週期回撥指令碼
mkdir -p package/scripts vim package/scripts/master.py
import sys, os from resource_management import * from resource_management.core.exceptions import ComponentIsNotRunning from resource_management.core.environment import Environment from resource_management.core.logger import Logger class Master(Script): def install(self, env): print "Install My Master" def configure(self, env): print "Configure My Master" def start(self, env): print "Start My Master" def stop(self, env): print "Stop My Master" def status(self, env): print "Status..." if __name__ == "__main__": Master().execute()
3.編寫Slave元件生命週期回撥指令碼
package/scripts/slave.py
import sys, os from resource_management import * from resource_management.core.exceptions import ComponentIsNotRunning from resource_management.core.environment import Environment from resource_management.core.logger import Logger class Slave(Script): def install(self, env): print "Install My Slave" def configure(self, env): print "Configure My Slave" def start(self, env): print "Start My Slave" def stop(self, env): print "Stop My Slave" def status(self, env): print "Status..." if __name__ == "__main__": Slave().execute()
4.重啟AmbariServer
ambari-server restart
5.加入剛才新增的My Service服務
-
在Ambari Web上。點選Actions-> Add Service 新增My Service服務
三、豐富自定義Service功能
1.增加Service Check邏輯
在 Service 的 metainfo.xml 中,commandScript 欄位就是用來配置 service check 指令碼入口。如果一個 Service 的 metainfo.xml 有該欄位,那麼在 Service 的 Action 列表中就會出現“Run Service Check”這個命令。
當用戶在 WEB 中點選“Run Service Check”時,Ambari Server 會隨機通知一個該 Service 所在機器上的 Agent 程序,然後由 Agent 執行該 Service check 指令碼。
<commandScript> <script>scripts/master/my_check.py</script> <scriptType>PYTHON</scriptType> <timeout>300</timeout> </commandScript>
2.增加Service 的配置項
這裡需要在Service的metainfo.xml 中增加<configuration-dependencies>欄位。該欄位就是用來關聯一個 Service 與配置項檔案入口
每一行<config-type>欄位,用來指定一個配置檔案。一個 Service 可以同時指定多個配置檔案。不過所有的配置檔案必須放在 Service 的 configuration 目錄中。
<!-- 以HDFS為例 --> <metainfo> <services> <service> <!-- 省略... --> <configuration-dependencies> <!-- 在下面指定配置檔案 --> <config-type>core-site</config-type> <config-type>hdfs-site</config-type> <config-type>hadoop-env</config-type> <config-type>hadoop-policy</config-type> <config-type>hdfs-log4j</config-type> <config-type>ranger-hdfs-plugin-properties</config-type> <config-type>ssl-client</config-type> <config-type>ssl-server</config-type> <config-type>ranger-hdfs-audit</config-type> <config-type>ranger-hdfs-policymgr-ssl</config-type> <config-type>ranger-hdfs-security</config-type> </configuration-dependencies> <restartRequiredAfterRackChange>true</restartRequiredAfterRackChange> </service> </services> </metainfo>
#configuration目錄下的檔案: [root@node1 2.1.0.2.0]# ll configuration/ total 84 -rwxr-xr-x 1 admin root7948 May 27 10:11 core-site.xml -rwxr-xr-x 1 admin root 16723 May 27 10:11 hadoop-env.xml -rwxr-xr-x 1 admin root6201 May 27 10:11 hadoop-policy.xml -rwxr-xr-x 1 admin root8879 May 27 10:11 hdfs-log4j.xml -rwxr-xr-x 1 admin root8192 May 27 10:11 hdfs-logsearch-conf.xml -rwxr-xr-x 1 admin root 19139 May 27 10:11 hdfs-site.xml -rwxr-xr-x 1 admin root2627 May 27 10:11 ssl-client.xml -rwxr-xr-x 1 admin root2959 May 27 10:11 ssl-server.xml
配置檔案中,其實就是指定了一些鍵值對的屬性,以及一個描述。當在 Ambari 的 WEB 中增加這個 Service 時,Ambari Server 會讀取這些資訊,並顯示到該 service 的配置頁面中(Customize Service 和 config 頁面)。預設情況下,如果一個配置項沒有配置預設值,使用者則必須輸入。如果一個項允許為空,則需要在<property>中增加 require-input="false“的屬性。
3.增加自定義Command
以RebalanceHDFS為例,在Service的metainfo.xml中增加以下內容
當點選RebalanceHDFS後 則觸發scripts/namenode.py指令碼
<customCommands> <customCommand> <name>REBALANCEHDFS</name> <background>true</background> <commandScript> <script>scripts/namenode.py</script> <scriptType>PYTHON</scriptType> </commandScript> </customCommand> </customCommands>
