Define services

This topic describes how to define services so that they can be discovered by other services. Refer to Services Overview for additional information.

Overview

You must tell Consul about the services deployed to your network if you want them to be discoverable. You can define services in a configuration file or send the service definition parameters as a payload to the /agent/service/register API endpoint. Refer to Register Services and Health Checks for details about how to register services with Consul.

You can define multiple services individually using service blocks or group multiple services into the same services configuration block. Refer to Define multiple services in a single file for additional information.

If Consul service mesh is enabled in your network, you can use the service defaults configuration entry to specify default global values for services. The configuration entry lets you define common service parameter, such as upstreams, namespaces, and partitions. Refer to Define service defaults for additional information.

Requirements

The core service discovery features are available in all versions of Consul.

Service defaults

To use the service defaults configuration entry, verify that your installation meets the following requirements:

  • Consul 1.5.0+
  • Consul 1.8.4+ is required to use the ServiceDefaults custom resource on Kubernetes

ACLs

If ACLs are enabled, resources in your network must present a token with service:read access to read a service defaults configuration entry.

You must also present a token with service:write access to create, update, or delete a service defaults configuration entry.

Service configurations must also contain and present an ACL token to perform anti-entropy syncs and deregistration operations. Refer to Modify anti-entropy synchronozation for additional information.

On Consul Enterprise, you can register services with specific namespaces if the services’ ACL tokens are scoped to the namespace. Services registered with a service definition do not inherit the namespace associated with the ACL token specified in the token field. The namespace and the token parameters must be included in the service definition for the service to be registered to the namespace that the ACL token is scoped to.

Define a service

Create a file for your service configurations and add a service block. The service block contains the parameters that configure various aspects of the service, including how it is discovered by other services in the network. The only required parameter is name. Refer to Service Definition Reference for details about the configuration options.

For Kubernetes environments, you can enable the connectInject configuration in your Consul Helm chart so that Consul automatically adds a sidecar to each of your pods. Consul uses the Kubernetes Service definition as a reference.

The following example defines a service named redis that is available on port 80. By default, the service has the IP address of the agent node.

service.hcl

  1. service {
  2. name = "redis"
  3. id = "redis"
  4. port = 80
  5. tags = ["primary"]
  6. meta = {
  7. custom_meta_key = "custom_meta_value"
  8. }
  9. tagged_addresses = {
  10. lan = {
  11. address = "192.168.0.55"
  12. port = 8000
  13. }
  14. wan = {
  15. address = "198.18.0.23"
  16. port = 80
  17. }
  18. }
  19. }

service.json

  1. {
  2. "service": [
  3. {
  4. "id": "redis",
  5. "meta": [
  6. {
  7. "custom_meta_key": "custom_meta_value"
  8. }
  9. ],
  10. "name": "redis",
  11. "port": 80,
  12. "tagged_addresses": [
  13. {
  14. "lan": [
  15. {
  16. "address": "192.168.0.55",
  17. "port": 8000
  18. }
  19. ],
  20. "wan": [
  21. {
  22. "address": "198.18.0.23",
  23. "port": 80
  24. }
  25. ]
  26. }
  27. ],
  28. "tags": [
  29. "primary"
  30. ]
  31. }
  32. ]
  33. }

service.yaml

  1. service:
  2. - id: redis
  3. meta:
  4. - custom_meta_key: custom_meta_value
  5. name: redis
  6. port: 80
  7. tagged_addresses:
  8. - lan:
  9. - address: 192.168.0.55
  10. port: 8000
  11. wan:
  12. - address: 198.18.0.23
  13. port: 80
  14. tags:
  15. - primary

Health checks

You can add a check or checks block to your service configuration to define one or more health checks that monitor the health of your services. Refer to Define Health Checks for additional information.

Register a service

You can register your service using the consul services command or by calling the /agent/services API endpoint. Refer to Register Services and Health Checks for details.

Define service defaults

