github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/website/source/api/index.html.md (about) 1 --- 2 layout: api 3 page_title: HTTP API 4 sidebar_current: api-overview 5 description: |- 6 Nomad exposes a RESTful HTTP API to control almost every aspect of the 7 Nomad agent. 8 --- 9 10 # HTTP API 11 12 The main interface to Nomad is a RESTful HTTP API. The API can query the current 13 state of the system as well as modify the state of the system. The Nomad CLI 14 actually invokes Nomad's HTTP for many commands. 15 16 ## Version Prefix 17 18 All API routes are prefixed with `/v1/`. 19 20 This documentation is only for the v1 API. 21 22 ~> **Backwards compatibility:** At the current version, Nomad does not yet 23 promise backwards compatibility even with the v1 prefix. We'll remove this 24 warning when this policy changes. We expect to reach API stability by Nomad 25 1.0. 26 27 ## Addressing & Ports 28 29 Nomad binds to a specific set of addresses and ports. The HTTP API is served via 30 the `http` address and port. This `address:port` must be accessible locally. If 31 you bind to `127.0.0.1:4646`, the API is only available _from that host_. If you 32 bind to a private internal IP, the API will be available from within that 33 network. If you bind to a public IP, the API will be available from the public 34 Internet (not recommended). 35 36 The default port for the Nomad HTTP API is `4646`. This can be overridden via 37 the Nomad configuration block. Here is an example curl request to query a Nomad 38 server with the default configuration: 39 40 ```text 41 $ curl http://127.0.0.1:4646/v1/agent/members 42 ``` 43 44 The conventions used in the API documentation do not list a port and use the 45 standard URL `nomad.rocks`. Be sure to replace this with your Nomad agent URL 46 when using the examples. 47 48 ## Data Model and Layout 49 50 There are four primary nouns in Nomad: 51 52 - jobs 53 - nodes 54 - allocations 55 - deployments 56 - evaluations 57 58 [](/assets/images/nomad-data-model.png) 59 60 Jobs are submitted by users and represent a _desired state_. A job is a 61 declarative description of tasks to run which are bounded by constraints and 62 require resources. Nodes are the servers in the clusters that tasks can be 63 scheduled on. The mapping of tasks in a job to nodes is done using allocations. 64 An allocation is used to declare that a set of tasks in a job should be run on a 65 particular node. Scheduling is the process of determining the appropriate 66 allocations and is done as part of an evaluation. Deployments are objects to 67 track a rolling update of allocations between two versions of a job. 68 69 The API is modeled closely on the underlying data model. Use the links to the 70 left for documentation about specific endpoints. There are also "Agent" APIs 71 which interact with a specific agent and not the broader cluster used for 72 administration. 73 74 ## ACLs 75 76 The Nomad API does not support ACLs at this time. 77 78 ## Authentication 79 80 The Nomad API does not support authentication at this time. 81 82 ## Blocking Queries 83 84 Many endpoints in Nomad support a feature known as "blocking queries". A 85 blocking query is used to wait for a potential change using long polling. Not 86 all endpoints support blocking, but each endpoint uniquely documents its support 87 for blocking queries in the documentation. 88 89 Endpoints that support blocking queries return an HTTP header named 90 `X-Nomad-Index`. This is a unique identifier representing the current state of 91 the requested resource. 92 93 On subsequent requests for this resource, the client can set the `index` query 94 string parameter to the value of `X-Nomad-Index`, indicating that the client 95 wishes to wait for any changes subsequent to that index. 96 97 When this is provided, the HTTP request will "hang" until a change in the system 98 occurs, or the maximum timeout is reached. A critical note is that the return of 99 a blocking request is **no guarantee** of a change. It is possible that the 100 timeout was reached or that there was an idempotent write that does not affect 101 the result of the query. 102 103 In addition to `index`, endpoints that support blocking will also honor a `wait` 104 parameter specifying a maximum duration for the blocking request. This is 105 limited to 10 minutes. If not set, the wait time defaults to 5 minutes. This 106 value can be specified in the form of "10s" or "5m" (i.e., 10 seconds or 5 107 minutes, respectively). A small random amount of additional wait time is added 108 to the supplied maximum `wait` time to spread out the wake up time of any 109 concurrent requests. This adds up to `wait / 16` additional time to the maximum 110 duration. 111 112 ## Consistency Modes 113 114 Most of the read query endpoints support multiple levels of consistency. Since 115 no policy will suit all clients' needs, these consistency modes allow the user 116 to have the ultimate say in how to balance the trade-offs inherent in a 117 distributed system. 118 119 The two read modes are: 120 121 - `default` - If not specified, the default is strongly consistent in almost all 122 cases. However, there is a small window in which a new leader may be elected 123 during which the old leader may service stale values. The trade-off is fast 124 reads but potentially stale values. The condition resulting in stale reads is 125 hard to trigger, and most clients should not need to worry about this case. 126 Also, note that this race condition only applies to reads, not writes. 127 128 - `stale` - This mode allows any server to service the read regardless of 129 whether it is the leader. This means reads can be arbitrarily stale; however, 130 results are generally consistent to within 50 milliseconds of the leader. The 131 trade-off is very fast and scalable reads with a higher likelihood of stale 132 values. Since this mode allows reads without a leader, a cluster that is 133 unavailable will still be able to respond to queries. 134 135 To switch these modes, use the `stale` query parameter on requests. 136 137 To support bounding the acceptable staleness of data, responses provide the 138 `X-Nomad-LastContact` header containing the time in milliseconds that a server 139 was last contacted by the leader node. The `X-Nomad-KnownLeader` header also 140 indicates if there is a known leader. These can be used by clients to gauge the 141 staleness of a result and take appropriate action. 142 143 ## Cross-Region Requests 144 145 By default, any request to the HTTP API will default to the region on which the 146 machine is servicing the request. If the agent runs in "region1", the request 147 will query the region "region1". A target region can be explicitly request using 148 the `?region` query parameter. The request will be transparently forwarded and 149 serviced by a server in the requested region. 150 151 ## Compressed Responses 152 153 The HTTP API will gzip the response if the HTTP request denotes that the client 154 accepts gzip compression. This is achieved by passing the accept encoding: 155 156 ``` 157 $ curl \ 158 --header "Accept-Encoding: gzip" \ 159 https://nomad.rocks/v1/... 160 ``` 161 162 ## Formatted JSON Output 163 164 By default, the output of all HTTP API requests is minimized JSON. If the client 165 passes `pretty` on the query string, formatted JSON will be returned. 166 167 In general, clients should prefer a client-side parser like `jq` instead of 168 server-formatted data. Asking the server to format the data takes away 169 processing cycles from more important tasks. 170 171 ``` 172 $ curl https://nomad.rocks/v1/page?pretty 173 ``` 174 175 ## HTTP Methods 176 177 Nomad's API aims to be RESTful, although there are some exceptions. The API 178 responds to the standard HTTP verbs GET, PUT, and DELETE. Each API method will 179 clearly document the verb(s) it responds to and the generated response. The same 180 path with different verbs may trigger different behavior. For example: 181 182 ```text 183 PUT /v1/jobs 184 GET /v1/jobs 185 ``` 186 187 Even though these share a path, the `PUT` operation creates a new job whereas 188 the `GET` operation reads all jobs.