Federation Between Kubernetes Clusters

1.8.0+: This feature is available in Consul versions 1.8.0 and higher

This topic requires familiarity with Mesh Gateways and WAN Federation Via Mesh Gateways.

Looking for a step-by-step guide? Please follow our Learn tutorial: Secure and Route Service Mesh Communication Across Kubernetes.

This page describes how to federate multiple Kubernetes clusters. See Multi-Cluster Overview for more information on use-cases and how it works.

Primary Datacenter

Consul treats each Kubernetes cluster as a separate Consul datacenter. In order to federate clusters, one cluster must be designated the primary datacenter. This datacenter will be responsible for creating the certificate authority that signs the TLS certificates Connect uses to encrypt and authorize traffic. It also handles validating global ACL tokens. All other clusters that are federated are considered secondaries.

First Time Installation

If you haven’t installed Consul on your cluster, continue reading below. If you’ve already installed Consul on a cluster and want to upgrade it to support federation, see Upgrading An Existing Cluster.

You will need to use the following config.yaml file for your primary cluster, with the possible modifications listed below.

  1. global:
  2. name: consul
  3. datacenter: dc1
  4. # TLS configures whether Consul components use TLS.
  5. tls:
  6. # TLS must be enabled for federation in Kubernetes.
  7. enabled: true
  8. federation:
  9. enabled: true
  10. # This will cause a Kubernetes secret to be created that
  11. # can be imported by secondary datacenters to configure them
  12. # for federation.
  13. createFederationSecret: true
  14. acls:
  15. manageSystemACLs: true
  16. # If ACLs are enabled, we must create a token for secondary
  17. # datacenters to replicate ACLs.
  18. createReplicationToken: true
  19. # Gossip encryption secures the protocol Consul uses to quickly
  20. # discover new nodes and detect failure.
  21. gossipEncryption:
  22. autoGenerate: true
  23. connectInject:
  24. # Consul Connect service mesh must be enabled for federation.
  25. enabled: true
  26. controller:
  27. enabled: true
  28. meshGateway:
  29. # Mesh gateways are gateways between datacenters. They must be enabled
  30. # for federation in Kubernetes since the communication between datacenters
  31. # goes through the mesh gateways.
  32. enabled: true

Federation Between Kubernetes Clusters - 图1

config.yaml

  1. global:
  2. name: consul
  3. datacenter: dc1
  4. # TLS configures whether Consul components use TLS.
  5. tls:
  6. # TLS must be enabled for federation in Kubernetes.
  7. enabled: true
  8. federation:
  9. enabled: true
  10. # This will cause a Kubernetes secret to be created that
  11. # can be imported by secondary datacenters to configure them
  12. # for federation.
  13. createFederationSecret: true
  14. acls:
  15. manageSystemACLs: true
  16. # If ACLs are enabled, we must create a token for secondary
  17. # datacenters to replicate ACLs.
  18. createReplicationToken: true
  19. # Gossip encryption secures the protocol Consul uses to quickly
  20. # discover new nodes and detect failure.
  21. gossipEncryption:
  22. autoGenerate: true
  23. connectInject:
  24. # Consul Connect service mesh must be enabled for federation.
  25. enabled: true
  26. controller:
  27. enabled: true
  28. meshGateway:
  29. # Mesh gateways are gateways between datacenters. They must be enabled
  30. # for federation in Kubernetes since the communication between datacenters
  31. # goes through the mesh gateways.
  32. enabled: true

Modifications:

  1. The Consul datacenter name is dc1. The datacenter name in each federated cluster must be unique.
  2. ACLs are enabled in the above config file. They can be disabled by setting:
  1. global:
  2. acls:
  3. manageSystemACLs: false
  4. createReplicationToken: false
  1. global:
  2. acls:
  3. manageSystemACLs: false
  4. createReplicationToken: false

ACLs secure Consul by requiring every API call to present an ACL token that is validated to ensure it has the proper permissions. If you are only testing Consul, this is not required.

  1. Gossip encryption is enabled in the above config file. To disable it, comment out or delete the gossipEncryption key:

    1. global:
    2. # gossipEncryption:
    3. # autoGenerate: true
    1. global:
    2. # gossipEncryption:
    3. # autoGenerate: true

    Gossip encryption encrypts the communication layer used to discover other nodes in the cluster and report on failure. If you are only testing Consul, this is not required.

  2. The default mesh gateway configuration creates a Kubernetes Load Balancer service. If you wish to customize the mesh gateway, for example using a Node Port service or a custom DNS entry, see the Helm reference for that setting.

With your config.yaml ready to go, follow our Installation Guide to install Consul on your primary cluster.

NOTE: You must be using consul-helm 0.21.0+. To update, run helm repo update.

Upgrading An Existing Cluster

If you have an existing cluster, you will need to upgrade it to ensure it has the following config:

  1. global:
  2. tls:
  3. enabled: true
  4. federation:
  5. enabled: true
  6. createFederationSecret: true
  7. acls:
  8. manageSystemACLs: true
  9. createReplicationToken: true
  10. meshGateway:
  11. enabled: true

Federation Between Kubernetes Clusters - 图2

config.yaml

  1. global:
  2. tls:
  3. enabled: true
  4. federation:
  5. enabled: true
  6. createFederationSecret: true
  7. acls:
  8. manageSystemACLs: true
  9. createReplicationToken: true
  10. meshGateway:
  11. enabled: true
  1. global.tls.enabled must be true. See Configuring TLS on an Existing Cluster for more information on safely upgrading a cluster to use TLS.

If you’ve set enableAutoEncrypt: true, this is also supported.

  1. global.federation.enabled must be set to true. This is a new config setting.
  2. If using ACLs, you’ll already have global.acls.manageSystemACLs: true. For the primary cluster, you’ll also need to set global.acls.createReplicationToken: true. This ensures that an ACL token is created that secondary clusters can use to authenticate with the primary.
  3. Mesh Gateways are enabled with the default configuration. The default configuration creates a Kubernetes Load Balancer service. If you wish to customize the mesh gateway, see the Helm reference for that setting.

With the above settings added to your existing config, follow the Upgrading guide to upgrade your cluster and then come back to the Federation Secret section.

NOTE: You must be using consul-helm 0.21.0+.

ProxyDefaults

If you are using consul-helm 0.30.0+ you must also create a ProxyDefaults resource to configure Consul to use the mesh gateways for service mesh traffic.

  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ProxyDefaults
  3. metadata:
  4. name: global
  5. spec:
  6. meshGateway:
  7. mode: 'local'
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ProxyDefaults
  3. metadata:
  4. name: global
  5. spec:
  6. meshGateway:
  7. mode: 'local'

The spec.meshGateway.mode can be set to local or remote. If set to local, traffic from one datacenter to another will egress through the local mesh gateway. This may be useful if you prefer all your cross-cluster network traffic to egress from the same locations. If set to remote, traffic will be routed directly from the pod to the remote mesh gateway (resulting in one less hop).

Verify that the resource was synced to Consul:

  1. $ kubectl get proxydefaults global
  2. NAME SYNCED AGE
  3. global True 1s
  1. $ kubectl get proxydefaults global
  2. NAME SYNCED AGE
  3. global True 1s

Its SYNCED status should be True.

NOTE: The ProxyDefaults resource can be created in any namespace, but we recommend creating it in the same namespace that Consul is installed in. Its name must be global.

Federation Secret

The federation secret is a Kubernetes secret containing information needed for secondary datacenters/clusters to federate with the primary. This secret is created automatically by setting:

  1. global:
  2. federation:
  3. createFederationSecret: true
  1. global:
  2. federation:
  3. createFederationSecret: true

After the installation into your primary cluster you will need to export this secret:

  1. $ kubectl get secret consul-federation --namespace consul --output yaml > consul-federation-secret.yaml
  1. $ kubectl get secret consul-federation --namespace consul --output yaml > consul-federation-secret.yaml

