github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/api-docs/index.mdx (about)

     1  ---
     2  layout: api
     3  page_title: HTTP API
     4  sidebar_title: HTTP 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  ```shell-session
    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 `localhost:4646`. 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 five primary nouns in Nomad:
    51  
    52  - jobs
    53  - nodes
    54  - allocations
    55  - deployments
    56  - evaluations
    57  
    58  [![Nomad Data Model](/img/nomad-data-model.png)](/img/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. Jobs can also have affinities which are used to express placement
    63  preferences. Nodes are the servers in the clusters that tasks can be
    64  scheduled on. The mapping of tasks in a job to nodes is done using allocations.
    65  An allocation is used to declare that a set of tasks in a job should be run on a
    66  particular node. Scheduling is the process of determining the appropriate
    67  allocations and is done as part of an evaluation. Deployments are objects to
    68  track a rolling update of allocations between two versions of a job.
    69  
    70  The API is modeled closely on the underlying data model. Use the links to the
    71  left for documentation about specific endpoints. There are also "Agent" APIs
    72  which interact with a specific agent and not the broader cluster used for
    73  administration.
    74  
    75  ## ACLs
    76  
    77  Several endpoints in Nomad use or require ACL tokens to operate. The token are used to authenticate the request and determine if the request is allowed based on the associated authorizations. Tokens are specified per-request by using the `X-Nomad-Token` request header set to the `SecretID` of an ACL Token.
    78  
    79  For more details about ACLs, please see the [ACL Guide](https://learn.hashicorp.com/nomad?track=acls#operations-and-development).
    80  
    81  ## Authentication
    82  
    83  When ACLs are enabled, a Nomad token should be provided to API requests using the `X-Nomad-Token` header. When using authentication, clients should communicate via TLS.
    84  
    85  Here is an example using curl:
    86  
    87  ```shell-session
    88  $ curl \
    89      --header "X-Nomad-Token: aa534e09-6a07-0a45-2295-a7f77063d429" \
    90      https://localhost:4646/v1/jobs
    91  ```
    92  
    93  ## Blocking Queries
    94  
    95  Many endpoints in Nomad support a feature known as "blocking queries". A
    96  blocking query is used to wait for a potential change using long polling. Not
    97  all endpoints support blocking, but each endpoint uniquely documents its support
    98  for blocking queries in the documentation.
    99  
   100  Endpoints that support blocking queries return an HTTP header named
   101  `X-Nomad-Index`. This is a unique identifier representing the current state of
   102  the requested resource. On a new Nomad cluster the value of this index starts at 1.
   103  
   104  On subsequent requests for this resource, the client can set the `index` query
   105  string parameter to the value of `X-Nomad-Index`, indicating that the client
   106  wishes to wait for any changes subsequent to that index.
   107  
   108  When this is provided, the HTTP request will "hang" until a change in the system
   109  occurs, or the maximum timeout is reached. A critical note is that the return of
   110  a blocking request is **no guarantee** of a change. It is possible that the
   111  timeout was reached or that there was an idempotent write that does not affect
   112  the result of the query.
   113  
   114  In addition to `index`, endpoints that support blocking will also honor a `wait`
   115  parameter specifying a maximum duration for the blocking request. This is
   116  limited to 10 minutes. If not set, the wait time defaults to 5 minutes. This
   117  value can be specified in the form of "10s" or "5m" (i.e., 10 seconds or 5
   118  minutes, respectively). A small random amount of additional wait time is added
   119  to the supplied maximum `wait` time to spread out the wake up time of any
   120  concurrent requests. This adds up to `wait / 16` additional time to the maximum
   121  duration.
   122  
   123  ## Consistency Modes
   124  
   125  Most of the read query endpoints support multiple levels of consistency. Since
   126  no policy will suit all clients' needs, these consistency modes allow the user
   127  to have the ultimate say in how to balance the trade-offs inherent in a
   128  distributed system.
   129  
   130  The two read modes are:
   131  
   132  - `default` - If not specified, the default is strongly consistent in almost all
   133    cases. However, there is a small window in which a new leader may be elected
   134    during which the old leader may service stale values. The trade-off is fast
   135    reads but potentially stale values. The condition resulting in stale reads is
   136    hard to trigger, and most clients should not need to worry about this case.
   137    Also, note that this race condition only applies to reads, not writes.
   138  
   139  - `stale` - This mode allows any server to service the read regardless of
   140    whether it is the leader. This means reads can be arbitrarily stale; however,
   141    results are generally consistent to within 50 milliseconds of the leader. The
   142    trade-off is very fast and scalable reads with a higher likelihood of stale
   143    values. Since this mode allows reads without a leader, a cluster that is
   144    unavailable will still be able to respond to queries.
   145  
   146  To switch these modes, use the `stale` query parameter on requests.
   147  
   148  To support bounding the acceptable staleness of data, responses provide the
   149  `X-Nomad-LastContact` header containing the time in milliseconds that a server
   150  was last contacted by the leader node. The `X-Nomad-KnownLeader` header also
   151  indicates if there is a known leader. These can be used by clients to gauge the
   152  staleness of a result and take appropriate action.
   153  
   154  ## Cross-Region Requests
   155  
   156  By default, any request to the HTTP API will default to the region on which the
   157  machine is servicing the request. If the agent runs in "region1", the request
   158  will query the region "region1". A target region can be explicitly request using
   159  the `?region` query parameter. The request will be transparently forwarded and
   160  serviced by a server in the requested region.
   161  
   162  ## Compressed Responses
   163  
   164  The HTTP API will gzip the response if the HTTP request denotes that the client
   165  accepts gzip compression. This is achieved by passing the accept encoding:
   166  
   167  ```shell-session
   168  $ curl \
   169      --header "Accept-Encoding: gzip" \
   170      https://localhost:4646/v1/...
   171  ```
   172  
   173  ## Formatted JSON Output
   174  
   175  By default, the output of all HTTP API requests is minimized JSON. If the client
   176  passes `pretty` on the query string, formatted JSON will be returned.
   177  
   178  In general, clients should prefer a client-side parser like `jq` instead of
   179  server-formatted data. Asking the server to format the data takes away
   180  processing cycles from more important tasks.
   181  
   182  ```shell-session
   183  $ curl https://localhost:4646/v1/page?pretty
   184  ```
   185  
   186  ## HTTP Methods
   187  
   188  Nomad's API aims to be RESTful, although there are some exceptions. The API
   189  responds to the standard HTTP verbs GET, PUT, and DELETE. Each API method will
   190  clearly document the verb(s) it responds to and the generated response. The same
   191  path with different verbs may trigger different behavior. For example:
   192  
   193  ```text
   194  PUT /v1/jobs
   195  GET /v1/jobs
   196  ```
   197  
   198  Even though these share a path, the `PUT` operation creates a new job whereas
   199  the `GET` operation reads all jobs.
   200  
   201  ## HTTP Response Codes
   202  
   203  Individual API's will contain further documentation in the case that more
   204  specific response codes are returned but all clients should handle the following:
   205  
   206  - 200 and 204 as success codes.
   207  - 400 indicates a validation failure and if a parameter is modified in the
   208    request, it could potentially succeed.
   209  - 403 marks that the client isn't authenticated for the request.
   210  - 404 indicates an unknown resource.
   211  - 5xx means that the client should not expect the request to succeed if retried.