Route Traffic to Peered Services

This topic describes how to configure Consul API Gateway to route traffic to services connected to the mesh through a cluster peering connection.

Requirements

  1. Consul 1.14 or later
  2. Verify that the requirements have been met.
  3. Verify that the Consul API Gateway CRDs and controller have been installed and applied. Refer to Installation for details.
  4. A peering connection must already be established between Consul clusters. Refer to Cluster Peering on Kubernetes for instructions.
  5. The Consul service that you want to route traffic to must be exported to the cluster containing your Gateway. Refer to Cluster Peering on Kubernetes for instructions.
  6. A ServiceResolver for the Consul service you want to route traffic to must be created in the cluster that contains your Gateway. Refer to Service Resolver Configuration Entry for instructions.

Configuration

Specify the following fields in your MeshService configuration to use this feature. Refer to the MeshService configuration reference for details about the parameters.

Example

In the following example, routes that use example-mesh-service as a backend are configured to send requests to the echo service exported by the peered Consul cluster cluster-02.

Route Traffic to Peered Services - 图1

serviceresolver.yaml

  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceResolver
  3. metadata:
  4. name: echo
  5. spec:
  6. redirect:
  7. peer: cluster-02
  8. service: echo

Route Traffic to Peered Services - 图2

meshservice.yaml

  1. apiVersion: api-gateway.consul.hashicorp.com/v1alpha1
  2. kind: MeshService
  3. metadata:
  4. name: example-mesh-service
  5. spec:
  6. name: echo
  7. peer: cluster-02

After applying the meshservice.yaml configuration, an HTTPRoute may then reference example-mesh-service as its backendRef.

Route Traffic to Peered Services - 图3

route.yaml

  1. apiVersion: gateway.networking.k8s.io/v1beta1
  2. kind: HTTPRoute
  3. metadata:
  4. name: example-route
  5. spec:
  6. ...
  7. rules:
  8. - backendRefs:
  9. - group: api-gateway.consul.hashicorp.com
  10. kind: MeshService
  11. name: example-mesh-service
  12. port: 3000
  13. ...