Security note: The federation secret makes it possible to gain full admin privileges in Consul. This secret must be kept securely, i.e. it should be deleted from your filesystem after importing it to your secondary cluster and you should use RBAC permissions to ensure only administrators can read it from Kubernetes.

Secret doesn’t exist? If you haven’t set global.name to consul then the name of the secret will be your Helm release name suffixed with -consul-federation e.g. helm-release-consul-federation. Also ensure you’re using the namespace Consul was installed into.

Now you’re ready to import the secret into your secondary cluster(s).

Switch kubectl context to your secondary Kubernetes cluster. In this example our context for our secondary cluster is dc2:

  1. $ kubectl config use-context dc2
  2. Switched to context "dc2".
  1. $ kubectl config use-context dc2
  2. Switched to context "dc2".

And import the secret:

  1. $ kubectl apply --filename consul-federation-secret.yaml
  2. secret/consul-federation configured
  1. $ kubectl apply --filename consul-federation-secret.yaml
  2. secret/consul-federation configured

Federation Secret Contents

The automatically generated federation secret contains:

  • Server certificate authority certificate - This is the certificate authority used to sign Consul server-to-server communication. This is required by secondary clusters because they must communicate with the Consul servers in the primary cluster.

  • Server certificate authority key - This is the signing key for the server certificate authority. This is required by secondary clusters because they need to create server certificates for each Consul server using the same certificate authority as the primary.

    Security note: The certificate authority key would enable an attacker to compromise Consul, it should be kept securely.

  • Consul server config - This is a JSON snippet that must be used as part of the server config for secondary datacenters. It sets:

    • primary_datacenter to the name of the primary datacenter.

    • primary_gateways to an array of IPs or hostnames for the mesh gateways in the primary datacenter. These are the addresses that Consul servers in secondary clusters will use to communicate with the primary datacenter.

      Even if there are multiple secondary datacenters, only the primary gateways need to be configured. Upon first connection with a primary datacenter, the addresses for other secondary datacenters will be discovered.

  • ACL replication token - If ACLs are enabled, secondary datacenters need an ACL token in order to authenticate with the primary datacenter. This ACL token is also used to replicate ACLs from the primary datacenter so that components in each datacenter can authenticate with one another.

  • Gossip encryption key - If gossip encryption is enabled, secondary datacenters need the gossip encryption key in order to be part of the gossip pool. Gossip is the method by which Consul discovers the addresses and health of other nodes.

    Security note: This gossip encryption key would enable an attacker to compromise Consul, it should be kept securely.

Kubernetes API URL

If ACLs are enabled, you must next determine the Kubernetes API URL for each secondary cluster. The API URL of the secondary cluster must be specified in the config files for each secondary cluster because they need to create global Consul ACL tokens (tokens that are valid in all datacenters) and these tokens can only be created by the primary datacenter. By setting the API URL, the secondary cluster will configure a Consul auth method 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 for your secondary:

  1. $ export CLUSTER=$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"$(kubectl config current-context)\")].context.cluster}")
  1. $ export CLUSTER=$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"$(kubectl config current-context)\")].context.cluster}")

Then get the API URL:

  1. $ kubectl config view -o jsonpath="{.clusters[?(@.name == \"$CLUSTER\")].cluster.server}"
  2. https://<some-url>
  1. $ kubectl config view -o jsonpath="{.clusters[?(@.name == \"$CLUSTER\")].cluster.server}"
  2. https://<some-url>

Keep track of this URL, you’ll need it in the next section.

Secondary Cluster(s)

With the primary cluster up and running, and the federation secret imported into the secondary cluster, we can now install Consul into the secondary cluster.

You will need to use the following config.yaml file for your secondary cluster(s), with the modifications listed below.

