2019-07-08 23:40:57 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
page_title: Connect - Mesh Gateways
2020-04-13 18:40:26 +00:00
sidebar_title: Mesh Gateways
2020-04-07 18:55:19 +00:00
description: >-
A Mesh Gateway enables better routing of a Connect service's data to upstreams
in other datacenters. This section details how to use Envoy and describes how
you can plug in a gateway of your choice.
2019-07-08 23:40:57 +00:00
---
2019-08-21 21:23:08 +00:00
# Mesh Gateways
2019-07-08 23:40:57 +00:00
2020-04-09 00:09:01 +00:00
-> **1.6.0+:** This feature is available in Consul versions 1.6.0 and newer.
2019-07-08 23:40:57 +00:00
Mesh gateways enable routing of Connect traffic between different Consul datacenters. Those datacenters
can reside in different clouds or runtime environments where general interconnectivity between all services
in all datacenters isn't feasible. These gateways operate by sniffing the SNI header out of the Connect session
and then route the connection to the appropriate destination based on the server name requested. The data
2020-07-24 21:07:36 +00:00
within the mTLS session is not decrypted by the Gateway.
2019-07-08 23:40:57 +00:00
2020-04-07 23:56:08 +00:00
![Mesh Gateway Architecture](/img/mesh-gateways.png)
2019-07-08 23:40:57 +00:00
2019-10-14 15:40:35 +00:00
For a complete example of how to connect services across datacenters,
2020-08-13 21:02:44 +00:00
review the [mesh gateway tutorial](https://learn.hashicorp.com/tutorials/consul/service-mesh-gateways).
2019-10-14 15:40:35 +00:00
2019-07-08 23:40:57 +00:00
## Prerequisites
Each mesh gateway needs three things:
1. A local Consul agent to manage its configuration.
2. General network connectivity to all services within its local Consul datacenter.
3. General network connectivity to all mesh gateways within remote Consul datacenters.
2019-07-17 18:02:58 +00:00
Mesh gateways also require that your Consul datacenters are configured correctly:
- You'll need to use Consul version 1.6.0.
2020-04-09 23:46:54 +00:00
- Consul [Connect](/docs/agent/options#connect) must be enabled in both datacenters.
- Each of your [datacenters](/docs/agent/options#datacenter) must have a unique name.
2020-08-13 21:02:44 +00:00
- Your datacenters must be [WAN joined](https://learn.hashicorp.com/tutorials/consul/federarion-gossip-wan).
2020-04-09 23:46:54 +00:00
- The [primary datacenter](/docs/agent/options#primary_datacenter) must be set to the same value in both datacenters. This specifies which datacenter is the authority for Connect certificates and is required for services in all datacenters to establish mutual TLS with each other.
- [gRPC](/docs/agent/options#grpc_port) must be enabled.
2020-05-28 19:19:17 +00:00
- If you want to [enable gateways globally](/docs/connect/mesh-gateway#enabling-gateways-globally) you must enable [centralized configuration](/docs/agent/options#enable_central_service_config).
2019-09-27 03:06:58 +00:00
Currently, Envoy is the only proxy with mesh gateway capabilities in Consul.
- Mesh gateway proxies receive their configuration through Consul, which
2020-04-06 20:27:35 +00:00
automatically generates it based on the proxy's registration. Currently Consul
can only translate mesh gateway registration information into Envoy
configuration, therefore the proxies acting as mesh gateways must be Envoy.
2019-09-27 03:06:58 +00:00
- Sidecar proxies that send traffic to an upstream service through a gateway
2020-04-06 20:27:35 +00:00
need to know the location of that gateway. They discover the gateway based on
their sidecar proxy registrations. Consul can only translate the gateway
registration information into Envoy configuration, so any sidecars that send
upstream traffic through a gateway must be Envoy.
2019-09-27 03:06:58 +00:00
Sidecar proxies that don't send upstream traffic through a gateway aren't
affected when you deploy gateways. If you are using Consul's built-in proxy as a
Connect sidecar it will continue to work for intra-datacenter traffic and will
receive incoming traffic even if that traffic has passed through a gateway.
2019-07-17 18:02:58 +00:00
2019-07-08 23:40:57 +00:00
## Modes of Operation
Each upstream of a Connect proxy can be configured to be routed through a mesh gateway. Depending on
your network, the proxy's connection to the gateway can happen in one of the following modes:
2020-04-06 20:27:35 +00:00
- `local` - In this mode the Connect proxy makes its outbound connection to a gateway running in the
2019-07-08 23:40:57 +00:00
same datacenter. That gateway is then responsible for ensuring the data gets forwarded along to
gateways in the destination datacenter. This is the mode of operation depicted in the diagram at
the beginning of the page.
2020-04-06 20:27:35 +00:00
- `remote` - In this mode the Connect proxy makes its outbound connection to a gateway running in the
2019-07-08 23:40:57 +00:00
destination datacenter. That gateway will then forward the data to the final destination service.
2020-04-06 20:27:35 +00:00
- `none` - In this mode, no gateway is used and a Connect proxy makes its outbound connections directly
2019-07-08 23:40:57 +00:00
to the destination services.
## Mesh Gateway Configuration
2020-05-13 21:29:40 +00:00
Mesh gateways are defined similarly to other services registered with Consul, with two exceptions.
The first is that the [service kind](/api/agent/service#kind) must be "mesh-gateway". Second,
the mesh gateway service definition may contain a `Proxy.Config` entry just like a
Connect proxy service, to define opaque configuration parameters useful for the actual proxy software.
For Envoy there are some supported [gateway options](/docs/connect/proxies/envoy#gateway-options) as well as
[escape-hatch overrides](/docs/connect/proxies/envoy#escape-hatch-overrides).
-> **Note:** If ACLs are enabled, a token granting `service:write` for the gateways service name
and `service:read` for all services in the datacenter. These permissions authorize the token to route
communications for other Connect services but does not allow decrypting any of their communications.
2019-07-08 23:40:57 +00:00
## Connect Proxy Configuration
Configuring a Connect Proxy to use gateways is as simple as setting its mode of operation. This can be done
in several different places allowing for global to more fine grained control. If the gateway mode is configured
in multiple locations the order of precedence is as follows
1. Upstream Definition
2. Service Instance Definition
3. Centralized `service-defaults` configuration entry
4. Centralized `proxy-defaults` configuration entry.
### Enabling Gateways Globally
The following `proxy-defaults` configuration will enable gateways for all Connect services in the `local` mode.
```hcl
Kind = "proxy-defaults"
Name = "global"
MeshGateway {
Mode = "local"
}
```
### Enabling Gateways Per-Service
The following `service-defaults` configuration will enable gateways for all Connect services with the name "web".
```hcl
Kind = "service-defaults"
Name = "web"
MeshGateway {
Mode = "local"
}
```
### Enabling Gateways for a Service Instance
2020-04-09 23:46:54 +00:00
The following [Proxy Service Registration](/docs/connect/registration/service-registration)
2019-12-03 16:09:25 +00:00
definition will enable gateways for the service instance in the `remote` mode.
2019-07-08 23:40:57 +00:00
```hcl
service {
name = "web-sidecar-proxy"
2019-12-03 16:09:25 +00:00
kind = "connect-proxy"
2019-07-08 23:40:57 +00:00
port = 8181
proxy {
destination_service_name = "web"
mesh_gateway {
mode = "remote"
}
upstreams = [
{
destination_name = "api"
datacenter = "secondary"
local_bind_port = 10000
}
]
}
}
```
2019-12-03 16:09:25 +00:00
Or alternatively inline with the service definition:
2019-07-08 23:40:57 +00:00
```hcl
service {
name = "web"
port = 8181
connect {
sidecar_service {
proxy {
mesh_gateway {
mode = "remote"
}
upstreams = [
{
destination_name = "api"
datacenter = "secondary"
local_bind_port = 10000
}
]
}
}
}
}
```
### Enabling Gateways for a Proxy Upstream
The following service definition will enable gateways in the `local` mode for one upstream, the `remote` mode
for a second upstream and will disable gateways for a third upstream.
```hcl
service {
name = "web-sidecar-proxy"
2019-12-03 16:09:25 +00:00
kind = "connect-proxy"
2019-07-08 23:40:57 +00:00
port = 8181
proxy {
destination_service_name = "web"
upstreams = [
{
destination_name = "api"
local_bind_port = 10000
mesh_gateway {
mode = "remote"
}
},
{
destination_name = "db"
local_bind_port = 10001
mesh_gateway {
mode = "local"
}
},
{
destination_name = "logging"
local_bind_port = 10002
mesh_gateway {
mode = "none"
}
},
]
}
}
```