2020-05-11 21:15:59 +00:00
---
layout: docs
2022-09-23 21:08:05 +00:00
page_title: WAN Federation Through Mesh Gateways - VMs and Kubernetes
2020-05-11 21:15:59 +00:00
description: >-
2022-10-11 17:25:19 +00:00
WAN federation through mesh gateways extends service mesh deployments by enabling Consul on Kubernetes to securely communicate with instances on VMs. Learn how to configure multi-cluster federation with k8s as either the primary or secondary datacenter.
2020-05-11 21:15:59 +00:00
---
2022-09-23 21:08:05 +00:00
# WAN Federation Between VMs and Kubernetes Through Mesh Gateways
2020-05-11 21:15:59 +00:00
-> **1.8.0+:** This feature is available in Consul versions 1.8.0 and higher
2023-01-25 16:52:43 +00:00
~> This topic requires familiarity with [Mesh Gateways](/consul/docs/connect/gateways/mesh-gateway/service-to-service-traffic-wan-datacenters) and [WAN Federation Via Mesh Gateways](/consul/docs/connect/gateways/mesh-gateway/wan-federation-via-mesh-gateways).
2020-05-11 21:15:59 +00:00
2023-07-26 18:42:56 +00:00
This page describes how to federate Consul clusters separately deployed in VM and Kubernetes runtimes. Refer to [Multi-Cluster Overview](/consul/docs/k8s/deployment-configurations/multi-cluster)
for more information, including [Kubernetes networking requirements](/consul/docs/k8s/deployment-configurations/multi-cluster#network-requirements).
2020-05-11 21:15:59 +00:00
Consul datacenters running on non-kubernetes platforms like VMs or bare metal can
2023-07-26 18:42:56 +00:00
be federated with Kubernetes datacenters.
2020-05-11 21:15:59 +00:00
## Kubernetes as the Primary
2023-07-26 18:42:56 +00:00
One Consul datacenter must be the [primary](/consul/docs/k8s/deployment-configurations/multi-cluster/kubernetes#primary-datacenter). If your primary datacenter is running on Kubernetes, use the Helm config from the [Primary Datacenter](/consul/docs/k8s/deployment-configurations/multi-cluster/kubernetes#primary-datacenter) section to install Consul.
2020-05-11 21:15:59 +00:00
2023-01-25 16:52:43 +00:00
Once installed on Kubernetes, and with the `ProxyDefaults` [resource created](/consul/docs/k8s/deployment-configurations/multi-cluster/kubernetes#proxydefaults),
2021-02-17 19:01:52 +00:00
you'll need to export the following information from the primary Kubernetes cluster:
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
- Certificate authority cert and key (in order to create SSL certs for VMs)
- External addresses of Kubernetes mesh gateways
- Replication ACL token
- Gossip encryption key
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
The following sections detail how to export this data.
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
### Certificates
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
1. Retrieve the certificate authority cert:
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
```sh
2022-05-11 17:11:45 +00:00
kubectl get secrets/consul-ca-cert --namespace consul --template='{{index .data "tls.crt" | base64decode }}' > consul-agent-ca.pem
2021-05-13 17:20:12 +00:00
```
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
1. And the certificate authority signing key:
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
```sh
2022-05-11 17:11:45 +00:00
kubectl get secrets/consul-ca-key --namespace consul --template='{{index .data "tls.key" | base64decode }}' > consul-agent-ca-key.pem
2021-05-13 17:20:12 +00:00
```
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
1. With the `consul-agent-ca.pem` and `consul-agent-ca-key.pem` files you can
create certificates for your servers and clients running on VMs that share the
same certificate authority as your Kubernetes servers.
You can use the `consul tls` commands to generate those certificates:
```sh
# NOTE: consul-agent-ca.pem and consul-agent-ca-key.pem must be in the current
# directory.
$ consul tls cert create -server -dc=vm-dc -node <node_name>
==> WARNING: Server Certificates grants authority to become a
server and access all state in the cluster including root keys
and all ACL tokens. Do not distribute them to production hosts
that are not server nodes. Store them as securely as CA keys.
==> Using consul-agent-ca.pem and consul-agent-ca-key.pem
==> Saved vm-dc-server-consul-0.pem
==> Saved vm-dc-server-consul-0-key.pem
```
2021-03-01 22:39:46 +00:00
2023-01-25 16:52:43 +00:00
-> Note the `-node` option in the above command. This should be same as the node name of the [Consul Agent](/consul/docs/agent#running-an-agent). This is a [requirement](/consul/docs/connect/gateways/mesh-gateway/wan-federation-via-mesh-gateways#tls) for Consul Federation to work. Alternatively, if you plan to use the same certificate and key pair on all your Consul server nodes, or you don't know the nodename in advance, use `-node "*"` instead.
2021-05-13 17:20:12 +00:00
Not satisfying this requirement would result in the following error in the Consul Server logs:
`[ERROR] agent.server.rpc: TLS handshake failed: conn=from= error="remote error: tls: bad certificate"`
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
See the help for output of `consul tls cert create -h` to see more options
for generating server certificates.
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
1. These certificates can be used in your server config file:
2020-05-11 21:15:59 +00:00
2021-07-31 01:37:33 +00:00
<CodeBlockConfig filename="server.hcl">
2021-05-13 17:20:12 +00:00
```hcl
2022-03-18 10:46:58 +00:00
tls {
defaults {
cert_file = "vm-dc-server-consul-0.pem"
key_file = "vm-dc-server-consul-0-key.pem"
ca_file = "consul-agent-ca.pem"
}
}
2021-05-13 17:20:12 +00:00
```
2020-05-11 21:15:59 +00:00
2021-07-31 01:37:33 +00:00
</CodeBlockConfig>
2021-05-13 17:20:12 +00:00
1. For clients, you can generate TLS certs with:
```shell-session
$ consul tls cert create -client
==> Using consul-agent-ca.pem and consul-agent-ca-key.pem
==> Saved dc1-client-consul-0.pem
==> Saved dc1-client-consul-0-key.pem
```
2020-05-11 21:15:59 +00:00
2023-01-25 16:52:43 +00:00
Or use the [auto_encrypt](/consul/docs/agent/config/config-files#auto_encrypt) feature.
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
### Mesh Gateway Addresses
Retrieve the WAN addresses of the mesh gateways:
2020-05-11 21:15:59 +00:00
2020-05-19 18:32:38 +00:00
```shell-session
2022-05-11 17:11:45 +00:00
$ kubectl exec statefulset/consul-server --namespace consul -- sh -c \
2022-01-12 23:05:01 +00:00
'curl --silent --insecure https://localhost:8501/v1/catalog/service/mesh-gateway | jq ".[].ServiceTaggedAddresses.wan"'
2020-05-11 21:15:59 +00:00
{
"Address": "1.2.3.4",
"Port": 443
}
{
"Address": "1.2.3.4",
"Port": 443
}
```
In this example, the addresses are the same because both mesh gateway pods are
fronted by the same Kubernetes load balancer.
These addresses will be used in the server config for the `primary_gateways`
setting:
```hcl
primary_gateways = ["1.2.3.4:443"]
```
2021-05-13 17:20:12 +00:00
### Replication ACL Token
If ACLs are enabled, you'll also need the replication ACL token:
2020-05-11 21:15:59 +00:00
2020-05-19 18:32:38 +00:00
```shell-session
2022-05-11 17:11:45 +00:00
$ kubectl get secrets/consul-acl-replication-acl-token --namespace consul --template='{{.data.token | base64decode}}'
2020-05-11 21:15:59 +00:00
e7924dd1-dc3f-f644-da54-81a73ba0a178
```
This token will be used in the server config for the replication token.
```hcl
acls {
tokens {
replication = "e7924dd1-dc3f-f644-da54-81a73ba0a178"
}
}
```
2021-05-13 17:20:12 +00:00
-> **NOTE:** You'll also need to set up additional ACL tokens as needed by the
2023-01-25 16:52:43 +00:00
ACL system. See tutorial [Secure Consul with Access Control Lists (ACLs)](/consul/tutorials/security/access-control-setup-production#apply-individual-tokens-to-agents)
2021-05-13 17:20:12 +00:00
for more information.
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
### Gossip Encryption Key
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
If gossip encryption is enabled, you'll need the key as well. The command
to retrieve the key will depend on which Kubernetes secret you've stored it in.
This key will be used in server and client configs for the `encrypt` setting:
```hcl
encrypt = "uF+GsbI66cuWU21kiXLze5JLEX5j4iDFlDTb0ZWNpDI="
```
### Final Configuration
2020-05-11 21:15:59 +00:00
A final example server config file might look like:
```hcl
# From above
2022-03-18 10:46:58 +00:00
tls {
defaults {
cert_file = "vm-dc-server-consul-0.pem"
key_file = "vm-dc-server-consul-0-key.pem"
ca_file = "consul-agent-ca.pem"
}
internal_rpc {
verify_incoming = true
verify_outgoing = true
verify_server_hostname = true
}
}
2020-05-11 21:15:59 +00:00
primary_gateways = ["1.2.3.4:443"]
acl {
enabled = true
default_policy = "deny"
down_policy = "extend-cache"
tokens {
agent = "e7924dd1-dc3f-f644-da54-81a73ba0a178"
replication = "e7924dd1-dc3f-f644-da54-81a73ba0a178"
}
}
encrypt = "uF+GsbI66cuWU21kiXLze5JLEX5j4iDFlDTb0ZWNpDI="
# Other server settings
server = true
datacenter = "vm-dc"
data_dir = "/opt/consul"
enable_central_service_config = true
primary_datacenter = "dc1"
connect {
enabled = true
enable_mesh_gateway_wan_federation = true
}
ports {
https = 8501
http = -1
grpc = 8502
}
```
## Kubernetes as the Secondary
If you're running your primary datacenter on VMs then you'll need to manually
2023-01-25 16:52:43 +00:00
construct the [Federation Secret](/consul/docs/k8s/deployment-configurations/multi-cluster/kubernetes#federation-secret) in order to federate
2023-07-26 18:42:56 +00:00
Kubernetes clusters as secondaries. In addition, primary clusters must be able to make requests to the Kubernetes API URLs of secondary clusters when ACLs are enabled.
2020-05-11 21:15:59 +00:00
-> Your VM cluster must be running mesh gateways, and have mesh gateway WAN
2023-01-25 16:52:43 +00:00
federation enabled. See [WAN Federation via Mesh Gateways](/consul/docs/connect/gateways/mesh-gateway/wan-federation-via-mesh-gateways).
2020-05-11 21:15:59 +00:00
You'll need:
1. The root certificate authority cert placed in `consul-agent-ca.pem`.
1. The root certificate authority key placed in `consul-agent-ca-key.pem`.
1. The IP addresses of the mesh gateways running in your VM datacenter. These must
2020-05-19 18:32:38 +00:00
be routable from the Kubernetes cluster.
2020-05-11 21:15:59 +00:00
1. If ACLs are enabled you must create an ACL replication token with the following rules:
2021-05-13 17:20:12 +00:00
```hcl
acl = "write"
operator = "write"
agent_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "write"
}
service_prefix "" {
policy = "read"
intentions = "read"
}
```
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
This token is used for ACL replication and for automatic ACL management in Kubernetes.
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
If you're running Consul Enterprise you'll need the rules:
2020-05-11 21:15:59 +00:00
2021-05-13 17:20:12 +00:00
```hcl
operator = "write"
agent_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "write"
}
namespace_prefix "" {
2021-09-07 14:53:57 +00:00
acl = "write"
2021-05-13 17:20:12 +00:00
service_prefix "" {
policy = "read"
intentions = "read"
}
}
```
2022-11-09 18:26:10 +00:00
1. If ACLs are enabled you'll also need to modify the [anonymous token](/consul/docs/security/acl/acl-tokens#anonymous-token) policy to have the following permissions:
```hcl
node_prefix "" {
policy = "read"
}
service_prefix "" {
policy = "read"
}
```
With Consul Enterprise, use:
2022-11-18 17:33:02 +00:00
2022-11-09 18:26:10 +00:00
```hcl
partition_prefix "" {
namespace_prefix "" {
node_prefix "" {
policy = "read"
}
service_prefix "" {
policy = "read"
}
}
}
```
2022-11-18 17:33:02 +00:00
2022-11-09 18:26:10 +00:00
These permissions are needed to allow cross-datacenter requests. To make a cross-dc request the sidecar proxy in the originating DC needs to know about the
services running in the remote DC. To do so, it needs an ACL token that allows it to look up the services in the remote DC. The way tokens are created in
Kubernetes, the sidecar proxies have local ACL tokens– i.e tokens that are only valid in the local DC. When a request goes from one DC to another, if the
request has a local token, it is stripped from the request because the remote DC won't be able to validate it. When the request lands in the other DC,
it has no ACL token and so will be subject to the anonymous token policy. This is why the anonymous token policy must be configured to allow read access
to all services. When the Kubernetes DC is the primary, this is handled automatically, but when the primary DC is on VMs, this must be configured manually.
2022-11-18 17:33:02 +00:00
2022-11-09 18:26:10 +00:00
To configure the anonymous token policy, first create a policy with the above rules, then attach it to the anonymous token. For example using the CLI:
2022-11-18 17:33:02 +00:00
2022-11-09 18:26:10 +00:00
```sh
echo 'node_prefix "" {
policy = "read"
}
service_prefix "" {
policy = "read"
}' | consul acl policy create -name anonymous -rules -
2022-11-18 17:33:02 +00:00
2022-11-09 18:26:10 +00:00
consul acl token update -id 00000000-0000-0000-0000-000000000002 -policy-name anonymous
```
2020-05-11 21:15:59 +00:00
1. If gossip encryption is enabled, you'll need the key.
With that data ready, you can create the Kubernetes federation secret:
```sh
kubectl create secret generic consul-federation \
--from-literal=caCert=$(cat consul-agent-ca.pem) \
--from-literal=caKey=$(cat consul-agent-ca-key.pem)
# If ACLs are enabled uncomment.
# --from-literal=replicationToken="<your acl replication token>" \
# If using gossip encryption uncomment.
# --from-literal=gossipEncryptionKey="<your gossip encryption key>"
```
2022-05-11 17:11:45 +00:00
If ACLs are enabled, you must next determine the Kubernetes API URL for the secondary cluster. The API URL of the
must be specified in the config files for all secondary clusters because secondary clusters need
to create global Consul ACL tokens (tokens that are valid in all datacenters) and these tokens can only be created
2023-01-25 16:52:43 +00:00
by the primary datacenter. By setting the API URL, the secondary cluster will configure a [Consul auth method](/consul/docs/security/acl/auth-methods)
2022-05-11 17:11:45 +00:00
in the primary cluster so that components in the secondary cluster can use their Kubernetes ServiceAccount tokens
to retrieve global Consul ACL tokens from the primary.
To determine the Kubernetes API URL, first get the cluster name in your kubeconfig:
```shell-session
$ export CLUSTER=$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"$(kubectl config current-context)\")].context.cluster}")
```
Then get the API URL:
```shell-session
$ kubectl config view -o jsonpath="{.clusters[?(@.name == \"$CLUSTER\")].cluster.server}"
https://<some-url>
```
You'll use this URL when setting `global.federation.k8sAuthMethodHost`.
2020-05-11 21:15:59 +00:00
Then use the following Helm config file:
```yaml
global:
name: consul
datacenter: dc2
tls:
enabled: true
caCert:
secretName: consul-federation
secretKey: caCert
caKey:
secretName: consul-federation
secretKey: caKey
# Delete this acls section if ACLs are disabled.
acls:
manageSystemACLs: true
replicationToken:
secretName: consul-federation
secretKey: replicationToken
federation:
enabled: true
2022-05-11 17:11:45 +00:00
k8sAuthMethodHost: <kubernetes-api-url>
primaryDatacenter: dc1
2020-05-11 21:15:59 +00:00
# Delete this gossipEncryption section if gossip encryption is disabled.
gossipEncryption:
secretName: consul-federation
secretKey: gossipEncryptionKey
connectInject:
enabled: true
meshGateway:
enabled: true
server:
extraConfig: |
{
"primary_gateways": ["<ip of your VM mesh gateway>", "<other ip>", ...]
}
```
2022-05-11 17:11:45 +00:00
Notes:
1. You must fill out the `server.extraConfig` section with the IPs of your mesh
2020-05-11 21:15:59 +00:00
gateways running on VMs.
2022-05-11 17:11:45 +00:00
1. Set `global.federation.k8sAuthMethodHost` to the Kubernetes API URL of this cluster (including `https://`).
1. `global.federation.primaryDatacenter` should be set to the name of your primary datacenter.
2020-05-11 21:15:59 +00:00
2023-01-25 16:52:43 +00:00
With your config file ready to go, follow our [Installation Guide](/consul/docs/k8s/installation/install)
2020-05-11 21:15:59 +00:00
to install Consul on your secondary cluster(s).
2021-02-17 19:01:52 +00:00
After installation, if you're using consul-helm 0.30.0+, [create the
2023-01-25 16:52:43 +00:00
`ProxyDefaults` resource](/consul/docs/k8s/deployment-configurations/multi-cluster/kubernetes#proxydefaults)
2021-02-17 19:01:52 +00:00
to allow traffic between datacenters.
2021-05-13 17:20:12 +00:00
## Next Steps
2023-01-25 16:52:43 +00:00
In both cases (Kubernetes as primary or secondary), after installation, follow the [Verifying Federation](/consul/docs/k8s/deployment-configurations/multi-cluster/kubernetes#verifying-federation)
2020-05-11 21:15:59 +00:00
section to verify that federation is working as expected.