NOTE: You must use a separate Helm config file for each cluster (primary and secondaries) since their settings are different.

  1. global:
  2. name: consul
  3. datacenter: dc2
  4. tls:
  5. enabled: true
  6. # Here we're using the shared certificate authority from the primary
  7. # datacenter that was exported via the federation secret.
  8. caCert:
  9. secretName: consul-federation
  10. secretKey: caCert
  11. caKey:
  12. secretName: consul-federation
  13. secretKey: caKey
  14. acls:
  15. manageSystemACLs: true
  16. # Here we're importing the replication token that was
  17. # exported from the primary via the federation secret.
  18. replicationToken:
  19. secretName: consul-federation
  20. secretKey: replicationToken
  21. federation:
  22. enabled: true
  23. k8sAuthMethodHost: <kubernetes-api-url-of-secondary>
  24. primaryDatacenter: dc1
  25. gossipEncryption:
  26. secretName: consul-federation
  27. secretKey: gossipEncryptionKey
  28. connectInject:
  29. enabled: true
  30. controller:
  31. enabled: true
  32. meshGateway:
  33. enabled: true
  34. server:
  35. # Here we're including the server config exported from the primary
  36. # via the federation secret. This config includes the addresses of
  37. # the primary datacenter's mesh gateways so Consul can begin federation.
  38. extraVolumes:
  39. - type: secret
  40. name: consul-federation
  41. items:
  42. - key: serverConfigJSON
  43. path: config.json
  44. load: true

Federation Between Kubernetes Clusters - 图3

config-cluster2.yaml

  1. global:
  2. name: consul
  3. datacenter: dc2
  4. tls:
  5. enabled: true
  6. # Here we're using the shared certificate authority from the primary
  7. # datacenter that was exported via the federation secret.
  8. caCert:
  9. secretName: consul-federation
  10. secretKey: caCert
  11. caKey:
  12. secretName: consul-federation
  13. secretKey: caKey
  14. acls:
  15. manageSystemACLs: true
  16. # Here we're importing the replication token that was
  17. # exported from the primary via the federation secret.
  18. replicationToken:
  19. secretName: consul-federation
  20. secretKey: replicationToken
  21. federation:
  22. enabled: true
  23. k8sAuthMethodHost: <kubernetes-api-url-of-secondary>
  24. primaryDatacenter: dc1
  25. gossipEncryption:
  26. secretName: consul-federation
  27. secretKey: gossipEncryptionKey
  28. connectInject:
  29. enabled: true
  30. controller:
  31. enabled: true
  32. meshGateway:
  33. enabled: true
  34. server:
  35. # Here we're including the server config exported from the primary
  36. # via the federation secret. This config includes the addresses of
  37. # the primary datacenter's mesh gateways so Consul can begin federation.
  38. extraVolumes:
  39. - type: secret
  40. name: consul-federation
  41. items:
  42. - key: serverConfigJSON
  43. path: config.json
  44. load: true