If Consul service mesh is enabled in your network, you can define default values for services in your mesh by creating and applying a service-defaults configuration entry containing. Refer to Service Mesh Configuration Overview for additional information.

Create a file for the configuration entry and specify the required fields. If you are authoring service-defaults in HCL or JSON, the Kind and Name fields are required. On Kubernetes, the apiVersion, kind, and metadata.name fields are required. Refer to Service Defaults Reference for details about the configuration options.

If you use Consul Enterprise, you can also specify the Namespace and Partition fields to apply the configuration to services in a specific namespace or partition. For Kubernetes environments, the configuration entry is always created in the same partition as the Kubernetes cluster.

Consul OSS example

The following example instructs services named counting to send up to 512 concurrent requests to a mesh gateway:

  1. Kind = "service-defaults"
  2. Name = "counting"
  3. UpstreamConfig = {
  4. Defaults = {
  5. MeshGateway = {
  6. Mode = "local"
  7. }
  8. Limits = {
  9. MaxConnections = 512
  10. MaxPendingRequests = 512
  11. MaxConcurrentRequests = 512
  12. }
  13. }
  14. Overrides = [
  15. {
  16. Name = "dashboard"
  17. MeshGateway = {
  18. Mode = "remote"
  19. }
  20. }
  21. ]
  22. }
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceDefaults
  3. metadata:
  4. name: counting
  5. spec:
  6. upstreamConfig:
  7. defaults:
  8. meshGateway:
  9. mode: local
  10. limits:
  11. maxConnections: 512
  12. maxPendingRequests: 512
  13. maxConcurrentRequests: 512
  14. overrides:
  15. - name: dashboard
  16. meshGateway:
  17. mode: remote
  1. {
  2. "Kind": "service-defaults",
  3. "Name": "counting",
  4. "UpstreamConfig": {
  5. "Defaults": {
  6. "MeshGateway": {
  7. "Mode": "local"
  8. },
  9. "Limits": {
  10. "MaxConnections": 512,
  11. "MaxPendingRequests": 512,
  12. "MaxConcurrentRequests": 512
  13. }
  14. },
  15. "Overrides": [
  16. {
  17. "Name": "dashboard",
  18. "MeshGateway": {
  19. "Mode": "remote"
  20. }
  21. }
  22. ]
  23. }
  24. }

Consul Enterprise example

The following example instructs services named counting in the prod namespace to send up to 512 concurrent requests to a mesh gateway:

  1. Kind = "service-defaults"
  2. Name = "counting"
  3. Namespace = "prod"
  4. UpstreamConfig = {
  5. Defaults = {
  6. MeshGateway = {
  7. Mode = "local"
  8. }
  9. Limits = {
  10. MaxConnections = 512
  11. MaxPendingRequests = 512
  12. MaxConcurrentRequests = 512
  13. }
  14. }
  15. Overrides = [
  16. {
  17. Name = "dashboard"
  18. MeshGateway = {
  19. Mode = "remote"
  20. }
  21. }
  22. ]
  23. }
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceDefaults
  3. metadata:
  4. name: counting
  5. namespace: prod
  6. spec:
  7. upstreamConfig:
  8. defaults:
  9. meshGateway:
  10. mode: local
  11. limits:
  12. maxConnections: 512
  13. maxPendingRequests: 512
  14. maxConcurrentRequests: 512
  15. overrides:
  16. - name: dashboard
  17. meshGateway:
  18. mode: remote
  1. {
  2. "Kind": "service-defaults",
  3. "Name": "counting",
  4. "Namespace" : "prod",
  5. "UpstreamConfig": {
  6. "Defaults": {
  7. "MeshGateway": {
  8. "Mode": "local"
  9. },
  10. "Limits": {
  11. "MaxConnections": 512,
  12. "MaxPendingRequests": 512,
  13. "MaxConcurrentRequests": 512
  14. }
  15. },
  16. "Overrides": [
  17. {
  18. "Name": "dashboard",
  19. "MeshGateway": {
  20. "Mode": "remote"
  21. }
  22. }
  23. ]
  24. }
  25. }

