官网:
https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=38571133
一、背景
栈的定义可以在源代码树中找到/ambari-server/src/main/resources/stacks
。当你安装
Ambari Server服务之后,栈的定义可以被发现/var/lib/ambari-server/resources/stacks
。
二、结构
一个栈的结构定义如下
|_ stacks
|_ <stack_name>
|_ <stack_version>
metainfo.xml
|_ hooks
|_ repos
repoinfo.xml
|_ services
|_ <service_name>
metainfo.xml
metrics.json
|_ configuration
{configuration files}
|_ package
{files, scripts, templates}
三、定义一个服务和组件
在service里的metainfo.xml文件描述这个服务,
服务的组件和管理脚本用于执行命令。一个组件的服务可以是MASTER,SLAVE或CLIENT类别。这个<category>告诉Ambari默认命令应该用于管理和监控组件。为每个组件指定< commandScript >执行命令时使用。有一个默认命令定义组件必须支持。
Ambari支持不同的命令脚本用PYTHON编写的。类型是用来知道如何执行命令脚本。你也可以创建自定义命令除了default lifecycle commands之外你的组件需要去支持。
例如,YARN Service描述ResourceManager组件配置metainfo.xml如下:
<component>
<name>RESOURCEMANAGER</name>
<category>MASTER</category>
<commandScript>
<script>scripts/resourcemanager.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
<customCommands>
<customCommand>
<name>DECOMMISSION</name>
<commandScript>
<script>scripts/resourcemanager.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</customCommand>
</customCommands>
</component>
ResourceManager是一个MASTER组件,并且命令脚本是scripts/resourcemanager.py
,可以被找到services/YARN/package目录,
PYTHON命令脚本,脚本作为PYTHON方法实现default lifecycle commands。这是默认的安装方法安装命令:
class Resourcemanager(Script):
def install(self, env):
self.install_packages(env)
self.configure(env)
你也可以看到一个自定义的命令定义DECOMMISSION,这意味着还有一个DECOMMISSION方法在python命令脚本:
def decommission(self, env):
import params
...
Execute(yarn_refresh_cmd,
user=yarn_user
)
pass
四、使用堆栈继承
栈可以扩展其他堆栈为了分享命令脚本和配置。这样可以减少重复的代码在栈使用以下:
·为子栈定义存储库
·在子栈添加新的服务(不是在父栈)
·覆盖父服务命令脚本
·覆盖父服务的配置
五、例子:实现一个自定义服务
在本例中,我们将创建一个名为“SAMPLESRV”的定制服务,将其添加到现有的栈的定义。
此服务包括MASTER,SLAVE,CLIENT组件。
创建并且添加服务
1.
Ambari Server上,浏览到/var/lib/ambari-server/resources/stacks/HDP/2.0.6/services
这个目录。在这种情况下,我们将浏览到HDP 2.0的定义。
cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services
2.创建一个目录/var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/
SAMPLESRV将包含SAMPLESRV的服务定义。
mkdir /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV
cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV
3.浏览到新创建的SAMPLESRV
目录,创建一个metainfo.xml文件去描述一个新的服务。例如:
<?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> <!-- note: use osType rather than osFamily for Ambari 1.5.0 and 1.5.1 -->
</osSpecific>
</osSpecifics>
</service>
</services>
</metainfo>
4.
在上面,我的服务名是
"SAMPLESRV"它包含:
·一个MASTER组件“SAMPLESRV_MASTER”
·一个SLAVE组件“SAMPLESRV_SLAVE”
·一个CLIENT组件“SAMPLESRV_CLIENT”
5.接下来,我们创建命令脚本。为命令脚本创建一个目录/var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts我们制定的服务metainfo
1 mkdir -p /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts
2 cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts
6.
进入目录并创建.py的命令脚本文件。
例如master.py文件:
import sys
from resource_management import *
class Master(Script):
def install(self, env):
print 'Install 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';
def configure(self, env):
print 'Configure the Sample Srv Master';
if __name__ == "__main__":
Master().execute()
例如slave.py文件
:
import sys
from resource_management import *
class Slave(Script):
def install(self, env):
print 'Install 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';
def configure(self, env):
print 'Configure the Sample Srv Slave';
if __name__ == "__main__":
Slave().execute()
例如sample_client.py文件
:
import 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()
7.
重启Ambari Server
ambari-server restart
参考网址:
https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=38571133
https:///Symantec/ambari-cassandra-service