简介
Apache旗下的一个分布式架构软件,主要作用是创建、管理、监控Hadoop集群(指的是Hadoop生态圈,如Hive、Hbase等),简而言之,Ambari就是为了让Hadoop及其相关的大数据软件更容易使用的一个工具。Ambari主要由两部分组成:Ambari Server 和 Ambari Agent。简单来说,用户通过 Ambari Server 通知 Ambari Agent 安装对应的软件;Agent 会定时地发送各个机器每个软件模块的状态给 Ambari Server,最终这些状态信息会呈现在 Ambari 的 GUI,方便用户了解到集群的各种状态,并进行相应的维护。
架构分析

Ambari的结构图如上所示,主要有四部分:
- Ambari-Server:web服务器,提供REST 接口给Agent 和 Web 访问
- Ambari-Agent:在集群中每台机器上都会部署一个agent,主要负责执行来自Server端的命令(安装、启动、停止某些服务),并定期上报对应服务上软件模块的运行状态
- Ambari-Web:供用户安装服务及展示已安装服务的状态
- Ambari-metrics-collector:web 服务器,将 Metrics Monitor 和 Metrics Sink 汇报上来的监控信息存储,同时提供监控信息查询接口,供 Ambari Server 进行查询
Ambari-Server

ambari-server是一个web server,它维护着自己的一个有限状态机FSM,并将状态机存储在数据库中,前期数据库主要采用postgres,主要功能点如下:
- 对外,Ambarii-Server提供ambari web,rest api,ambari shell三大方式操作机群
- ambari将集群的配置、各个服务的配置等信息存在ambari server端的DB中(比如可以是postgresql)
- ambari-server与amabri-agent通过RPC进行信息交互,agent向server定期汇报心跳,server将command通过response发送给agent,agent执行本地命令并将执行结果进行反馈
- ambari有自己的一套监控、告警、镜像服务,以可插拔的形式供上层服务调用,如下图所示

Ambari-Agent
ambari-agent是一个无状态的进程,其功能主要分两部分:
- 采集所在节点的信息,定制执行用户定义的告警服务,并且汇总发心跳汇报给ambari-server
- 处理ambari-server的执行请求
它的内部存在两种两种队列:
- 消息队列MessageQueue,包括节点状态信息(包括注册信息)和执行结果信息,并且汇总后通过心跳发送给ambari-server
- 操作队列ActionQueue,用于接收ambari-server返回过来的待执行操作,然后能过执行器按序调用puppet或python脚本等模块完成任务

整体来说,Ambari的工作流如下图所示:
- HeartBeatHandler,收集组件当前状态(通过ResultMap)、Command 执行结果(通过ResultMap)、Alert 检查结果(通过 AlertCollect)等,封装到 HTTP Request 当中,发送给 Ambari Server;Ambari Server 响应请求,通过 HTTP Response 带回来需要执行的 Command、需要终止的 Command、发生修改的 Config、发生修改的 Alert 定义等,并把 Comand 和 修改的 Config 封装为 Agent Command 对象,存储到 CommandQueue 中;把修改的 Alert 定义,更新到 Alert definitions 文件中(如果 Alert definitions 文件发生了变化,需要通知 AlertSchedulerHandler 重新加载一遍
- ActionExecutor,定期从 CommandQueue 中加载需要执行的 Command,找到 Command 对应的 Python 脚本,执行脚本,并把结果存储到 ResultMap 中
- AlertSchedulerHandler,从 Alert definitions 文件中加载所有 Alert 定义,根据 Alert 定义,找到对应的 Python 脚本,周期性执行,并把结果存储到 AlertCollect 中

Ambari Metrics
Ambari Metrics System 简称为 AMS,它主要为系统管理员提供了集群性能的监察功能。Metrics 一般分为 Cluster、Host 以及 Service 三个层级。Cluster 和 Host 级主要负责监察集群机器相关的使用情况,而 Service 级别则负责监控Host Component 对集群资源的使用情况。AMS 涉及的模块如下图所示:

AMS是一个Master-Slave架构,Master节点是Metrics Collector,Slave节点是 Metrics Monitor 和 Hadoop Sinks,Slave模块负责收集信息并发送给Collector,前者主要负责收集机器本身相关的指标,例如 CPU、Mem、Disk 等;后者则负责收集Service内部软件模块的性能数据,例如该模块占用了多少 Mem,以及该模块的 CPU 占用率等。
添加自定义服务
添加一个自定义服务主要包含以下步骤:
1) 创建服务文件夹
2) 创建描述服务信息的Metainfo.xml
3) 创建关于安装、配置、启动、停止等命令的脚本
4) 给自定义服务添加配置
下面以添加一个自定义服务SAMPLESRV为例,来说明Ambari的使用机制,SAMPLESRV包含三个组件,分别为MASTER、SLAVE 、CLIENT。
创建自定义服务
1) 创建服务,在/ambari/ambari-server/src/main/resources/stacks/TDS/1.0/services/目录下创建SAMPLESRV目录1
2mkdir SAMPLESRV
cd SAMPLESRV
2) 在SAMPLESRV目录中创建metainfo.xml文件,内容如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52<?xml version="1.0"?>
<metainfo>
<schemaVersion>2.0</schemaVersion>
<services>
<service>
<name>SAMPLESRV</name>
<displayName>New Sample Service</displayName>
<comment>A New Sample Service</comment>
<version>1.0.0</version>
<components>
<component>
<name>SAMPLESRV_MASTER</name>
<displayName>Sample Srv Master</displayName>
<category>MASTER</category>
<cardinality>1</cardinality>
<commandScript>
<script>scripts/master.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</component>
<component>
<name>SAMPLESRV_SLAVE</name>
<displayName>Sample Srv Slave</displayName>
<category>SLAVE</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/slave.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</component>
<component>
<name>SAMPLESRV_CLIENT</name>
<displayName>Sample Srv Client</displayName>
<category>CLIENT</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</component>
</components>
<osSpecifics>
<osSpecific>
<osFamily>any</osFamily>
</osSpecific>
</osSpecifics>
</service>
</services>
</metainfo>
metainfo.xml 文件描述了服务信息、组件信息及组件对应的命令脚本,组件有三种类型,分别是MASTER、SLAVE、CLIENT,下表列出了每种组件必须支持的命令,这些命令的执行内容需要写在对应的命令脚本里,同时自己也可以添加自定义命令。
| 组件类型 | 默认的生命周期命令 |
|---|---|
| MASTER | install, start, stop, configure, status |
| SLAVE | install, start, stop, configure, status |
| CLIENT | install, configure, status |
3) 创建命令脚本1
2mkdir -p package/scripts
cd package/scripts
4) 在scripts目录中建立命令脚本,如下
master.py1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import sys
from resource_management import *
class Master(Script):
def install(self, env):
print 'Install the Sample Srv Master';
def configure(self, env):
print 'Configure the Sample Srv Master';
def stop(self, env):
print 'Stop the Sample Srv Master';
def start(self, env):
print 'Start the Sample Srv Master';
def status(self, env):
print 'Status of the Sample Srv Master';
if __name__ == "__main__":
Master().execute()
slave.py1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import sys
from resource_management import *
class Slave(Script):
def install(self, env):
print 'Install the Sample Srv Slave';
def configure(self, env):
print 'Configure the Sample Srv Slave';
def stop(self, env):
print 'Stop the Sample Srv Slave';
def start(self, env):
print 'Start the Sample Srv Slave';
def status(self, env):
print 'Status of the Sample Srv Slave';
if __name__ == "__main__":
Slave().execute()
sample_client.py1
2
3
4
5
6
7
8
9import sys
from resource_management import *
class SampleClient(Script):
def install(self, env):
print 'Install the Sample Srv Client';
def configure(self, env):
print 'Configure the Sample Srv Client';
if __name__ == "__main__":
SampleClient().execute()
添加自定义命令
1) 在metainfo.xml的SAMPLESRV_CLIENT组件中添加自定义命令SOMETHINGCUSTOM1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<component>
<name>SAMPLESRV_CLIENT</name>
<displayName>Sample Srv Client</displayName>
<category>CLIENT</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
<customCommands>
<customCommand>
<name>SOMETHINGCUSTOM</name>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</customCommand>
</customCommands>
</component>
2) 在package/scripts/sample_client.py中添加命令对应的处理方法1
2
3
4
5
6
7
8
9
10
11
12
13import sys
from resource_management import *
class SampleClient(Script):
def install(self, env):
print 'Install the Sample Srv Client';
def configure(self, env):
print 'Configure the Sample Srv Client';
def somethingcustom(self, env):
print 'Something custom';
if __name__ == "__main__":
SampleClient().execute()
为自定义服务添加配置
1) 修改metainfo.xml,为SAMPLESRV_CLIENT添加配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<component>
<name>SAMPLESRV_CLIENT</name>
<displayName>Sample Srv Client</displayName>
<category>CLIENT</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
<configFiles>
<configFile>
<type>xml</type>
<fileName>test-config.xml</fileName>
<dictionaryName>test-config</dictionaryName>
</configFile>
</configFiles>
</component>
2) 创建SAMPLESRV/configuration目录1
2mkdir -p configuration
cd configuration
3) 在configuration目录下创建配置文件test-config.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>some.test.property</name>
<value>this.is.the.default.value</value>
<description>This is a test description.</description>
</property>
<property>
<name>another.test.property</name>
<value>5</value>
<description>This is a second test description.</description>
</property>
</configuration>
4) 可以在configuration-dependencies 部分设置配置依赖关系,可以针对服务全体或者部分组件的设置添加,依赖的配置文件修改后对应的组件会自动重启,如下1
2
3
4
<configuration-dependencies>
<config-type>test-config</config-type>
</configuration-dependencies>
附录
metainfo.xml 说明
metainfo.xml是自定义服务的描述文件,下面来看其中重要的组成元素
1) service字段,描述服务
| 字段 | 描述 |
|---|---|
| name | 服务名,必须唯一 |
| displayName | 服务在web UI上的显示名 |
| vesrion | 版本号 |
| components | 包含的组件名 |
| osSpecifics | 操作系统及特定的安装包 |
| CommandScript | 可选,如果出现在service中,配置service check执行脚本 |
2) service/components 字段,描述服务包含的组件
| 字段 | 描述 |
|---|---|
| name | 组件名 |
| displayName | 组件显示名 |
| category | 组件分类-MASTER, SLAVE和CLIENT |
| commandScript | 组件命令所在脚本 |
| cardinality | 允许/需要的实例数量 |
| dependencies | 可选, 组件依赖的组件 |
| customCommands | 可选,标准命令之外自定义的命令 |
| configFiles | 可选,客户端配置可以下载的配置文件 |
3) service/osSpecifics字段,描述支持的操作系统及需要安装的包
| 字段 | 描述 |
|---|---|
| osFamily | 包适用的操作系统 |
| packages | 部署服务需要的包 |
| package/name | 包名,被yum/apt等适用 |
4) service/component/customCommands/customCommand字段,描述组件的自定义命令
| 字段 | 描述 |
|---|---|
| name | 自定义命令名称 |
| commandScript | 实现自定义命令的脚本 |
| commandScript/script | 脚本路径 |
| commandScript/scriptType | 可选,脚本类型,目前只支持python |
| commandScript/timeout | 可选,命令超时时间 |