Apply service defaults

You can apply your service-defaults configuration entry using the consul config command or by calling the /config API endpoint. In Kubernetes environments, apply the service-defaults custom resource definitions (CRD) to implement and manage Consul configuration entries.

Refer to the following topics for details about applying configuration entries:

Define multiple services in a single file

The services block contains an array of service objects. It is a wrapper that enables you to define multiple services in the service definition and instruct Consul to expect more than just a single service configuration. As a result, you can register multiple services in a single consul services register command. Note that the /agent/service/register API endpoint does not support the services parameter.

In the following example, the service definition configures an instance of the redis service tagged as primary running on port 6000. It also configures an instance of the service tagged as secondary running on port 7000.

Multiple Service Definitions

Multiple Service Definitions

Define services - 图1

redis-services.hcl

  1. services {
  2. id = "red0"
  3. name = "redis"
  4. tags = [
  5. "primary"
  6. ]
  7. address = ""
  8. port = 6000
  9. checks = [
  10. {
  11. args = ["/bin/check_redis", "-p", "6000"]
  12. interval = "5s"
  13. timeout = "20s"
  14. }
  15. ]
  16. }
  17. services {
  18. id = "red1"
  19. name = "redis"
  20. tags = [
  21. "delayed",
  22. "secondary"
  23. ]
  24. address = ""
  25. port = 7000
  26. checks = [
  27. {
  28. args = ["/bin/check_redis", "-p", "7000"]
  29. interval = "30s"
  30. timeout = "60s"
  31. }
  32. ]
  33. }

Define services - 图2

redis-services.json

  1. {
  2. "services": [
  3. {
  4. "id": "red0",
  5. "name": "redis",
  6. "tags": [
  7. "primary"
  8. ],
  9. "address": "",
  10. "port": 6000,
  11. "checks": [
  12. {
  13. "args": ["/bin/check_redis", "-p", "6000"],
  14. "interval": "5s",
  15. "timeout": "20s"
  16. }
  17. ]
  18. },
  19. {
  20. "id": "red1",
  21. "name": "redis",
  22. "tags": [
  23. "delayed",
  24. "secondary"
  25. ],
  26. "address": "",
  27. "port": 7000,
  28. "checks": [
  29. {
  30. "args": ["/bin/check_redis", "-p", "7000"],
  31. "interval": "30s",
  32. "timeout": "60s"
  33. }
  34. ]
  35. },
  36. ...
  37. ]
  38. }

Modify anti-entropy synchronization

By default, the Consul agent uses anti-entropy mechanisms to maintain information about services and service health, and synchronize local states with the Consul catalog. You can enable the enable_tag_override option in the service configuration, which lets external agents change the tags for a service. This can be useful in situations where an external monitoring service needs to be the source of truth for tag information. Refer Anti-entropy for details.

Add the enable_tag_override option to the service block and set the value to true:

  1. service {
  2. ## ...
  3. enable_tag_override = true
  4. ## ...
  5. }
  1. "service": {
  2. ## ...
  3. "enable_tag_override": true,
  4. ## ...
  5. }

This configuration only applies to the locally registered service. Nodes that register the same service apply the enable_tag_override and other service configurations independently. The tags for a service registered on one node update are not affected by operations performed on services with the same name registered on other nodes.

Refer to enable_tag_override for additional configuration information.

Services in service mesh environments

Defining services for service mesh environments on virtual machines and in Kubernetes requires a different workflow.

Define service mesh proxies

You can register services to function as service mesh or sidecar proxies so that they can facilitate communication between other services across your network. Refer to Service Mesh Proxy Overview for additional information.

Define services in Kubernetes

You can enable the services running in Kubernetes and Consul to sync automatically. Doing so ensures that Kubernetes services are available to Consul agents and services in Consul can be available as first-class Kubernetes services. Refer to Service Sync for Consul on Kubernetes for details.