基于ambari-2.7.4.0和HDP3.1版本。 # 一、自定义服务实例 本案例创建一个名为 "SAMPLESRV"的服务,以添加到一个已存在的Stack定义。服务包括 MASTER,SLAVE以及CLIENT 组件。 ## 创建与添加服务 - 在Ambari服务器上,cd到 /var/lib/ambari-server/resources/stacks/HDP/3.1/services 目录。 ```bash cd /var/lib/ambari-server/resources/stacks/HDP/3.1/services ``` - 创建目录 /var/lib/ambari-server/resources/stacks/HDP/3.1/services/SAMPLESRV,这样就包含了 SAMPLESRV 定义。 ```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 文件用于描述新加的服务。例如: ```xml 2.0 SAMPLESRV New Sample Service A New Sample Service 1.0.0 SAMPLESRV_MASTER Sample Srv Master MASTER 1 PYTHON 600 SAMPLESRV_SLAVE Sample Srv Slave SLAVE 1+ PYTHON 600 SAMPLESRV_CLIENT Sample Srv Client CLIENT 1+ PYTHON 600 any ``` - 以上创建的服务名称为 "SAMPLESRV",其包含: - 一个 MASTER 组件 "SAMPLESRV\_MASTER" - 一个 SLAVE 组件 "SAMPLESRV\_SLAVE" - 一个 CLIENT 组件 "SAMPLESRV\_CLIENT" - 接下来,创建脚本命令。创建一个脚本命令 /var/lib/ambari-server/resources/stacks/HDP/3.1/services/SAMPLESR/package/scripts。 ```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: ```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: ```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: ```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 服务准备安装新服务。 ```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"。 # 二、参考 - [AMBARI-Overview](https://cwiki.apache.org/confluence/display/AMBARI/Overview) - [Ambari Greenplum database plugin](https://github.com/bhuvnesh2703/ambari-gpdb-plugin/) - [zData Ambari Extensions](https://github.com/zdata-inc/ambari-extensions)