Establish cluster peering connections on Kubernetes
This page details the process for establishing a cluster peering connection between services in a Consul on Kubernetes deployment.
The overall process for establishing a cluster peering connection consists of the following steps:
- Create a peering token in one cluster.
- Use the peering token to establish peering with a second cluster.
- Export services between clusters.
- Create intentions to authorize services for peers.
Cluster peering between services cannot be established until all four steps are complete.
Cluster peering between services cannot be established until all four steps are complete. If you want to establish cluster peering connections and create sameness groups at the same time, refer to the guidance in create sameness groups.
For general guidance for establishing cluster peering connections, refer to Establish cluster peering connections.
Prerequisites
You must meet the following requirements to use Consul's cluster peering features with Kubernetes:
- Consul v1.14.1 or higher
- Consul on Kubernetes v1.0.0 or higher
- At least two Kubernetes clusters
In Consul on Kubernetes, peers identify each other using the metadata.name
values you establish when creating the PeeringAcceptor
and PeeringDialer
CRDs. For additional information about requirements for cluster peering on Kubernetes deployments, refer to Cluster peering on Kubernetes technical specifications.
Assign cluster IDs to environmental variables
After you provision a Kubernetes cluster and set up your kubeconfig file to manage access to multiple Kubernetes clusters, you can assign your clusters to environmental variables for future use.
Get the context names for your Kubernetes clusters using one of these methods:
- Run the
kubectl config current-context
command to get the context for the cluster you are currently in. - Run the
kubectl config get-contexts
command to get all configured contexts in your kubeconfig file.
- Run the
Use the
kubectl
command to export the Kubernetes context names and then set them to variables. For more information on how to use kubeconfig and contexts, refer to the Kubernetes docs on configuring access to multiple clusters.
Install Consul using Helm and configure peering over mesh gateways
To use cluster peering with Consul on Kubernetes deployments, update the Helm chart with the required values. After updating the Helm chart, you can use the consul-k8s
CLI to apply values.yaml
to each cluster.
In
cluster-01
, run the following commands:In
cluster-02
, run the following commands:For both clusters apply the
Mesh
configuration entry values provided in Mesh Gateway Specifications to allow establishing peering connections over mesh gateways.
Configure the mesh gateway mode for traffic between services
In Kubernetes deployments, you can configure mesh gateways to use local
mode so that a service dialing a service in a remote peer dials the local mesh gateway instead of the remote mesh gateway. To configure the mesh gateway mode so that this traffic always leaves through the local mesh gateway, you can use the ProxyDefaults
CRD.
In
cluster-01
apply the followingProxyDefaults
CRD to configure the mesh gateway mode.proxy-defaults.yamlIn
cluster-02
apply the followingProxyDefaults
CRD to configure the mesh gateway mode.proxy-defaults.yaml
Create a peering token
To begin the cluster peering process, generate a peering token in one of your clusters. The other cluster uses this token to establish the peering connection.
Every time you generate a peering token, a single-use secret for establishing the secret is embedded in the token. Because regenerating a peering token invalidates the previously generated secret, you must use the most recently created token to establish peering connections.
In
cluster-01
, create thePeeringAcceptor
custom resource. To ensure cluster peering connections are secure, themetadata.name
field cannot be duplicated. Refer to the peer by a specific name.acceptor.yamlApply the
PeeringAcceptor
resource to the first cluster.Save your peering token so that you can export it to the other cluster.
Establish a connection between clusters
Next, use the peering token to establish a secure connection between the clusters.
Apply the peering token to the second cluster.
In
cluster-02
, create thePeeringDialer
custom resource. To ensure cluster peering connections are secure, themetadata.name
field cannot be duplicated. Refer to the peer by a specific name.dialer.yamlApply the
PeeringDialer
resource to the second cluster.
Export services between clusters
After you establish a connection between the clusters, you need to create an exported-services
CRD that defines the services that are available to another admin partition.
While the CRD can target admin partitions either locally or remotely, clusters peering always exports services to remote admin partitions. Refer to exported service consumers for more information.
For the service in
cluster-02
that you want to export, add the"consul.hashicorp.com/connect-inject": "true"
annotation to your service's pods prior to deploying. The annotation allows the workload to join the mesh. It is highlighted in the following example:backend.yamlDeploy the
backend
service to the second cluster.In
cluster-02
, create anExportedServices
custom resource. The name of the peer that consumes the service should be identical to the name set in thePeeringDialer
CRD.exported-service.yamlApply the
ExportedServices
resource to the second cluster.
Authorize services for peers
Before you can call services from peered clusters, you must set service intentions that authorize those clusters to use specific services. Consul prevents services from being exported to unauthorized clusters.
Create service intentions for the second cluster. The name of the peer should match the name set in the
PeeringDialer
CRD.intention.yamlApply the intentions to the second cluster.
Add the
"consul.hashicorp.com/connect-inject": "true"
annotation to your service's pods before deploying the workload so that the services incluster-01
can dialbackend
incluster-02
. To dial the upstream service from an application, configure the application so that that requests are sent to the correct DNS name as specified in Service Virtual IP Lookups. In the following example, the annotation that allows the workload to join the mesh and the configuration provided to the workload that enables the workload to dial the upstream service using the correct DNS name is highlighted. Service Virtual IP Lookups for Consul Enterprise details how you would similarly format a DNS name including partitions and namespaces.frontend.yamlApply the service file to the first cluster.
Run the following command in
frontend
and then check the output to confirm that you peered your clusters successfully.