一, 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 服务
WX20170728-150942@2x.PNG
三, 丰富自定义 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>
WX20170728-152404@2x.PNG
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 root 7948 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 root 6201 May 27 10:11 hadoop-policy.xml -rwxr-xr-x 1 admin root 8879 May 27 10:11 hdfs-log4j.xml -rwxr-xr-x 1 admin root 8192 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 root 2627 May 27 10:11 ssl-client.xml -rwxr-xr-x 1 admin root 2959 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>
来源: https://www.cnblogs.com/felixzh/p/10594952.html