github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/gateway.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: gateway Stanza - Job Specification
     4  sidebar_title: gateway
     5  description: |-
     6    The "gateway" stanza allows specifying options for configuring Consul Gateways
     7    used in the Consul Connect integration
     8  ---
     9  
    10  # `gateway` Stanza
    11  
    12  <Placement groups={['job', 'group', 'service', 'connect', 'gateway']} />
    13  
    14  The `gateway` stanza allows configuration of [Consul Connect Gateways](https://www.consul.io/docs/connect/gateways). Nomad will
    15  automatically create the necessary Gateway [Configuration Entry](https://www.consul.io/docs/agent/config-entries)
    16  as well as inject an Envoy proxy task into the Nomad job to serve as the Gateway.
    17  
    18  The `gateway` configuration is valid within the context of a `connect` stanza.
    19  Additional information about Gateway configurations can be found in Consul's
    20  [Connect Gateways](https://www.consul.io/docs/connect/gateways) documentation.
    21  
    22  ~> **Note:** [Ingress Gateways](https://www.consul.io/docs/connect/gateways/ingress-gateway)
    23  are generally intended for enabling access into a Consul service mesh from within the
    24  same network. For public ingress products like [NGINX](https://learn.hashicorp.com/tutorials/nomad/load-balancing-nginx?in=nomad/load-balancing)
    25  provide more suitable features.
    26  
    27  ```hcl
    28  service {
    29    connect {
    30      gateway {
    31        # ...
    32      }
    33    }
    34  }
    35  ```
    36  
    37  ## `gateway` Parameters
    38  
    39  Exactly one of `ingress` or `terminating` must be configured.
    40  
    41  - `proxy` <code>([proxy]: nil)</code> - Configuration of the Envoy proxy that will
    42    be injected into the task group.
    43  - `ingress` <code>([ingress]: nil)</code> - Configuration Entry of type `ingress-gateway`
    44    that will be associated with the service.
    45  - `terminating` <code>([terminating]: nil)</code> - Configuration Entry of type `terminating-gateway`
    46    that will be associated with the service.
    47  
    48  ### `proxy` Parameters
    49  
    50  - `connect_timeout` `(string: "5s")` - The amount of time to allow when making upstream
    51    connections before timing out. Defaults to 5 seconds. If the upstream service has
    52    the configuration option <code>[connect_timeout_ms]</code> set for the `service-resolver`, that
    53    timeout value will take precedence over this gateway proxy option.
    54  - `envoy_gateway_bind_tagged_addresses` `(bool: false)` - Indicates that the gateway
    55    services tagged addresses should be bound to listeners in addition to the default
    56    listener address.
    57  - `envoy_gateway_bind_addresses` <code>(map<string|[address]>: nil)</code> - A map of additional addresses to be bound.
    58    The keys to this map are the same of the listeners to be created and the values are
    59    a map with two keys - address and port, that combined make the address to bind the
    60    listener to. These are bound in addition to the default address.
    61    If `bridge` networking is in use, this map is automatically populated with additional
    62    listeners enabling the Envoy proxy to work from inside the network namespace.
    63  
    64  ```
    65  envoy_gateway_bind_addresses "<service>" {
    66    address = "0.0.0.0"
    67    port    = <port>
    68  }
    69  ```
    70  
    71  - `envoy_gateway_no_default_bind` `(bool: false)` - Prevents binding to the default
    72    address of the gateway service. This should be used with one of the other options
    73    to configure the gateway's bind addresses. If `bridge` networking is in use, this
    74    value will default to `true` since the Envoy proxy does not need to bind to the
    75    service address from inside the network namespace.
    76  - `envoy_dns_discovery_type` `(string: optional)` - Determintes how Envoy will
    77    resolve hostnames. Defaults to `LOGICAL_DNS`. Must be one of `STRICT_DNS` or
    78    `LOGICAL_DNS`. Details for each type are available in the [Envoy Documentation](https://www.envoyproxy.io/docs/envoy/v1.16.1/intro/arch_overview/upstream/service_discovery).
    79    This option applies to terminating gateways that route to services addressed by a
    80    hostname.
    81  - `config` `(map: nil)` - Escape hatch for [Advanced Configuration] of Envoy.
    82  
    83  #### `address` Parameters
    84  
    85  - `address` `(string: required)` - The address to bind to when combined with `port`.
    86  - `port` `(int: required)` - The port to listen to.
    87  
    88  ### `ingress` Parameters
    89  
    90  - `tls` <code>([tls]: nil)</code> - TLS configuration for this gateway.
    91  - `listener` <code>(array<[listener]> : required)</code> - One or more listeners that the
    92    ingress gateway should setup, uniquely identified by their port number.
    93  
    94  #### `tls` Parameters
    95  
    96  - `enabled` `(bool: false)` - Set this configuration to enable TLS for every listener
    97    on the gateway. If TLS is enabled, then each host defined in the `host` field will
    98    be added as a DNSSAN to the gateway's x509 certificate.
    99  
   100  #### `listener` Parameters
   101  
   102  - `port` `(int: required)` - The port that the listener should receive traffic on.
   103  - `protocol` `(string: "tcp")` - The protocol associated with the listener. Either
   104    `tcp` or `http`.
   105  
   106    ~> **Note:** If using `http`, preconfiguring a [service-default] in Consul to
   107    set the [Protocol](https://www.consul.io/docs/agent/config-entries/service-defaults#protocol)
   108    of the service to `http` is recommended.
   109  
   110  - `service` <code>(array<[service]>: required)</code> - One or more services to be
   111    exposed via this listener. For `tcp` listeners, only a single service is allowed.
   112  
   113  #### `service` Parameters
   114  
   115  - `name` `(string: required)` - The name of the service that should be exposed through
   116    this listener. This can be either a service registered in the catalog, or a
   117    service defined by other config entries, or a service that is going to be configured
   118    by Nomad. If the wildcard specifier `*` is provided, then ALL services will be
   119    exposed through this listener. This is not supported for a listener with protocol `tcp`.
   120  - `hosts` `(array<string>: nil)` - A list of hosts that specify what requests will
   121    match this service. This cannot be used with a `tcp` listener, and cannot be
   122    specified alongside a wildcard (`*`) service name. If not specified, the default
   123    domain `<service-name>.ingress.*` will be used to match services. Requests _must_
   124    send the correct host to be routed to the defined service.
   125  
   126    The wildcard specifier `*` can be used by itself to match all traffic coming to
   127    the ingress gateway, if TLS is not enabled. This allows a user to route all traffic
   128    to a single service without specifying a host, allowing simpler tests and demos.
   129    Otherwise, the wildcard specifier can be used as part of the host to match
   130    multiple hosts, but only in the leftmost DNS label. This ensures that all defined
   131    hosts are valid DNS records. For example, `*.example.com` is valid while `example.*`
   132    and `*-suffix.example.com` are not.
   133  
   134    ~> **Note:** If a well-known port is not used, i.e. a port other than 80 (http) or 443 (https),
   135    then the port must be appended to the host to correctly match traffic. This is
   136    defined in the [HTTP/1.1 RFC](https://tools.ietf.org/html/rfc2616#section-14.23).
   137    If TLS is enabled, then the host **without** the port must be added to the `hosts`
   138    field as well. TLS verification only matches against the hostname of the incoming
   139    connection, and does not take into account the port.
   140  
   141  ### `terminating` Parameters
   142  
   143  - `service` <code>(array<[linked-service]>: required)</code> - One or more services to be
   144    linked with the gateway. The gateway will proxy traffic to these services. These
   145    linked services must be registered with Consul for the gateway to discover their
   146    addresses. They must also be registered in the same Consul datacenter as the
   147    terminating gateway.
   148  
   149  #### `service` Parameters
   150  
   151  - `name` `(string: required)` - The name of the service to link with the gateway.
   152    If the wildcard specifier `*` is provided, then ALL services within the Consul
   153    namespace wil lbe linked with the gateway.
   154  
   155  - `ca_file` `(string: <optional>)` - A file path to a PEM-encoded certificate
   156    authority. The file must be accessible by the gateway task. The certificate authority
   157    is used to verify the authenticity of the service linked with the gateway. It
   158    can be provided along with a `cert_file` and `key_file` for mutual TLS
   159    authentication, or on its own for one-way TLS authentication. If none is provided
   160    the gateway **will not** encrypt traffic to the destination.
   161  - `cert_file` `(string: <optional>)` - A file path to a PEM-encoded certificate.
   162    The file must be accessible by the gateway task. The certificate is provided to servers
   163    to verify the gateway's authenticity. It must be provided if a `key_file` is provided.
   164  - `key_file` `(string: <optional>)` - A file path to a PEM-encoded private key.
   165    The file must be accessible by the gateway task. The key is used with the certificate
   166    to verify the gateway's authenticity. It must be provided if a `cert_file` is provided.
   167  - `sni` `(string: <optional>)` - An optional hostname or domain name to specify during
   168    the TLS handshake.
   169  
   170  ### Gateway with host networking
   171  
   172  Nomad supports running gateways using host networking. A static port must be allocated
   173  for use by the [Envoy admin interface](https://www.envoyproxy.io/docs/envoy/latest/operations/admin)
   174  and assigned to the proxy service definition.
   175  
   176  !> **Warning:** There is no way to disable the Envoy admin interface, which will be
   177  accessible to any workload running on the same Nomad client. The admin interface exposes
   178  information about the proxy, including a Consul Service Identity token if Consul ACLs
   179  are enabled.
   180  
   181  ### Specify Envoy image
   182  
   183  The Docker image used for Connect gateway tasks defaults to the official [Envoy
   184  Docker] image, `envoyproxy/envoy:v${NOMAD_envoy_version}`, where `${NOMAD_envoy_version}`
   185  is resolved automatically by a query to Consul. The image to use can be configured
   186  by setting `meta.connect.gateway_image` in the Nomad job. Custom images can still
   187  make use of the envoy version interpolation, e.g.
   188  
   189  ```hcl
   190  meta.connect.gateway_image = custom/envoy-${NOMAD_envoy_version}:latest
   191  ```
   192  
   193  ### Custom gateway task
   194  
   195  The task created for the gateway can be configured manually using the
   196  [`sidecar_task`][sidecar_task] stanza.
   197  
   198  ```
   199  connect {
   200    gateway {
   201      # ...
   202    }
   203  
   204    sidecar_task {
   205      # see /docs/job-specification/sidecar_task for more details
   206    }
   207  }
   208  ```
   209  
   210  ### Examples
   211  
   212  #### ingress gateway
   213  
   214  ```hcl
   215  job "ingress-demo" {
   216  
   217    datacenters = ["dc1"]
   218  
   219    # This group will have a task providing the ingress gateway automatically
   220    # created by Nomad. The ingress gateway is based on the Envoy proxy being
   221    # managed by the docker driver.
   222    group "ingress-group" {
   223  
   224      network {
   225        mode = "bridge"
   226  
   227        # This example will enable plain HTTP traffic to access the uuid-api connect
   228        # native example service on port 8080.
   229        port "inbound" {
   230          static = 8080
   231          to     = 8080
   232        }
   233      }
   234  
   235      service {
   236        name = "my-ingress-service"
   237        port = "8080"
   238  
   239        connect {
   240          gateway {
   241  
   242            # Consul gateway [envoy] proxy options.
   243            proxy {
   244              # The following options are automatically set by Nomad if not
   245              # explicitly configured when using bridge networking.
   246              #
   247              # envoy_gateway_no_default_bind = true
   248              # envoy_gateway_bind_addresses "uuid-api" {
   249              #   address = "0.0.0.0"
   250              #   port    = <associated listener.port>
   251              # }
   252              #
   253              # Additional options are documented at
   254              # https://www.nomadproject.io/docs/job-specification/gateway#proxy-parameters
   255            }
   256  
   257            # Consul Ingress Gateway Configuration Entry.
   258            ingress {
   259              # Nomad will automatically manage the Configuration Entry in Consul
   260              # given the parameters in the ingress block.
   261              #
   262              # Additional options are documented at
   263              # https://www.nomadproject.io/docs/job-specification/gateway#ingress-parameters
   264              listener {
   265                port     = 8080
   266                protocol = "tcp"
   267                service {
   268                  name = "uuid-api"
   269                }
   270              }
   271            }
   272          }
   273        }
   274      }
   275    }
   276  
   277    # The UUID generator from the connect-native demo is used as an example service.
   278    # The ingress gateway above makes access to the service possible over normal HTTP.
   279    # For example,
   280    #
   281    # $ curl $(dig +short @127.0.0.1 -p 8600 uuid-api.ingress.dc1.consul. ANY):8080
   282    group "generator" {
   283      network {
   284        mode = "host"
   285        port "api" {}
   286      }
   287  
   288      service {
   289        name = "uuid-api"
   290        port = "api"
   291  
   292        connect {
   293          native = true
   294        }
   295      }
   296  
   297      task "generate" {
   298        driver = "docker"
   299  
   300        config {
   301          image        = "hashicorpnomad/uuid-api:v5"
   302          network_mode = "host"
   303        }
   304  
   305        env {
   306          BIND = "0.0.0.0"
   307          PORT = "${NOMAD_PORT_api}"
   308        }
   309      }
   310    }
   311  }
   312  ```
   313  
   314  #### terminating gateway
   315  
   316  ```hcl
   317  job "countdash-terminating" {
   318  
   319    datacenters = ["dc1"]
   320  
   321    # This group provides the service that exists outside of the Consul Connect
   322    # service mesh. It is using host networking and listening to a statically
   323    # allocated port.
   324    group "api" {
   325      network {
   326        mode = "host"
   327        port "port" {
   328          static = "9001"
   329        }
   330      }
   331  
   332      # This example will enable services in the service mesh to make requests
   333      # to this service which is not in the service mesh by making requests
   334      # through the terminating gateway.
   335      service {
   336        name = "count-api"
   337        port = "port"
   338      }
   339  
   340      task "api" {
   341        driver = "docker"
   342  
   343        config {
   344          image        = "hashicorpnomad/counter-api:v3"
   345          network_mode = "host"
   346        }
   347      }
   348    }
   349  
   350    group "gateway" {
   351      network {
   352        mode = "bridge"
   353      }
   354  
   355      service {
   356        name = "api-gateway"
   357  
   358        connect {
   359          gateway {
   360            # Consul gateway [envoy] proxy options.
   361            proxy {
   362              # The following options are automatically set by Nomad if not explicitly
   363              # configured with using bridge networking.
   364              #
   365              # envoy_gateway_no_default_bind = true
   366              # envoy_gateway_bind_addresses "default" {
   367              #   address = "0.0.0.0"
   368              #   port    = <generated listener port>
   369              # }
   370              # Additional options are documented at
   371              # https://www.nomadproject.io/docs/job-specification/gateway#proxy-parameters
   372            }
   373  
   374            # Consul Terminating Gateway Configuration Entry.
   375            terminating {
   376              # Nomad will automatically manage the Configuration Entry in Consul
   377              # given the parameters in the terminating block.
   378              #
   379              # Additional options are documented at
   380              # https://www.nomadproject.io/docs/job-specification/gateway#terminating-parameters
   381              service {
   382                name = "count-api"
   383              }
   384            }
   385          }
   386        }
   387      }
   388    }
   389  
   390    # The dashboard service is in the service mesh, making use of bridge network
   391    # mode and connect.sidecar_service. When running, the dashboard should be
   392    # available from a web browser at localhost:9002.
   393    group "dashboard" {
   394      network {
   395        mode = "bridge"
   396  
   397        port "http" {
   398          static = 9002
   399          to     = 9002
   400        }
   401      }
   402  
   403      service {
   404        name = "count-dashboard"
   405        port = "9002"
   406  
   407        connect {
   408          sidecar_service {
   409            proxy {
   410              upstreams {
   411                # By configuring an upstream destination to the linked service of
   412                # the terminating gateway, the dashboard is able to make requests
   413                # through the gateway to the count-api service.
   414                destination_name = "count-api"
   415                local_bind_port  = 8080
   416              }
   417            }
   418          }
   419        }
   420      }
   421  
   422      task "dashboard" {
   423        driver = "docker"
   424  
   425        env {
   426          COUNTING_SERVICE_URL = "http://${NOMAD_UPSTREAM_ADDR_count_api}"
   427        }
   428  
   429        config {
   430          image = "hashicorpnomad/counter-dashboard:v3"
   431        }
   432      }
   433    }
   434  }
   435  ```
   436  
   437  [address]: /docs/job-specification/gateway#address-parameters
   438  [advanced configuration]: https://www.consul.io/docs/connect/proxies/envoy#advanced-configuration
   439  [connect_timeout_ms]: https://www.consul.io/docs/agent/config-entries/service-resolver#connecttimeout
   440  [envoy docker]: https://hub.docker.com/r/envoyproxy/envoy/tags
   441  [ingress]: /docs/job-specification/gateway#ingress-parameters
   442  [proxy]: /docs/job-specification/gateway#proxy-parameters
   443  [linked-service]: /docs/job-specification/gateway#service-parameters-1
   444  [listener]: /docs/job-specification/gateway#listener-parameters
   445  [service]: /docs/job-specification/gateway#service-parameters
   446  [service-default]: https://www.consul.io/docs/agent/config-entries/service-defaults
   447  [sidecar_task]: /docs/job-specification/sidecar_task
   448  [terminating]: /docs/job-specification/gateway#terminating-parameters
   449  [tls]: /docs/job-specification/gateway#tls-parameters
   450