Skip to content
LATEST

🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.

Upgradix
Upgradix Code. Learn. Ship.

Zero-Trust Service-to-Service Traffic with Consul Connect

In traditional networks, we rely on a firewall at the edge. Once a request gets inside the private network, services trust each other by default. If an attacker compromises one small background worker, they can freely scan and call your payment API or database.

Zero-Trust networking flips this model completely: never trust, always verify. With HashiCorp Consul Connect, every microservice gets its own cryptographic identity, all service-to-service traffic is automatically encrypted with mutual TLS (mTLS), and explicit rules called intentions decide which service is allowed to talk to which.

What you will learn
  • Why IP-based firewalls break down in dynamic microservice environments.
  • How Consul Connect uses sidecar proxies and mTLS to encrypt traffic without changing your app code.
  • How intentions enforce allow/deny authorization rules between services.
  • Hands-On Lab: Register an upstream API and a downstream worker with Consul Connect proxies, verify encrypted traffic succeeds when allowed, and confirm requests fail when denied.
  • Real-world architectural trade-offs and senior-level interview questions.

1. Why Traditional Network Security Fails for Microservices

When you run a few static servers, writing IP-based firewall rules is manageable. In modern cloud and Kubernetes environments, containers spin up, scale out, and die every minute. IP addresses change constantly.

Consul Connect solves this by moving security from IP addresses to service identities. Instead of saying "allow 10.0.1.45 to call 10.0.2.18", you say "allow worker to call billing-api".

Feature Traditional Firewall Consul Connect (Zero-Trust)
Identity IP address and port Cryptographic X.509 certificate (SPIFFE ID)
Encryption Usually plain HTTP inside the VPC Automatic mutual TLS (mTLS) on every hop
Authorization Static CIDR/subnet rules Dynamic service-to-service intentions
App Code Changes Manual TLS cert management in code Zero code changes (handled by sidecar proxy)

2. Architecture: How mTLS and Intentions Work

You do not need to rewrite your Python, .NET, or Node.js apps to support mTLS. Instead, Consul runs a lightweight sidecar proxy next to each service instance. Your app simply talks to localhost, and the sidecar proxy handles certificate rotation, encryption, and authorization.

Zero-Trust Service-to-Service Flow with Consul Connect

Consul Control Plane Built-in CA (mTLS Certs) + Intentions Store Certs Certs + Rules Worker App Calls localhost:9191 (Plain HTTP) Worker Proxy Presents Cert Sidecar :9191 mTLS Encrypted API Proxy Checks Intention Allow / Deny Upstream API Listens :8080 (Localhost only) ALLOW: API Proxy forwards decrypted request to :8080 DENY: API Proxy drops connection immediately

Both proxies automatically receive short-lived TLS certificates from Consul. Traffic over the network is always encrypted, and the upstream proxy enforces allow/deny intentions before the request ever reaches your API.

3. Hands-On Lab: Setting Up Zero-Trust Traffic Locally

Let's build a working lab on your local machine. We will start a Consul dev agent, run a simple upstream HTTP API, register both an upstream API and a downstream worker with Consul Connect proxies, and test allow vs. deny intentions.

Prerequisites: Install the consul CLI (v1.15+) and Python 3 (to spin up a quick local API server). Everything runs locally on your machine with no cloud account needed.

Step 1: Start the Consul Agent in Dev Mode

Open Terminal 1 and start Consul. Dev mode automatically enables Consul Connect and spins up a built-in Certificate Authority (CA).

Terminal 1: Start Consul

consul agent -dev -ui

Expected output

==> Starting Consul agent...
           Version: '1.19.x'
         Node name: 'dev-node'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
==> Consul agent running!

Step 2: Start the Upstream API Service

Open Terminal 2 and start a lightweight HTTP server on port 8080. Notice we bind it to 127.0.0.1 so external callers cannot bypass the sidecar proxy.

Terminal 2: Run the upstream API on localhost:8080

python3 -m http.server 8080 --bind 127.0.0.1

Expected output

