FAIRYFAR-INTERNAL
 
  FAIRYFAR-INTERNAL  |  SITEMAP  |  ABOUT-ME  |  HOME  
您的足迹: Ambari自定义服务
Ambari自定义服务

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

一、自定义服务实例

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

创建与添加服务

  • 在Ambari服务器上,cd到 /var/lib/ambari-server/resources/stacks/HDP/3.1/services 目录。
snippet.bash
cd /var/lib/ambari-server/resources/stacks/HDP/3.1/services
  • 创建目录 /var/lib/ambari-server/resources/stacks/HDP/3.1/services/SAMPLESRV,这样就包含了 SAMPLESRV 定义。
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
  • cd到新建的 SAMPLESRV 目录,创建 metainfo.xml 文件用于描述新加的服务。例如:
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>
  • 以上创建的服务名称为 "SAMPLESRV",其包含:
    • 一个 MASTER 组件 "SAMPLESRV_MASTER"
    • 一个 SLAVE 组件 "SAMPLESRV_SLAVE"
    • 一个 CLIENT 组件 "SAMPLESRV_CLIENT"
  • 接下来,创建脚本命令。创建一个脚本命令 /var/lib/ambari-server/resources/stacks/HDP/3.1/services/SAMPLESR/package/scripts。
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
  • cd到创建的脚本命令准备新建 .py 脚本文件。

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()
  • 重启 Ambari 服务准备安装新服务。
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"。

二、参考



打赏作者以资鼓励: