Ambari实现一个自定义的服务

来源:互联网 发布:手机mac地址怎么查看 编辑:程序博客网 时间:2024/04/28 22:07

在这个例子中,我们将创建一个称为“SAMPLESRV”的自定义的服务,将它添加到现有源里面。这服务包括MASTER, SLAVE和CLIENT 组件。

创建和添加服务
  1. 在Ambari服务器,进入/var/lib/ambari-server/resources/stacks/HDP/2.0.6/services目录。在这种情况下,我们将浏览到HDP-2源的。

    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里面配置。

    mkdir -p /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts
    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服务器

    ambari-server restart

安装服务
Icon

注意:通过web界面添加自定义服务是在1.7.0极以后版本上

  1. 在Ambari Web, 进入到服务界面并点击 操作 按钮(在左边的服务导航区域)。
  2. 打开"添加服务" 安装向导。 你可以看到包含"My Sample Service" 的选项(就是在服务的 metainfo.xml 文件里面定义的名称)。
  3. 选择 "My Sample Service" 并点击下一步。
  4. 分配  "Sample Srv Master" 并点击下一步。
  5. 选择主机安装 "Sample Srv Client" 并点击下一步。
  6. 一旦完成,该"My Sample Service" 将出现在服务的导航区域.
  7. 如果你想添加"Sample Srv Client"到任一主机,你可以进入到"主机"并进入到需要的主机,点击“+添加”。

0 0
原创粉丝点击