Service Defaults

v1.8.4+: On Kubernetes, the ServiceDefaults custom resource is supported in Consul versions 1.8.4+.
v1.5.0+: On other platforms, this config entry is supported in Consul versions 1.5.0+.

The service-defaults config entry kind (ServiceDefaults on Kubernetes) controls default global values for a service, such as its protocol.

Sample Config Entries

Default protocol

NOTE: The default protocol can also be configured globally for all proxies using the proxy defaults config entry. However, if the protocol value is specified in a service defaults config entry for a given service, that value will take precedence over the globally configured value from proxy defaults.

Set the default protocol for a service in the default namespace to HTTP:

HCL

Service Defaults - 图1

  • HCL
  • Kubernetes YAML
  • JSON
  1. Kind = "service-defaults"
  2. Name = "web"
  3. Namespace = "default"
  4. Protocol = "http"
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceDefaults
  3. metadata:
  4. name: web
  5. spec:
  6. protocol: http
  1. {
  2. "Kind": "service-defaults",
  3. "Name": "web",
  4. "Namespace": "default",
  5. "Protocol": "http"
  6. }

Upstream configuration Beta

Service Defaults - 图2

Service Defaults - 图3

Set default connection limits and mesh gateway mode across all upstreams of “counting”, and also override the mesh gateway mode used when dialing the “dashboard” service.

HCL

Service Defaults - 图4

  • HCL
  • Kubernetes YAML
  • JSON
  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. }

Available Fields

Service Defaults - 图5

