基于ambari-2.7.4.0和HDP3.1版本。

一、自定义服务实例

本案例创建一个名为 "SAMPLESRV"的服务,以添加到一个已存在的Stack定义。服务包括 MASTER,SLAVE以及CLIENT 组件。

创建与添加服务

snippet.bash
cd /var/lib/ambari-server/resources/stacks/HDP/3.1/services
snippet.bash
mkdir /var/lib/ambari-server/resources/stacks/HDP/3.1/services/SAMPLESRV
cd /var/lib/ambari-server/resources/stacks/HDP/3.1/services/SAMPLESRV
snippet.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>
snippet.bash
mkdir -p /var/lib/ambari-server/resources/stacks/HDP/3.1/services/TESTSRV/package/scripts
cd /var/lib/ambari-server/resources/stacks/HDP/3.1/services/TESTSRV/package/scripts

master.py:

snippet.python
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:

snippet.python
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:

snippet.python
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()
snippet.bash
ambari-server restart

安装服务(通过Ambari 网页的"Add Services")

注:Ambari 1.7.0之后的版本才有网页添加自定义服务功能

  1. 在 Ambari 页面,导航到 Services 单击 Actions 按钮。
  2. 在 "Add Services" 向导页面。可以看到新增了 "New Sample Service" 可选项。
  3. 选中 "New Sample Service" 继续。
  4. 指定 "Sample Srv Master" 并继续。
  5. 选择需要安装 "Sample Srv Client" 的服务器,并继续。
  6. 一旦安装完成,"New Sample Service" 将添加到 Service 导航区。
  7. 如果想把 "Sample Srv Client" 添加到服务器,可以导航至 Hosts,并指定服务器后单击 "+ Add"。

二、参考