github.com/clly/consul@v1.4.5/website/source/docs/connect/platform/nomad.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Connect - Nomad"
     4  sidebar_current: "docs-connect-platform-nomad"
     5  description: |-
     6    Connect can be used with [Nomad](https://www.nomadproject.io) to provide secure service-to-service communication between Nomad jobs. The ability to use the dynamic port feature of Nomad makes Connect particularly easy to use.
     7  ---
     8  
     9  # Connect on Nomad
    10  
    11  Connect can be used with [Nomad](https://www.nomadproject.io) to provide
    12  secure service-to-service communication between Nomad jobs and task groups. The ability to
    13  use the [dynamic port](https://www.nomadproject.io/docs/job-specification/network.html#dynamic-ports)
    14  feature of Nomad makes Connect particularly easy to use.
    15  
    16  Using Connect with Nomad today requires manually specifying the Connect
    17  sidecar proxy and managing intentions directly via Consul (outside of Nomad).
    18  The Consul and Nomad teams are working together towards a more automatic
    19  and unified solution in an upcoming Nomad release.
    20  
    21  ~> **Important security note:** As of Nomad 0.8.4, Nomad doesn't yet support network namespacing
    22  for tasks in a task group. As a result, running Connect with Nomad should
    23  assume the same [security checklist](/docs/connect/security.html#prevent-non-connect-traffic-to-services) as running directly on a machine without namespacing.
    24  
    25  -> **Envoy Support:** Using the [Envoy Connect integration](/docs/connect/proxies/envoy.html)
    26  with Nomad is not currently possible following this guide. This guide relies on some features
    27  of the built-in proxy that are not possible via Envoy.
    28  
    29  ## Requirements
    30  
    31  To use Connect with Nomad, the following requirements must be first be
    32  satisfied:
    33  
    34  
    35    * **Nomad 0.8.3 or later.** - The server and clients of the Nomad cluster
    36      must be running version 0.8.3 or later. This is the earliest version that
    37  	was verified to work with Connect. It is possible to work with earlier
    38  	versions but it is untested.
    39  
    40    * **Consul 1.2.0 or later.** - A Consul cluster must be setup and running with
    41      version 1.2.0 or later.
    42      Nomad must be [configured to use this Consul cluster](https://www.nomadproject.io/docs/service-discovery/index.html).
    43  
    44  ## Accepting Connect for an Existing Service
    45  
    46  The job specification below shows a job that is configured with Connect.
    47  The example uses `raw_exec` for now just to show how it can be used locally
    48  but the Docker driver or any other driver could easily be used. The example
    49  will be updated to use the official Consul Docker image following release.
    50  
    51  The example below shows a hypothetical database being configured to listen
    52  with Connect only. Explanations of the various sections follow the example.
    53  
    54  ```hcl
    55  job "db" {
    56      datacenters = ["dc1"]
    57  
    58      group "db" {
    59          task "db" {
    60              driver = "raw_exec"
    61  
    62              config {
    63                  command = "/usr/local/bin/my-database"
    64                  args    = ["-bind", "127.0.0.1:${NOMAD_PORT_tcp}"]
    65              }
    66  
    67              resources {
    68                  network {
    69                      port "tcp" {}
    70                  }
    71              }
    72          }
    73  
    74          task "connect-proxy" {
    75              driver = "raw_exec"
    76  
    77              config {
    78                  command = "/usr/local/bin/consul"
    79                  args    = [
    80                      "connect", "proxy",
    81                      "-service", "db",
    82                      "-service-addr", "${NOMAD_ADDR_db_tcp}",
    83                      "-listen", ":${NOMAD_PORT_tcp}",
    84                      "-register",
    85                  ]
    86              }
    87  
    88              resources {
    89                  network {
    90                      port "tcp" {}
    91                  }
    92              }
    93          }
    94      }
    95  }
    96  ```
    97  
    98  The job specification contains a single task group "db" with two tasks.
    99  By placing the two tasks in the same group, the Connect proxy will always
   100  be colocated directly next to the database, and has access to information
   101  such as the dynamic port it is running on.
   102  
   103  For the "db" task, there are a few important configurations:
   104  
   105    * The `-bind` address for the database is loopback only and listening on
   106      a dynamic port. This prevents non-Connect connections from outside of
   107      the node that the database is running on.
   108  
   109    * The `tcp` port is dynamic. This removes any static constraints on the port,
   110      allowing Nomad to allocate any available port for any allocation.
   111  
   112    * The database is _not_ registered with Consul using a `service` block.
   113      This isn't strictly necessary, but since we won't be connecting directly
   114      to this service, we also don't need to register it. We recommend registering
   115      the source service as well since Consul can then know the health of the
   116      target service, which is used in determining if the proxy should
   117  	receive requests.
   118  
   119  Next, the "connect-proxy" task is colocated next to the "db" task. This is
   120  using "raw_exec" executing Consul directly. In the future this example will
   121  be updated to use the official Consul Docker image.
   122  
   123  The important configuration for this proxy:
   124  
   125    * The `-service` and `-service-addr` flag specify the name of the service
   126      the proxy is representing. The address is set to the interpolation
   127      `${NOMAD_ADDR_db_tcp}` which allows the database to listen on any
   128      dynamic address and the proxy can still find it.
   129  
   130    * The `-listen` flag sets up a public listener (TLS) to accept connections
   131      on behalf of the "db" service. The port this is listening on is dynamic,
   132      since service discovery can be used to find the service ports.
   133  
   134    * The `-register` flag tells the proxy to self-register with Consul. Nomad
   135      doesn't currently know how to register Connect proxies with the `service`
   136      stanza, and this causes the proxy to register itself so it is discoverable.
   137  
   138  Following running this job specification, the DB will be started with a
   139  Connect proxy. The only public listener from the job is the proxy. This means
   140  that only Connect connections can access the database from an external node.
   141  
   142  ## Connecting to Upstream Dependencies
   143  
   144  In addition to accepting Connect-based connections, services often need
   145  to connect to upstream dependencies that are listening via Connect. For
   146  example, a "web" application may need to connect to the "db" exposed
   147  in the example above.
   148  
   149  The job specification below shows an example of this scenario:
   150  
   151  ```hcl
   152  job "web" {
   153      datacenters = ["dc1"]
   154  
   155      group "web" {
   156          task "web" {
   157              # ... typical configuration.
   158  
   159              env {
   160                  DATABASE_URL = "postgresql://${NOMAD_ADDR_proxy_tcp}/db"
   161              }
   162          }
   163  
   164          task "proxy" {
   165              driver = "raw_exec"
   166  
   167              config {
   168                  command = "/usr/local/bin/consul"
   169                  args    = [
   170                      "connect", "proxy",
   171                      "-service", "web",
   172                      "-upstream", "db:${NOMAD_PORT_tcp}",
   173                  ]
   174              }
   175  
   176              resources {
   177                  network {
   178                      port "tcp" {}
   179                  }
   180              }
   181          }
   182      }
   183  }
   184  ```
   185  
   186  Starting with the "proxy" task, the primary difference to accepting
   187  connections is that the service address, `-listen`, and `-register` flag
   188  are not specified. This prevents the proxy from registering itself as
   189  a valid listener for the given service.
   190  
   191  The `-upstream` flag is specified to configure a private listener to
   192  connect to the service "db" as "web". The port is dynamic. The listener
   193  will bind to a loopback address only.
   194  
   195  Finally, the "web" task is configured to use the localhost address to
   196  connect to the database. This will establish a connection to the remote
   197  DB using Connect. Interpolation is used to retrieve the address dynamically
   198  since the port is dynamic.
   199  
   200  -> **Both -listen and -upstream can be specified** for services that both
   201  accept Connect connections as well as have upstream dependencies. Additionally,
   202  multiple `-upstream` flags can be specified for multiple upstream dependencies. This
   203  can be done on a single proxy instance rather than having multiple.