Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Service Mesh

Service Mesh Concepts with Consul

Learn what a service mesh is, how Consul service mesh secures service-to-service traffic with sidecar proxies, mutual TLS and identity certificates.

So far you have used Consul to find services and check their health. A service mesh adds a second job: controlling and securing the traffic between those services. Instead of each application opening plain connections to its neighbors, every call travels through a small proxy that encrypts it, proves who is calling and enforces rules about who may call whom.

Consul calls its mesh feature service mesh (older docs say "Connect"). This lesson explains the moving parts. The product itself cannot run on this site, so the commands and configuration files are shown as read-only, and the runnable examples are small Python stand-ins that model the same ideas.

The problem a mesh solves

Imagine a web service calling an api service. On a flat network you face three questions:

  • Is the traffic private? Anyone who can sniff the network can read it unless every app implements TLS correctly.
  • Is the caller who it claims to be? An IP address is a weak identity, especially when containers move around.
  • Is the call allowed? A compromised service should not be able to reach every other service.

A mesh answers all three outside your application code, so every language and framework gets the same behavior.

The data plane and the control plane

flowchart LR
  subgraph Node A
    W["web app"] --> WP["sidecar proxy"]
  end
  subgraph Node B
    AP["sidecar proxy"] --> A["api app"]
  end
  WP -- "mutual TLS" --> AP
  CS[("Consul servers: catalog, CA, intentions")] -. "config and certificates" .-> WP
  CS -. "config and certificates" .-> AP

The proxies carry the traffic (data plane); the Consul servers tell them what to do (control plane).

  • The data plane is the set of proxies. Consul ships with a simple built-in proxy for development and supports Envoy for production. Each application instance gets its own sidecar proxy running next to it.
  • The control plane is Consul itself. The servers hold the service catalog, a certificate authority (CA) and the access rules. Agents push the right configuration and certificates to each proxy.

Your app talks to its local sidecar over localhost; the sidecar talks to the remote sidecar over the network; the remote sidecar hands the request to the real service, also over localhost.

Identity: certificates, not IP addresses

When a proxy starts, Consul issues it a leaf certificate signed by the mesh CA. The certificate carries a SPIFFE ID, a URI that names the service:

TEXT
spiffe://11111111-2222-3333-4444-555555555555.consul/ns/default/dc/dc1/svc/web

Read it from the right: service web, in datacenter dc1, namespace default (Enterprise feature; OSS always shows default), under a trust domain unique to the cluster. Because the certificate is signed by the CA, the other side can verify it cryptographically. This is mutual TLS (mTLS): both sides present certificates, so each knows exactly who it is talking to.

Certificates are short-lived (three days by default) and rotated automatically; your applications never see a key.

Python Stand-in: build and parse a SPIFFE ID

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Authorization: intentions

Encrypting traffic is not enough. Intentions are Consul's access rules: "web may call api", "* may not call database". The receiving proxy checks the caller's certificate identity against the intentions before it forwards a single byte. Intentions are covered in detail in Intentions.

Registering a service for the mesh

A service joins the mesh by declaring a sidecar in its registration. This is a service definition file loaded by the agent:

HCL
service {
  name = "api"
  port = 9090

  connect {
    sidecar_service {}
  }
}

The empty sidecar_service {} block asks Consul to register a second service called api-sidecar-proxy with sensible defaults (a port from the sidecar range, a TCP check on the proxy and a link back to api). Then you start the proxy:

Terminal
consul connect envoy -sidecar-for api

Envoy must be installed separately; consul connect envoy generates its bootstrap configuration and launches it. To try the built-in development proxy instead you could run consul connect proxy -sidecar-for api, but it is not meant for production.

To use the mesh you also need the gRPC xDS port enabled on the agent, which is on by default in current releases for ports.grpc (8502) and ports.grpc_tls (8503). Check your version's configuration reference if the proxy cannot connect.

Service mesh must be turned on in the servers' configuration for older releases with connect { enabled = true }. In current releases it is enabled by default; setting it explicitly does no harm.

What the mesh does and does not do

Capability Provided by
Encryption in transit between services Sidecar mTLS
Service identity Leaf certificates from the CA
Who may call whom Intentions
Retries, timeouts, circuit breaking Proxy configuration (proxy-defaults, service-defaults)
Traffic splitting, routing Config entries: router, splitter, resolver
Authentication of end users Not the mesh: your application or an API gateway

The mesh authenticates services, not people. Do not treat an allowed intention as proof that a human user is authorized.

Python Stand-in: default-deny between services

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Calling the remote service directly. The app must dial its local upstream port on localhost, not the remote address, or it bypasses the mesh.
  • Forgetting the sidecar process. Registering sidecar_service {} only creates the catalog entry. Something must run the proxy, or the check on api-sidecar-proxy fails.
  • Expecting the mesh to fix slow services. A sidecar adds a small hop of latency; it does not make an overloaded service faster.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which part of the mesh actually carries application traffic?

    Proxies form the data plane. The servers are the control plane and only distribute configuration, certificates and rules.
  2. 2. What does the SPIFFE ID in a leaf certificate identify?

    The URI names the service identity, which proxies use to evaluate intentions.
  3. 3. Why does mTLS beat checking client IP addresses?

    Certificates travel with the service identity even when containers move between hosts and IPs change.
  4. 4. What does registering connect { sidecar_service {} } do on its own?

    It adds the catalog registration. A process such as consul connect envoy -sidecar-for api must actually run.

Summary

  • A service mesh secures and controls service-to-service traffic with sidecar proxies (data plane) managed by Consul (control plane).
  • Each service gets a short-lived leaf certificate whose SPIFFE ID names it; mTLS makes both sides prove identity.
  • Intentions decide who may call whom; the mesh authenticates services, not end users.
  • A mesh service declares connect { sidecar_service {} } and runs a proxy such as Envoy next to it.

Next: Sidecar Proxies and Upstreams