Serving HTTP on 127.0.0.1 port 8080 (http://127.0.0.1:8080/) ...

Step 3: Register the Upstream API and Downstream Worker

Create two service definition files. The billing-api registers a Connect sidecar proxy. The worker service also registers a sidecar proxy and declares billing-api as an upstream bound to local port 9191.

File 1: billing-api.hcl

service {
  name = "billing-api"
  port = 8080

  connect {
    sidecar_service {}
  }
}

File 2: worker.hcl

service {
  name = "worker"
  port = 8081

  connect {
    sidecar_service {
      proxy {
        upstreams = [
          {
            destination_name = "billing-api"
            local_bind_port  = 9191
          }
        ]
      }
    }
  }
}

Now register both services with Consul in Terminal 3:

Terminal 3: Register services

consul services register billing-api.hcl
consul services register worker.hcl

Expected output

Registered service: billing-api
Registered service: worker

Step 4: Start the Consul Connect Sidecar Proxies

Consul includes a built-in L4 proxy that is perfect for local testing (in production, you typically use Envoy via consul connect envoy). Start one proxy for each service in separate terminals.

Terminal 4: Start the billing-api sidecar proxy

consul connect proxy -sidecar-for billing-api

Terminal 5: Start the worker sidecar proxy

consul connect proxy -sidecar-for worker

Expected output (in both proxy terminals)

==> Consul Connect proxy starting...
    Configuration mode: Agent API
        Sidecar for ID: billing-api
              Proxy ID: billing-api-sidecar-proxy
==> Proxy loaded config and ready to serve

Step 5: Verify Encrypted Traffic Succeeds When Allowed

Let's create an explicit allow intention so worker is authorized to call billing-api, then send a request through the worker's local upstream port (9191).

Terminal 3: Create an ALLOW intention and test with curl

# Allow worker -> billing-api
consul intention create -allow worker billing-api

# Send a request through the worker sidecar proxy
curl -i http://127.0.0.1:9191

Expected output

Created: worker => billing-api (allow)

HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.0
Content-type: text/html; charset=utf-8
...

Your request traveled from curl to the worker proxy on port 9191, was encrypted with mTLS over the network to the billing-api proxy, passed the intention check, and reached the upstream API on port 8080.

Step 6: Confirm Requests Fail When Denied

Now let's flip the intention to deny and run the exact same curl command. Notice we do not restart any services or proxies—Consul pushes the updated rule in real time.

Terminal 3: Switch intention to DENY and re-test

# Replace the rule with DENY
consul intention create -replace -deny worker billing-api

# Attempt the same request
curl -i http://127.0.0.1:9191

Expected output

Created: worker => billing-api (deny)

curl: (52) Empty reply from server
Watch out: By default in Consul dev mode without ACLs enabled, the default intention policy is allow. In production, always enable Consul ACLs with default_policy = "deny" so that every new service is locked down until you explicitly grant it permission.

4. Step-by-Step Execution Breakdown

Here is what happened under the hood during that single curl command:

Step 1: curl sends a plain HTTP request to 127.0.0.1:9191, which is the worker's local sidecar proxy.
Step 2: The worker proxy looks up healthy instances of billing-api in Consul and opens a TLS connection to the billing-api sidecar proxy.
Step 3: Both proxies exchange short-lived X.509 certificates signed by Consul's CA. This proves the caller really is worker and the destination really is billing-api.
Step 4: The billing-api proxy checks the intention rules. If allow, it decrypts the traffic and forwards it to 127.0.0.1:8080. If deny, it immediately terminates the connection.

5. Hands-On Practice

Try these exercises on your local setup to reinforce what you just built.

Beginner Exercise 1: Inspect Intentions via CLI

List all active intentions and check whether worker is currently allowed to call billing-api without sending an HTTP request.

Hint: Run consul intention list and consul intention check worker billing-api.

Intermediate Exercise 2: Add a Third Service

Register a new service called analytics with an upstream pointing to billing-api on port 9192. Set a global deny rule (* => billing-api), then allow only worker.

Expected result: Requests from worker succeed (HTTP 200), while requests from analytics are rejected.

Advanced Exercise 3: Upgrade to L7 HTTP Intentions with Envoy

Switch from the built-in L4 proxy to Envoy and configure a service-intentions config entry that allows GET /health but denies POST /charge.

Hint: Set the service protocol to http in a service-defaults config entry and apply L7 rules using consul config write.

6. Interview Questions

If you are interviewing for a Senior Backend, DevOps, or Cloud Architect role, expect questions like these on Service Mesh and Zero-Trust:

Q1. Why do we use sidecar proxies for mTLS instead of implementing TLS directly inside each microservice?

Answer: Implementing mTLS in application code means every language (Python, .NET, Go, Node.js) needs its own TLS library, certificate rotation logic, and authorization checks. A sidecar proxy centralizes encryption, automatic short-lived cert rotation, and intention enforcement outside the app, keeping business code clean and security policies consistent.

Q2. What happens to existing service traffic if the Consul control plane goes down temporarily?

Answer: Existing data-plane traffic continues to flow. Sidecar proxies cache their current TLS certificates, service endpoints, and intention rules locally. As long as the cached certificates have not expired, services keep communicating until the control plane recovers.

Q3. How do L4 intentions differ from L7 intentions in Consul Connect?

Answer: L4 (TCP) intentions are binary: they either allow or deny all connections between Service A and Service B. L7 (HTTP/gRPC) intentions inspect application-level traffic via Envoy, letting you allow specific HTTP methods, URL paths, or headers (for example, allowing GET /orders while blocking DELETE /orders).

Conclusion

Zero-Trust does not have to mean rewriting your entire microservice architecture. By pairing Consul Connect sidecar proxies with mTLS and explicit intentions, you eliminate implicit network trust and protect east-west traffic automatically.

Start by locking down your most sensitive services—like billing, authentication, or user data APIs—with a default-deny posture, and expand your mesh one service at a time.

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Discussion & Comments

0
Guest mode · Login / Register
Post directly as a guest or log in first.

No comments yet

Start the discussion above — ask a question, share a tip, or reply to a comment.

This article is protected. Printing and saving as PDF are disabled.