Consul
Create and Manage Cluster Peering Connections
Cluster peering is currently in beta: Functionality associated with cluster peering is subject to change. You should never use the beta release in secure environments or production scenarios. Features in beta may have performance issues, scaling issues, and limited support.
Cluster peering is not currently available in the HCP Consul offering.
A peering token enables cluster peering between different datacenters. Once you generate a peering token, you can use it to establish a connection between clusters. Then you can export services and create intentions so that peered clusters can call those services.
Create a peering connection
Cluster peering is not enabled by default on Consul servers. To peer clusters, you must first configure all Consul servers so that peering
is enabled
and the gRPC port(8502) accepts traffic from the peering cluster (e.g., client_addr="0.0.0.0"
). For additional information, refer to Configuration Files.
After enabling peering for all Consul servers, complete the following steps in order:
- Create a peering token
- Establish a connection between clusters
- Export services between clusters
- Authorize services for peers
You can generate peering tokens and initiate connections on any available agent using either the API or the Consul UI. If you use the API, we recommend performing these operations through a client agent in the partition you want to connect.
The UI does not currently support exporting services between clusters or authorizing services for peers.
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 establishment 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
, issue a request for a peering token.
$ curl --request POST --data '{"PeerName":"cluster-02"}' --url http://localhost:8500/v1/peering/token
The CLI outputs the peering token, which is a base64-encoded string containing the token details.
Create a JSON file that contains the first cluster's name and the peering token.
peering_token.json
{
"PeerName": "cluster-01",
"PeeringToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImF1ZCI6IlNvbHIifQ.5T7L_L1MPfQ_5FjKGa1fTPqrzwK4bNSM812nW6oyjb8"
}
Establish a connection between clusters
Next, use the peering token to establish a secure connection between the clusters.
In one of the client agents in "cluster-02," use peering_token.json
to establish the peering connection. This endpoint does not generate an output unless there is an error.
$ curl --request POST --data @peering_token.json http://127.0.0.1:8500/v1/peering/establish
When you connect server agents through cluster peering, they peer their default partitions. To establish peering connections for other partitions through server agents, you must add the Partition
field to peering_token.json
and specify the partitions you want to peer. For additional configuration information, refer to Cluster Peering - HTTP API.
Export services between clusters
After you establish a connection between the clusters, you need to create a configuration entry that defines the services that are available for other clusters. Consul uses this configuration entry to advertise service information and support service mesh connections across clusters.
First, create a configuration entry and specify the Kind
as "exported-services"
.
peering-config.hcl
Kind = "exported-services"
Name = "default"
Services = [
{
## The name and namespace of the service to export.
Name = "service-name"
Namespace = "default"
## The list of peer clusters to export the service to.
Consumers = [
{
## The peer name to reference in config is the one set
## during the peering process.
PeerName = "cluster-02"
}
]
}
]
Then, add the configuration entry to your cluster.
$ consul config write peering-config.hcl
Before you proceed, wait for the clusters to sync and make services available to their peers. You can issue an endpoint query to check the peered cluster status.
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.
First, create a configuration entry and specify the Kind
as "service-intentions"
. Declare the service on "cluster-02" that can access the service in "cluster-01." The following example sets service intentions so that "frontend-service" can access "backend-service."
peering-intentions.hcl
Kind = "service-intentions"
Name = "backend-service"
Sources = [
{
Name = "frontend-service"
Peer = "cluster-02"
Action = "allow"
}
]
If the peer's name is not specified in Peer
, then Consul assumes that the service is in the local cluster.
Then, add the configuration entry to your cluster.
$ consul config write peering-intentions.hcl
Manage peering connections
After you establish a peering connection, you can get a list of all active peering connections, read a specific peering connection's information, check peering connection health, and delete peering connections.
List all peering connections
You can list all active peering connections in a cluster.
After you establish a peering connection, query the /peering/
endpoint to get a list of all peering connections. For example, the following command requests a list of all peering connections on localhost
and returns the information as a series of JSON objects:
$ curl http://127.0.0.1:8500/v1/peerings
[
{
"ID": "462c45e8-018e-f19d-85eb-1fc1bcc2ef12",
"Name": "cluster-02",
"State": "ACTIVE",
"Partition": "default",
"PeerID": "e83a315c-027e-bcb1-7c0c-a46650904a05",
"PeerServerName": "server.dc1.consul",
"PeerServerAddresses": [
"10.0.0.1:8300"
],
"CreateIndex": 89,
"ModifyIndex": 89
},
{
"ID": "1460ada9-26d2-f30d-3359-2968aa7dc47d",
"Name": "cluster-03",
"State": "INITIAL",
"Partition": "default",
"Meta": {
"env": "production"
},
"CreateIndex": 109,
"ModifyIndex": 119
},
]
Read a peering connection
You can get information about individual peering connections between clusters.
After you establish a peering connection, query the /peering/:name
endpoint to get peering information about for a specific cluster. For example, the following command requests peering connection information for "cluster-02" and returns the info as a JSON object:
$ curl http://127.0.0.1:8500/v1/peering/cluster-02
{
"ID": "462c45e8-018e-f19d-85eb-1fc1bcc2ef12",
"Name": "cluster-02",
"State": "INITIAL",
"PeerID": "e83a315c-027e-bcb1-7c0c-a46650904a05",
"PeerServerName": "server.dc1.consul",
"PeerServerAddresses": [
"10.0.0.1:8300"
],
"CreateIndex": 89,
"ModifyIndex": 89
}
Check peering connection health
You can check the status of your peering connection to perform health checks.
To confirm that the peering connection between your clusters remains healthy, query the health/service
endpoint of one cluster from the other cluster. For example, in "cluster-02," query the endpoint and add the peer=cluster-01
query parameter to the end of the URL.
$ curl \
"http://127.0.0.1:8500/v1/health/service/<service-name>?peer=cluster-01"
A successful query includes service information in the output.
Delete peering connections
You can disconnect the peered clusters by deleting their connection. Deleting a peering connection stops data replication to the peer and deletes imported data, including services and CA certificates.
In "cluster-01," request the deletion through the /peering/ endpoint
.
$ curl --request DELETE http://127.0.0.1:8500/v1/peering/cluster-02