Service Defaults - 图6

  • Kind - Must be set to service-defaults

  • Name (string: <required>) - Set to the name of the service being configured.

  • Namespace (string: "default")

    Enterprise

    - Specifies the namespace the config entry will apply to.

  • Meta (map<string|string>: nil) - Specifies arbitrary KV metadata pairs. Added in Consul 1.8.4.

  • Protocol (string: "tcp") - Sets the protocol of the service. This is used by Connect proxies for things like observability features and to unlock usage of the service-splitter and service-router config entries for a service. It also unlocks the ability to define L7 intentions via service-intentions. Supported values are one of tcp, http, http2, or grpc.

  • Mode (string: "") - One of direct or transparent. transparent represents that inbound and outbound application traffic is being captured and redirected through the proxy. This mode does not enable the traffic redirection itself. Instead it signals Consul to configure Envoy as if traffic is already being redirected. direct represents that the proxy’s listeners must be dialed directly by the local application and other proxies. Added in v1.10.0.

  • UpstreamConfig (UpstreamConfiguration: <optional>) - Controls default configuration settings that apply across all upstreams, and per-upstream configuration overrides. Note that per-upstream configuration applies across all federated datacenters to the pairing of source and upstream destination services. Added in v1.10.0.

    • Overrides (array<UpstreamConfig>: []) - A list of optional overrides for per-upstream configuration.

      • Name (string: "") - The upstream name to apply the configuration to. This should not be set to the wildcard specifier *.

      • Namespace (string: "") - The namespace of the upstream. This should not be set to the wildcard specifier *.

      • Protocol (string: "") - The protocol for the upstream listener.

        NOTE: The protocol of a service should ideally be configured via the protocol field of a service-defaults config entry for the upstream destination service. Configuring it in a proxy upstream config will not fully enable some L7 features. It is supported here for backwards compatibility with Consul versions prior to 1.6.0.

      • ConnectTimeoutMs (int: 5000) - The number of milliseconds to allow when making upstream connections before timing out.

        NOTE: The connect timeout of a service should ideally be configured via the connect_timeout field of a service-resolver config entry for the upstream destination service. Configuring it in a proxy upstream config will not fully enable some L7 features. It is supported here for backwards compatibility with Consul versions prior to 1.6.0.

      • MeshGateway (MeshGatewayConfig: <optional>) - Controls the default mesh gateway configuration for this upstream.

        • Mode (string: "") - One of none, local, or remote.
      • Limits (Limits: <optional>) - A set of limits to apply when connecting to the upstream service. These limits are applied on a per-service-instance basis. The following limits are respected.

        • MaxConnections (int: 0) - The maximum number of connections a service instance will be allowed to establish against the given upstream. Use this to limit HTTP/1.1 traffic, since HTTP/1.1 has a request per connection.

        • MaxPendingRequests (int: 0) - The maximum number of requests that will be queued while waiting for a connection to be established. For this configuration to be respected, a L7 protocol must be defined in the protocol field.

        • MaxConcurrentRequests (int: 0) - The maximum number of concurrent requests that will be allowed at a single point in time. Use this to limit HTTP/2 traffic, since HTTP/2 has many requests per connection. For this configuration to be respected, a L7 protocol must be defined in the protocol field.

      • PassiveHealthCheck (PassiveHealthCheck: <optional>) - Passive health checks are used to remove hosts from the upstream cluster which are unreachable or are returning errors..

        • Interval (duration: 0s) - The time between checks. Each check will cause hosts which have exceeded max_failures to be removed from the load balancer, and any hosts which have passed their ejection time to be returned to the load balancer.

        • MaxFailures (int: 0) - The number of consecutive failures which cause a host to be removed from the load balancer.

    • Defaults (UpstreamConfig: <optional>) - Default configuration that applies to all upstreams of this service.

      • Protocol (string: "") - The protocol for the upstream listener.

        NOTE: The protocol of a service should ideally be configured via the protocol field of a service-defaults config entry for the upstream destination service. Configuring it in a proxy upstream config will not fully enable some L7 features. It is supported here for backwards compatibility with Consul versions prior to 1.6.0.

      • ConnectTimeoutMs (int: 5000) - The number of milliseconds to allow when making upstream connections before timing out.

        NOTE: The connect timeout of a service should ideally be configured via the connect_timeout field of a service-resolver config entry for the upstream destination service. Configuring it in a proxy upstream config will not fully enable some L7 features. It is supported here for backwards compatibility with Consul versions prior to 1.6.0.

      • MeshGateway (MeshGatewayConfig: <optional>) - Controls the default mesh gateway configuration for this upstream.

        • Mode (string: "") - One of none, local, or remote.
      • Limits (Limits: <optional>) - A set of limits to apply when connecting to the upstream service. These limits are applied on a per-service-instance basis. The following limits are respected.

        • MaxConnections (int: 0) - The maximum number of connections a service instance will be allowed to establish against the given upstream. Use this to limit HTTP/1.1 traffic, since HTTP/1.1 has a request per connection.

        • MaxPendingRequests (int: 0) - The maximum number of requests that will be queued while waiting for a connection to be established. For this configuration to be respected, a L7 protocol must be defined in the protocol field.

        • MaxConcurrentRequests (int: 0) - The maximum number of concurrent requests that will be allowed at a single point in time. Use this to limit HTTP/2 traffic, since HTTP/2 has many requests per connection. For this configuration to be respected, a L7 protocol must be defined in the protocol field.

      • PassiveHealthCheck (PassiveHealthCheck: <optional>) - Passive health checks are used to remove hosts from the upstream cluster which are unreachable or are returning errors..

        • Interval (duration: 0s) - The time between checks. Each check will cause hosts which have exceeded max_failures to be removed from the load balancer, and any hosts which have passed their ejection time to be returned to the load balancer.

        • MaxFailures (int: 0) - The number of consecutive failures which cause a host to be removed from the load balancer.

  • TransparentProxy (TransparentProxyConfig: <optional>) - Controls configuration specific to proxies in transparent mode. Added in v1.10.0.

    • OutboundListenerPort (int: "15001") - The port the proxy should listen on for outbound traffic. This must be the port where outbound application traffic is redirected to.

    • DialedDirectly (bool: false) - Determines whether this proxy instance’s IP address can be dialed directly by transparent proxies. Typically transparent proxies dial upstreams using the “virtual” tagged address, which load balances across instances. Dialing individual instances can be helpful in cases like stateful services such as a database cluster with a leader.

  • MeshGateway (MeshGatewayConfig: <optional>) - Controls the default mesh gateway configuration for this service. Added in v1.6.0.

    • Mode (string: "") - One of none, local, or remote.
  • ExternalSNI (string: "") - This is an optional setting that allows for the TLS SNI value to be changed to a non-connect value when federating with an external system. Added in v1.6.0.

  • Expose (ExposeConfig: <optional>) - Controls the default expose path configuration for Envoy. Added in v1.6.2.

    Exposing paths through Envoy enables a service to protect itself by only listening on localhost, while still allowing non-Connect-enabled applications to contact an HTTP endpoint. Some examples include: exposing a /metrics path for Prometheus or /healthz for kubelet liveness checks.

    • Checks (bool: false) - If enabled, all HTTP and gRPC checks registered with the agent are exposed through Envoy. Envoy will expose listeners for these checks and will only accept connections originating from localhost or Consul’s advertise address. The port for these listeners are dynamically allocated from expose_min_port to expose_max_port. This flag is useful when a Consul client cannot reach registered services over localhost. One example is when running Consul on Kubernetes, and Consul agents run in their own pods.

    • Paths (array<Path>: []) - A list of paths to expose through Envoy.

      • Path (string: "") - The HTTP path to expose. The path must be prefixed by a slash. ie: /metrics.

      • LocalPathPort (int: 0) - The port where the local service is listening for connections to the path.

      • ListenerPort (int: 0) - The port where the proxy will listen for connections. This port must be available for the listener to be set up. If the port is not free then Envoy will not expose a listener for the path, but the proxy registration will not fail.

      • Protocol (string: "http") - Sets the protocol of the listener. One of http or http2. For gRPC use http2.

ACLs

Configuration entries may be protected by ACLs.

Reading a service-defaults config entry requires service:read on the resource.

Creating, updating, or deleting a service-defaults config entry requires service:write on the resource.