Exported Services

This topic describes the exported-services configuration entry type. The exported-services configuration entry enables Consul to export service instances to other clusters from a single file and connect services across clusters. For additional information, refer to Cluster Peering and Admin Partitions.

v1.11.0+: This config entry is supported in Consul versions 1.11.0+.

Introduction

To configure Consul to export services contained in a Consul Enterprise admin partition or Consul OSS datacenter to one or more additional clusters, create a new configuration entry and declare exported-services in the kind field. This configuration entry enables you to route traffic between services in different clusters.

You can configure the settings defined in the exported-services configuration entry to apply to all namespaces in a Consul Enterprise admin partition.

Requirements

  • A 1.11.0+ Consul Enteprise binary or a 1.13.0+ Consul OSS binary.
  • Enterprise Only: A corresponding partition that the configuration entry can export from. For example, the exported-services configuration entry for a partition named frontend requires an existing frontend partition.

Usage

  1. Verify that your datacenter meets the conditions specified in the Requirements.
  2. Specify the exported-services configuration in the agent configuration file (see config_entries) as described in Configuration.
  3. Apply the configuration using one of the following methods:

Configuration

Configure the following parameters to define a exported-services configuration entry:

Exported Services - 图1

Exported Services - 图2

Exported services configuration syntax

Exported services configuration syntax

HCL

