Support Questions
Find answers, ask questions, and share your expertise
Announcements
Check out our newest addition to the community, the Cloudera Innovation Accelerator group hub.

REST API to stop/start a Host Component running on multiple hosts

Explorer

Hi Experts,

How can I stop/start a host component running on any of the hosts by single command using REST API?

7 REPLIES 7

Super Mentor

@samarth srivastava

.

Example: STOP All Components on host "erie2.example.com"

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X PUT -d '{"RequestInfo":{"context":"Stop All Host Components","operation_level": {"level":"HOST","cluster_name":"ErieCluster","host_names":"erie2.example.com"},"query":"HostRoles/component_name.in(DATANODE, JOURNALNODE, LOGSEARCH_LOGFEEDER, NAMENODE,NODEMANAGER, RESOURCEMANAGER, SPARK_THRIFTSERVER, ZKFC, ZOOKEEPER_SERVER)"}, "Body": {"HostRoles": {"state":"INSTALLED"}}}' http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie2.example.com/host_components

.

Example Start all components on host "erie2.example.com"

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X PUT -d '{"RequestInfo":{"context":"Start All Host Components","operation_level": {"level":"HOST","cluster_name":"ErieCluster","host_names":"erie2.example.com"},"query":"HostRoles/component_name.in(DATANODE, JOURNALNODE, LOGSEARCH_LOGFEEDER, NAMENODE,NODEMANAGER, RESOURCEMANAGER, SPARK_THRIFTSERVER, ZKFC, ZOOKEEPER_SERVER)"}, "Body": {"HostRoles": {"state":"STARTED"}}}' http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie2.example.com/host_components

Here "http://erie1.example.com:8080" is ambari Host & port.

Cluster name is: ErieCluster

erie2.example.com is the host on which we want to stop/start all the host components.

.

Explorer

Thanks, my requirement is little different. I dont know which are all host the host components run so cant give a specific hostname.

http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie2.example.com/host_component

Is there any REST API available to do so in just one shot other than finding out all hosts for a given host component and run the above for all those hosts in loop ?

Super Mentor

@samarth srivastava

If you want to know which component is installed on which host then you can use the "/services/${SERVICE_NAME}/components/${COMPONENT_NAME}" APIs to find out which all host has that component installed in it.

For example if you want to know which all hosts has DataNode then you can do the following:

# curl -i -u admin:admin -H "X-Requested-By: ambari" -X GET <a href="http://erie1.example.com:8080/api/v1/clusters/ErieCluster/services/HDFS/components/DATANODE">http://erie1.example.com:8080/api/v1/clusters/ErieCluster/services/HDFS/components/DATANODE</a>

Example Output:

# curl -i -u admin:admin -H "X-Requested-By: ambari" -X GET <a href="http://erie1.example.com:8080/api/v1/clusters/ErieCluster/services/HDFS/components/DATANODE">http://erie1.example.com:8080/api/v1/clusters/ErieCluster/services/HDFS/components/DATANODE</a>


HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Set-Cookie: AMBARISESSIONID=1qhzyiqhh934k81ih6t90niwb;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
User: admin
Content-Type: text/plain
Vary: Accept-Encoding, User-Agent
Content-Length: 1697
Server: Jetty(8.1.19.v20160209)

{
  "href" : "http://erie1.example.com:8080/api/v1/clusters/ErieCluster/services/HDFS/components/DATANODE",
  "ServiceComponentInfo" : {
    "category" : "SLAVE",
    "cluster_name" : "ErieCluster",
    "component_name" : "DATANODE",
    "display_name" : "DataNode",
    "init_count" : 0,
    "install_failed_count" : 0,
    "installed_count" : 0,
    "recovery_enabled" : "false",
    "service_name" : "HDFS",
    "started_count" : 4,
    "state" : "STARTED",
    "total_count" : 4,
    "unknown_count" : 0
  },
  "host_components" : [
    {
      "href" : "http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie1.example.com/host_components/DATANODE",
      "HostRoles" : {
        "cluster_name" : "ErieCluster",
        "component_name" : "DATANODE",
        "host_name" : "erie1.example.com"
      }
    },
    {
      "href" : "http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie2.example.com/host_components/DATANODE",
      "HostRoles" : {
        "cluster_name" : "ErieCluster",
        "component_name" : "DATANODE",
        "host_name" : "erie2.example.com"
      }
    },
    {
      "href" : "http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie3.example.com/host_components/DATANODE",
      "HostRoles" : {
        "cluster_name" : "ErieCluster",
        "component_name" : "DATANODE",
        "host_name" : "erie3.example.com"
      }
    },
    {
      "href" : "http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie4.example.com/host_components/DATANODE",
      "HostRoles" : {
        "cluster_name" : "ErieCluster",
        "component_name" : "DATANODE",
        "host_name" : "erie4.example.com"
      }
    }
  ]
}
<strong></strong>

