当前位置:   article > 正文

Ambari 常用的 REST API 介绍_ambari rest api

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"
      }
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

等同于

[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"
      }
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

查询集群主机信息

[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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

例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"
}
  • 1
  • 2
  • 3
  • 4
  • 5

[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" : [ ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142

停止服务

[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"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这里写图片描述
删除服务

[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
  • 1
  • 2

这里写图片描述
执行完成后,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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

例 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"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

小结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/683632
推荐阅读
相关标签
  

闽ICP备14008679号