Exported Services - 图3

  • HCL
  • Kubernetes YAML
  • JSON
  1. Kind = "exported-services"
  2. Name = "default"
  3. Services = [
  4. {
  5. Name = "<name of service to export>"
  6. Consumers = [
  7. {
  8. PeerName = "<name of the peered cluster that dials the exported service>"
  9. }
  10. ]
  11. }
  12. ]
  1. Kind = "exported-services"
  2. Name = "default"
  3. Services = [
  4. {
  5. Name = "<name of service to export>"
  6. Consumers = [
  7. {
  8. PeerName = "<name of the peered cluster that dials the exported service>"
  9. }
  10. ]
  11. }
  12. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ExportedServices
  3. metadata:
  4. name: default
  5. spec:
  6. services:
  7. - name: <name of service to export>
  8. consumers:
  9. - peerName: <name of the peered cluster that dials the exported service>
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ExportedServices
  3. metadata:
  4. name: default
  5. spec:
  6. services:
  7. - name: <name of service to export>
  8. consumers:
  9. - peerName: <name of the peered cluster that dials the exported service>
  1. "Kind": "exported-services",
  2. "Name": "default",
  3. "Services": [
  4. {
  5. "Name": "<name of service to export>",
  6. "Consumers": [
  7. {
  8. "PeerName": "<name of the peered cluster that dials the exported service>"
  9. }
  10. ]
  11. }
  12. ]
  1. "Kind": "exported-services",
  2. "Name": "default",
  3. "Services": [
  4. {
  5. "Name": "<name of service to export>",
  6. "Consumers": [
  7. {
  8. "PeerName": "<name of the peered cluster that dials the exported service>"
  9. }
  10. ]
  11. }
  12. ]

Configuration Parameters

The following table describes the parameters associated with the exported-services configuration entry.

ParameterDescriptionRequiredDefault
KindString value that enables the configuration entry. The value should always be exported-services (HCL and JSON) or ExportedServices (YAML)RequiredNone
Partition
Enterprise
String value that specifies the name of the partition that contains the services you want to export.
RequiredNone
NameString value that specifies the name of the partition that contains the services you want to export. Must be default in Consul OSS.RequiredNone
ServicesList of objects that specify which services to export. For details, refer to Services.RequiredNone
MetaObject that defines a map of the max 64 key/value pairs.OptionalNone

Services

The Services parameter contains a list of one or more parameters that specify which services to export, which namespaces the services reside, and the destination cluster for the exported services. Each item in the Services list must contain the following parameters:

  • Name: Specifies the name of the service to export. You can use an asterisk wildcard (*) to include all services in the namespace.
  • Namespace:

    Enterprise

    Specifies the namespace containing the services to export. You can use an asterisk wildcard (*) to include all namespaces in the partition.

  • Consumers: Specifies one or more objects that identify a destination cluster for the exported services.

Consumers

The Consumers parameter contains a list of one or more parameters that specify the destination cluster for an exported service. Each item in the Consumers list must contain exactly one of the following parameters:

  • PeerName: Specifies the name of the peered cluster to export the service to. A asterisk wildcard (*) cannot be specified as the PeerName. Added in Consul 1.13.0.
  • Partition:

    Enterprise

    Specifies an admin partition in the datacenter to export the service to. A asterisk wildcard (*) cannot be specified as the Partition.

Examples

Exporting services to peered clusters

Exported Services - 图4

Exported Services - 图5

The following example configures Consul to export the payments and refunds services to the peered web-shop cluster.

HCL

Exported Services - 图6

  • HCL
  • Kubernetes YAML
  • JSON
  1. Kind = "exported-services"
  2. Name = "default"
  3. Services = [
  4. {
  5. Name = "payments"
  6. Consumers = [
  7. {
  8. PeerName = "web-shop"
  9. },
  10. ]
  11. },
  12. {
  13. Name = "refunds"
  14. Consumers = [
  15. {
  16. PeerName = "web-shop"
  17. }
  18. ]
  19. }
  20. ]
  1. Kind = "exported-services"
  2. Name = "default"
  3. Services = [
  4. {
  5. Name = "payments"
  6. Consumers = [
  7. {
  8. PeerName = "web-shop"
  9. },
  10. ]
  11. },
  12. {
  13. Name = "refunds"
  14. Consumers = [
  15. {
  16. PeerName = "web-shop"
  17. }
  18. ]
  19. }
  20. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. Kind: ExportedServices
  3. metadata:
  4. name: default
  5. spec:
  6. services:
  7. - name: payments
  8. consumers:
  9. - peerName: web-shop
  10. - name: refunds
  11. consumers:
  12. - peerName: web-shop
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. Kind: ExportedServices
  3. metadata:
  4. name: default
  5. spec:
  6. services:
  7. - name: payments
  8. consumers:
  9. - peerName: web-shop
  10. - name: refunds
  11. consumers:
  12. - peerName: web-shop
  1. "Kind": "exported-services",
  2. "Name": "default",
  3. "Services": [
  4. {
  5. "Name": "payments",
  6. "Consumers": [
  7. {
  8. "PeerName": "web-shop"
  9. },
  10. ],
  11. },
  12. {
  13. "Name": "refunds",
  14. "Consumers": [
  15. {
  16. "PeerName": "web-shop"
  17. }
  18. ]
  19. }
  20. ]
  1. "Kind": "exported-services",
  2. "Name": "default",
  3. "Services": [
  4. {
  5. "Name": "payments",
  6. "Consumers": [
  7. {
  8. "PeerName": "web-shop"
  9. },
  10. ],
  11. },
  12. {
  13. "Name": "refunds",
  14. "Consumers": [
  15. {
  16. "PeerName": "web-shop"
  17. }
  18. ]
  19. }
  20. ]

Exporting all services

Exported Services - 图7

Exported Services - 图8

The following example configures Consul to export all services in the datacenter to the peered monitoring and platform clusters.

HCL

Exported Services - 图9

  • HCL
  • Kubernetes YAML
  • JSON
  1. Kind = "exported-services"
  2. Name = "default"
  3. Services = [
  4. {
  5. Name = "*"
  6. Consumers = [
  7. {
  8. PeerName = "monitoring"
  9. },
  10. {
  11. PeerName = "platform"
  12. }
  13. ]
  14. }
  15. ]
  1. Kind = "exported-services"
  2. Name = "default"
  3. Services = [
  4. {
  5. Name = "*"
  6. Consumers = [
  7. {
  8. PeerName = "monitoring"
  9. },
  10. {
  11. PeerName = "platform"
  12. }
  13. ]
  14. }
  15. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. Kind: ExportedServices
  3. metadata:
  4. name: default
  5. spec:
  6. services:
  7. - name: *
  8. consumers:
  9. - peerName: monitoring
  10. - peerName: platform
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. Kind: ExportedServices
  3. metadata:
  4. name: default
  5. spec:
  6. services:
  7. - name: *
  8. consumers:
  9. - peerName: monitoring
  10. - peerName: platform
  1. "Kind": "exported-services",
  2. "Name": "default",
  3. "Services": [
  4. {
  5. "Name": "*",
  6. "Namespace": "*"
  7. "Consumers": [
  8. {
  9. "PeerName": "monitoring"
  10. },
  11. {
  12. "PeerName": "platform"
  13. }
  14. ]
  15. }
  16. ]
  1. "Kind": "exported-services",
  2. "Name": "default",
  3. "Services": [
  4. {
  5. "Name": "*",
  6. "Namespace": "*"
  7. "Consumers": [
  8. {
  9. "PeerName": "monitoring"
  10. },
  11. {
  12. "PeerName": "platform"
  13. }
  14. ]
  15. }
  16. ]

Reading Services

When an exported service has been imported to another cluster, you can use the health REST API endpoint to query the service on the consumer cluster.

Exported Services - 图10

Exported Services - 图11

The following example queries the finance peer for the imported payments service:

  1. $ curl 'localhost:8500/v1/health/service/payments?peer=finance'
  1. $ curl 'localhost:8500/v1/health/service/payments?peer=finance'

An ACL token with service:write permissions is required for the cluster the query is made from. If the call in the previous example is made from a service named web in a partition named frontend, then the request requires a token with write permissions to web in the frontend partition.

Exports are available to all services in the consumer cluster. In the previous example, any service with write permissions for the frontend partition can read exports.

For additional information, refer to Health HTTP Endpoint.