.

Super Mentor

@samarth srivastava

You can get the list of Hosts using the following API call:

# curl -iv -u admin:admin -H "X-Requested-By: ambari" -X GET http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts

Then you can use the Wildcard character '*' to start all the host components (even when you do not know the name of the components installed on that host)

- Stopping All host components:

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X PUT -d '{"RequestInfo":{"context":"Stopping All Host Components","operation_level": {"level":"HOST","cluster_name":"ErieCluster", "host_names":"erie2example.com"},"query":"HostRoles/component_name/*"},  "Body": {"HostRoles": {"state":"INSTALLED"}}}' http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie2.example.com/host_components

Notice: the use of wildcard character "query":"HostRoles/component_name/*"

.

- Starting All host components:

curl -iv -u admin:admin -H "X-Requested-By: ambari" -X PUT -d '{"RequestInfo":{"context":"Starting All Host Components","operation_level": {"level":"HOST","cluster_name":"ErieCluster", "host_names":"erie2example.com"},"query":"HostRoles/component_name/*"},  "Body": {"HostRoles": {"state":"STARTED"}}}' http://erie1.example.com:8080/api/v1/clusters/ErieCluster/hosts/erie2.example.com/host_components

Super Collaborator

Props goes to @Nate

POST api/v1/clusters/<YOUR-CLUSTER-NAME>/requests

{
  "RequestInfo": {
    "command": "RESTART",
    "context": "Restart all ZK on the selected hosts",
    "operation_level": {
      "level": "HOST",
      "cluster_name": "YOUR-CLUSTER-NAME"
    }
  },
  "Requests/resource_filters": [
    {
      "service_name": "ZOOKEEPER",
      "component_name": "ZOOKEEPER_CLIENT",
      "hosts_predicate": "HostRoles/component_name=ZOOKEEPER_CLIENT"
    }
  ]
}

Contributor

@samarth srivastava I created custom scripts using below service based approach to stop the components on all the hosts service wise. This is do not fit your requirement, though it will be helpful to know this too.

STEP1: Get components related to a service: HBASE is example

curl -s -u admin:$PASSWORD -H "X-Requested-By: ambari" -X GET http://$AMBARI_SERVER_HOST:8080/api/v1/clusters/$CLUSTER_NAME/services/HBASE|grep component_name "component_name" : "HBASE_CLIENT", "component_name" : "HBASE_MASTER", "component_name" : "HBASE_REGIONSERVER", "component_name" : "PHOENIX_QUERY_SERVER",

STEP2: Stop the components (in all hosts)

for COMP in HBASE_CLIENT HBASE_MASTER HBASE_REGIONSERVER PHOENIX_QUERY_SERVER;do echo $COMP; curl -u admin:$PASSWORD -H "X-Requested-By: ambari" -X PUT -d '{"RequestInfo":{"context":"Stop All Components"},"Body":{"ServiceComponentInfo":{"state":"INSTALLED"}}}' http://$AMBARI_SERVER_HOST:8080/api/v1/clusters/$CLUSTER_NAME/services/HBASE/components/$COMP; done

@samarth srivastava

Just in case if you're looking for the API to restart required services after some configuration chnages, You can use a GET call to find out the components to be restarted.

http://${AMBARI_HOST}:8080/api/v1/clusters/${CLUSTER_NAME}/host_components?HostRoles/stale_configs=t...			

And if a list of components to be restarted is returned, you can invoke a POST request with a request body as below:

http://${AMBARI_HOST}:8080/api/v1/clusters/${CLUSTER_NAME}/requests 

Request body:

{
	"RequestInfo": {
		"command": "RESTART",
		"context": "Restart all required services",
		"operation_level": "host_component"
	},
	"Requests/resource_filters": [{
		"hosts_predicate": "HostRoles/stale_configs=true"
	}]
}

Note: This API works with Ambari version 2.4+ only.