Modifications:

  1. If ACLs are enabled, change the value of global.federation.k8sAuthMethodHost to the full URL (including https://) of the secondary cluster’s Kubernetes API.

  2. global.federation.primaryDatacenter must be set to the name of the primary datacenter.

  3. The Consul datacenter name for the datacenter in this example is dc2. The datacenter name in each federated cluster must be unique.

  4. ACLs are enabled in the above config file. They can be disabled by removing the whole acls block:

    1. acls:
    2. manageSystemACLs: false
    3. replicationToken:
    4. secretName: consul-federation
    5. secretKey: replicationToken
    1. acls:
    2. manageSystemACLs: false
    3. replicationToken:
    4. secretName: consul-federation
    5. secretKey: replicationToken

    If ACLs are enabled in one datacenter, they must be enabled in all datacenters because in order to communicate with that one datacenter ACL tokens are required.

  5. Gossip encryption is enabled in the above config file. To disable it, don’t set the gossipEncryption key:

    1. global:
    2. # gossipEncryption:
    3. # secretName: consul-federation
    4. # secretKey: gossipEncryptionKey
    1. global:
    2. # gossipEncryption:
    3. # secretName: consul-federation
    4. # secretKey: gossipEncryptionKey

    If gossip encryption is enabled in one datacenter, it must be enabled in all datacenters because in order to communicate with that one datacenter the encryption key is required.

  6. The default mesh gateway configuration creates a Kubernetes Load Balancer service. If you wish to customize the mesh gateway, for example using a Node Port service or a custom DNS entry, see the Helm reference for that setting.

With your config.yaml ready to go, follow our Installation Guide to install Consul on your secondary cluster(s).

Verifying Federation

To verify that both datacenters are federated, run the consul members -wan command on one of the Consul server pods:

  1. $ kubectl exec statefulset/consul-server --namespace consul -- consul members -wan
  2. Node Address Status Type Build Protocol DC Segment
  3. consul-server-0.dc1 10.32.4.216:8302 alive server 1.8.0 2 dc1 <all>
  4. consul-server-0.dc2 192.168.2.173:8302 alive server 1.8.0 2 dc2 <all>
  5. consul-server-1.dc1 10.32.5.161:8302 alive server 1.8.0 2 dc1 <all>
  6. consul-server-1.dc2 192.168.88.64:8302 alive server 1.8.0 2 dc2 <all>
  7. consul-server-2.dc1 10.32.1.175:8302 alive server 1.8.0 2 dc1 <all>
  8. consul-server-2.dc2 192.168.35.174:8302 alive server 1.8.0 2 dc2 <all>
  1. $ kubectl exec statefulset/consul-server --namespace consul -- consul members -wan
  2. Node Address Status Type Build Protocol DC Segment
  3. consul-server-0.dc1 10.32.4.216:8302 alive server 1.8.0 2 dc1 <all>
  4. consul-server-0.dc2 192.168.2.173:8302 alive server 1.8.0 2 dc2 <all>
  5. consul-server-1.dc1 10.32.5.161:8302 alive server 1.8.0 2 dc1 <all>
  6. consul-server-1.dc2 192.168.88.64:8302 alive server 1.8.0 2 dc2 <all>
  7. consul-server-2.dc1 10.32.1.175:8302 alive server 1.8.0 2 dc1 <all>
  8. consul-server-2.dc2 192.168.35.174:8302 alive server 1.8.0 2 dc2 <all>

In this example (run from dc1), you can see that this datacenter knows about the servers in dc2 and that they have status alive.

You can also use the consul catalog services command with the -datacenter flag to ensure each datacenter can read each other’s services. In this example, our kubectl context is dc1 and we’re querying for the list of services in dc2:

  1. $ kubectl exec statefulset/consul-server --namespace consul -- consul catalog services -datacenter dc2
  2. consul
  3. mesh-gateway
  1. $ kubectl exec statefulset/consul-server --namespace consul -- consul catalog services -datacenter dc2
  2. consul
  3. mesh-gateway

You can switch kubectl contexts and run the same command in dc2 with the flag -datacenter dc1 to ensure dc2 can communicate with dc1.

Consul UI

We can also use the Consul UI to verify federation. See Viewing the Consul UI for instructions on how to view the UI.

NOTE: If ACLs are enabled, your kubectl context must be in the primary datacenter to retrieve the bootstrap token mentioned in the UI documentation.

With the UI open, you’ll be able to switch between datacenters via the dropdown in the top left:

Consul Datacenter Dropdown

Next Steps

With your Kubernetes clusters federated, try out using Consul service mesh to route between services deployed on each cluster by following our Learn tutorial: Secure and Route Service Mesh Communication Across Kubernetes.

You can also read our in-depth documentation on Consul Service Mesh In Kubernetes.

If you are still considering a move to Kubernetes, or to Consul on Kubernetes specifically, our Migrate to Microservices with Consul Service Mesh on Kubernetes collection uses an example application written by a fictional company to illustrate why and how organizations can migrate from monolith to microservices using Consul service mesh on Kubernetes. The case study in this collection should provide information valuable for understanding how to develop services that leverage Consul during any stage of your microservices journey.