Deploy Single Consul Datacenter Across Multiple Kubernetes Clusters
Note: When running Consul across multiple Kubernetes clusters, we recommend using admin partitions for production environments. This Consul Enterprise feature allows you to accommodate multiple tenants without resource collisions when administering a cluster at scale. Admin partitions also enable you to run Consul on Kubernetes clusters across a non-flat network.
This page describes deploying a single Consul datacenter in multiple Kubernetes clusters, with servers running in one cluster and only Consul on Kubernetes components in the rest of the clusters. This example uses two Kubernetes clusters, but this approach could be extended to using more than two.
Requirements
consul-k8s
v1.0.x or higher, and Consul 1.14.x or higher- Kubernetes clusters must be able to communicate over LAN on a flat network.
- Either the Helm release name for each Kubernetes cluster must be unique, or
global.name
for each Kubernetes cluster must be unique to prevent collisions of ACL resources with the same prefix.
Prepare Helm release name ahead of installs
The Helm release name must be unique for each Kubernetes cluster.
The Helm chart uses the Helm release name as a prefix for the
ACL resources that it creates, such as tokens and auth methods. If the names of the Helm releases are identical, or if global.name
for each cluster is identical, subsequent Consul on Kubernetes clusters will overwrite existing ACL resources and cause the clusters to fail.
Before proceeding with installation, prepare the Helm release names as environment variables for both the server and client install.
Deploying Consul servers in the first cluster
First, deploy the first cluster with Consul servers according to the following example Helm configuration.
Note that this will deploy a secure configuration with gossip encryption, TLS for all components and ACLs. In addition, this will enable the Consul Service Mesh and the controller for CRDs that can be used later to verify the connectivity of services across clusters.
The UI's service type is set to be NodePort
.
This is needed to connect to servers from another cluster without using the pod IPs of the servers,
which are likely going to change.
Other services are exposed as NodePort
services and configured with random port numbers. In this example, the grpc
port is set to 30014
, which enables services to discover Consul servers using gRPC when connecting from another cluster.
To deploy, first generate the Gossip encryption key and save it as a Kubernetes secret.
Now install Consul cluster with Helm:
Once the installation finishes and all components are running and ready, the following information needs to be extracted (using the below command) and applied to the second Kubernetes cluster.
- The CA certificate generated during installation
- The ACL bootstrap token generated during installation
Deploying Consul Kubernetes in the second cluster
Note: If multiple Kubernetes clusters will be joined to the Consul Datacenter, then the following instructions will need to be repeated for each additional Kubernetes cluster.
Switch to the second Kubernetes cluster where Consul clients will be deployed that will join the first Consul cluster.
First, apply the credentials extracted from the first cluster to the second cluster:
To deploy in the second cluster, the following example Helm configuration will be used:
Note the references to the secrets extracted and applied from the first cluster in ACL and TLS configuration.
The externalServers.hosts
and externalServers.httpsPort
refer to the IP and port of the UI's NodePort service deployed in the first cluster.
Set the externalServers.hosts
to any Node IP of the first cluster,
which can be seen by running kubectl get nodes --output wide
.
Set externalServers.httpsPort
to the nodePort
of the cluster1-consul-ui
service.
In our example, the port is 31557
.
The grpcPort: 30014
configuration refers to the gRPC port number specified in the NodePort
configuration in the first cluster.
Set the externalServer.tlsServerName
to server.dc1.consul
. This the DNS SAN
(Subject Alternative Name) that is present in the Consul server's certificate.
This is required because the connection to the Consul servers uses the node IP,
but that IP isn't present in the server's certificate.
To make sure that the hostname verification succeeds during the TLS handshake, set the TLS
server name to a DNS name that is present in the certificate.
Next, set externalServers.k8sAuthMethodHost
to the address of the second Kubernetes API server.
This should be the address that is reachable from the first cluster, so it cannot be the internal DNS
available in each Kubernetes cluster. Consul needs it so that consul login
with the Kubernetes auth method will work
from the second cluster.
More specifically, the Consul server will need to perform the verification of the Kubernetes service account
whenever consul login
is called, and to verify service accounts from the second cluster, it needs to
reach the Kubernetes API in that cluster.
The easiest way to get it is from the kubeconfig
by running kubectl config view
and grabbing
the value of cluster.server
for the second cluster.
Now, proceed with the installation of the second cluster.
Verifying the Consul Service Mesh works
When Transparent proxy is enabled, services in one Kubernetes cluster that need to communicate with a service in another Kubernetes cluster must have an explicit upstream configured through the "consul.hashicorp.com/connect-service-upstreams" annotation.
Now that the Consul cluster spanning across multiple k8s clusters is up and running, deploy two services in separate k8s clusters and verify that they can connect to each other.
First, deploy static-server
service in the first cluster:
Note that defining a Service intention is required so that our services are allowed to talk to each other.
Next, deploy static-client
in the second cluster with the following configuration:
Once both services are up and running, try connecting to the static-server
from static-client
:
A successful installation would return hello world
for the above curl command output.