Ambari 常用的 REST API 介绍

Ambari 借鉴了很多成熟分布式软件的 API 设计。
Rest API 就是一个很好地体现。通过 Ambari 的 Rest API,可以在脚本中通过 curl 维护整个集群。
并且,我们可以用 Rest API 实现一些无法在 Ambari GUI 上面做的操作。下面是一些实例。

查询关于集群信息

[root@hadron ~]# curl -u admin:admin http://192.168.1.25:8080/api/v1/clusters
{
  "href" : "http://192.168.1.25:8080/api/v1/clusters",
  "items" : [
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc",
      "Clusters" : {
        "cluster_name" : "cc",
        "version" : "HDP-2.5"
      }
    }
  ]
}

等同于

[root@hadron ~]#curl -H "X-Requested-By: ambari" -X GET -u admin:admin http://192.168.1.25:8080/api/v1/clusters
{
  "href" : "http://192.168.1.25:8080/api/v1/clusters",
  "items" : [
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc",
      "Clusters" : {
        "cluster_name" : "cc",
        "version" : "HDP-2.5"
      }
    }
  ]
}

查询集群主机信息

[root@hadron ~]# curl -u admin:admin http://192.168.1.25:8080/api/v1/hosts
{
  "href" : "http://192.168.1.25:8080/api/v1/hosts",
  "items" : [
    {
      "href" : "http://192.168.1.25:8080/api/v1/hosts/anode1",
      "Hosts" : {
        "cluster_name" : "cc",
        "host_name" : "anode1"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/hosts/anode2",
      "Hosts" : {
        "cluster_name" : "cc",
        "host_name" : "anode2"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/hosts/anode3",
      "Hosts" : {
        "cluster_name" : "cc",
        "host_name" : "anode3"
      }
    }
  ]
}

例1,通过 API 卸载已安装的 Service

目前 Ambari 不支持在 GUI 上面卸载已安装的 Service。
所以当一个 Service 不再需要的时候,用户没法删除掉该 Service。
幸运的是 Ambari 提供了 DELETE 的 Rest API,我们可以通过该 API 来删除 Ambari 中 Service。不过这里需要注意,这个方法只是从 Ambari Service 中删除了 Service。这样一来,Ambari 的 GUI 界面中不再显示这个 Service。但是 Service 本身还安装在 Agent 所在的机器。如果用户需要彻底的清除掉这个 Service,仍需要手工的到每个机器卸载(例如,在每个机器执行 yum erase)。
这里我以删除 Storm 为例。卸载之前,需要确认是否停掉了该 Service。
我们通过 GET 方法来得到这个结果(这里当然也可以直接从 GUI 上面看到 Service 状态)。
具体的命令如下:
[root@hadron ~]# curl -u admin:admin -H “X-Requested-By: ambari” -X GET http://192.168.1.25:8080/api/v1/clusters/cc/services/STORM

[root@hadron ~]# curl -u admin:admin -H "X-Requested-By: ambari" -X GET http://192.168.1.25:8080/api/v1/clusters/cc/services/STORM
{
  "status" : 404,
  "message" : "The requested resource doesn't exist: Service not found, clusterName=cc, serviceName=STORM"
}

[root@hadron ~]# curl -u admin:admin -H “X-Requested-By: ambari” -X GET http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE

[root@hadron ~]# curl -u admin:admin -H "X-Requested-By: ambari" -X GET http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE
{
  "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE",
  "ServiceInfo" : {
    "cluster_name" : "cc",
    "maintenance_state" : "OFF",
    "service_name" : "HBASE",
    "state" : "STARTED"
  },
  "alerts_summary" : {
    "CRITICAL" : 0,
    "MAINTENANCE" : 0,
    "OK" : 8,
    "UNKNOWN" : 0,
    "WARNING" : 0
  },
  "alerts" : [
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/63",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 63,
        "definition_name" : "hbase_regionserver_process_percent",
        "host_name" : null,
        "id" : 63,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/64",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 64,
        "definition_name" : "hbase_regionserver_process",
        "host_name" : "anode1",
        "id" : 64,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/65",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 61,
        "definition_name" : "hbase_master_process",
        "host_name" : "anode1",
        "id" : 65,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/71",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 62,
        "definition_name" : "hbase_master_cpu",
        "host_name" : "anode1",
        "id" : 71,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/61",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 64,
        "definition_name" : "hbase_regionserver_process",
        "host_name" : "anode2",
        "id" : 61,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/60",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 64,
        "definition_name" : "hbase_regionserver_process",
        "host_name" : "anode3",
        "id" : 60,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/62",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 61,
        "definition_name" : "hbase_master_process",
        "host_name" : "anode3",
        "id" : 62,
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/alerts/70",
      "Alert" : {
        "cluster_name" : "cc",
        "definition_id" : 62,
        "definition_name" : "hbase_master_cpu",
        "host_name" : "anode3",
        "id" : 70,
        "service_name" : "HBASE"
      }
    }
  ],
  "components" : [
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/components/HBASE_CLIENT",
      "ServiceComponentInfo" : {
        "cluster_name" : "cc",
        "component_name" : "HBASE_CLIENT",
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/components/HBASE_MASTER",
      "ServiceComponentInfo" : {
        "cluster_name" : "cc",
        "component_name" : "HBASE_MASTER",
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/components/HBASE_REGIONSERVER",
      "ServiceComponentInfo" : {
        "cluster_name" : "cc",
        "component_name" : "HBASE_REGIONSERVER",
        "service_name" : "HBASE"
      }
    },
    {
      "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE/components/PHOENIX_QUERY_SERVER",
      "ServiceComponentInfo" : {
        "cluster_name" : "cc",
        "component_name" : "PHOENIX_QUERY_SERVER",
        "service_name" : "HBASE"
      }
    }
  ],
  "artifacts" : [ ]
}

停止服务

[root@hadron ~]# curl -u admin:admin -H "X-Requested-By: ambari" -X PUT -d \
> '{"RequestInfo":{"context":"Stop Service"},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}'\
>  192.168.1.25:8080/api/v1/clusters/cc/services/HBASE
{
  "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/requests/56",
  "Requests" : {
    "id" : 56,
    "status" : "Accepted"
  }
}

这里写图片描述
删除服务

[root@hadron ~]# curl -u admin:admin -H "X-Requested-By: ambari" -X \
> DELETE  http://192.168.1.25:8080/api/v1/clusters/cc/services/HBASE

这里写图片描述
执行完成后,HBASE就从Ambari的Service里面删掉了,但是HBASE的package还存在于机器
如果需要彻底清除掉HBASE的 package,则需要到各个 Agent 机器执行如下命令。
yum erase “hbase_2_5*”
执行完后,这个 Service 就被彻底的清除掉了。

例 2,获取 Service 的 Component 和 Host 列表

上个实例中,让用户登录到每个机器去执行 yum 卸载安装包,其实是不太现实的。
一般我们会写一个脚本先通过 curl 调用 GET 方法,先获取到 Service 的 Component 列表,
然后再调用 GET 方法,获取 Component 的机器列表,接着调用 DELETE 从 Ambari 中删除 Service。
最后脚本通过 SSH 登录到各个 Agent 机器上执行 yum 卸载安装包。
脚本示例代码如下(该脚本只能在 Ambari Server 上执行,
因为 Ambari Server 有无密码登录所有 Agent 机器的权限)。

#!/bin/sh
GetHostList()
{
 curl -u admin:admin -H "X-Requested-By: ambari" -X GET
 http://$AMBARI_HOST:8080/api/v1/clusters/$CLUSTER/services/$SERVICE/components/$1
 2>/dev/null |grep host_name|awk -F: '{print $2}'|sed 's/"//g' >> temp_host_list
}

GetServiceComponent()
{
 curl -u admin:admin -H "X-Requested-By: ambari" -X GET
 http://$AMBARI_HOST:8080/api/v1/clusters/$CLUSTER/services/$SERVICE
 2>/dev/null | grep "component_name" > ./temp_component_list
 sed -i 's/"//g' ./temp_component_list
 sed -i 's/,//g' ./temp_component_list
}


if [ $# != 4 ]; then
 echo "Usage: $0 Ambari_Server Cluster_Name Service_Name Package_Name"
 exit 1
fi

AMBARI_HOST=$1
CLUSTER=$2
SERVICE=$3
PACKAGE=$4

GetServiceComponent

cat ./temp_component_list|while read line
do
 COMPONENT=`echo $line|awk -F: '{print $2}'`
 GetHostList $COMPONENT
done

curl -u admin:admin -H "X-Requested-By: ambari" -X DELETE
http://$AMBARI_HOST:8080/api/v1/clusters/$CLUSTER/services/$SERVICE

rm -f ./temp_component_list >/dev/null 2>&1
#delete duplicated lines (duplicated host name)

hosts=`cat temp_host_list|sort |uniq`
for host in $hosts
do
 ssh $host "yum erase $PACKAGE"
done

rm -f temp_host_list >/dev/null 2>&1

例 3,通过 API 执行 Service 的命令

这里,我们以调用 API 执行 Service Check 为例。
首先需要知道命令的名字,这里每个 Service 的 Check 命令也是不同的。
不过 Service Check 是 build-in 的命令,所以有一定的格式可循。
格式大致如下:
NAME_SERVICE_CHCECK
只要将 NAME 替换成对应的 Service,就是该 Service 的 Check 命令。以 YARN 为例,执行如下的命令。
curl -u admin:admin -H “X-Requested-By: ambari” -X POST -d ’
{“RequestInfo”:{“context”:”My YARN Service Check”, “command”:
“YARN_SERVICE_CHECK”},”Requests/resource_filters”:[{“service_name”:”YARN”}]}’
http://zwshen86:8080/api/v1/clusters/bigdata/requests

[root@hadron ~]# curl -u admin:admin -H "X-Requested-By: ambari" -X POST -d \
> '{"RequestInfo":{"context":"My YARN Service Check", "command":"YARN_SERVICE_CHECK"},"Requests/resource_filters":[{"service_name":"YARN"}]}' \
> http://192.168.1.25:8080/api/v1/clusters/cc/requests
{
  "href" : "http://192.168.1.25:8080/api/v1/clusters/cc/requests/57",
  "Requests" : {
    "id" : 57,
    "status" : "Accepted"
  }
}

这里写图片描述
执行完后,可以发现在 WEB GUI 上面,就多了一个正在进行的 Operation

小结

通过这三个简单实例,就可以体会到 Ambari Rest API 的作用。
在 Rest API 的基础上,就算脱离了 WEB,我们也可以很好地控制 Ambari。
当然,我们也不得不记住很多生涩的参数。
因此,大多情况下,只有当 Ambari 的 GUI 不足以完成需求,或者不期望暴露在 GUI 上面的时候,
就可以使用 Rest API。有兴趣的读者可以搜索下 Ambari Server 目录所有的 Python 脚本,
其实 Ambari 自身很多地方都在用 curl 调用 Rest API。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值