The sidecar pattern hands-on
Run Envoy in the app's network namespace.
The sidecar trick, in one diagram
A sidecar means: two containers in one network namespace. From the
inside, both containers see the same lo and eth0. The application can
reach the sidecar at localhost:<sidecar-port>; the sidecar can reach the
application at localhost:<app-port>.
The outside network only ever talks to the sidecar. The application is "insulated" — it knows nothing about TLS, retries, the upstream service mesh, or even the cluster topology. It just talks to localhost.
That insulation is the whole point. Every app in the fleet behaves identically on the network because they all delegate to identical sidecars.
In Docker (what you'll do today)
In Kubernetes the kubelet shares a network namespace across containers in
a pod automatically. In plain Docker, you do it explicitly with
network_mode: "service:<name>".
services:
app:
image: kennethreitz/httpbin
# The app exposes nothing directly — Envoy will publish the port.
envoy:
image: envoyproxy/envoy:v1.31-latest
network_mode: "service:app" # ← share the app's network namespace
volumes:
- ./envoy.yaml:/etc/envoy/envoy.yaml
# No `ports:` here either — port mapping must be on `app`,
# because that's the container that "owns" the network namespace.
Important consequence: any port published by app is the only way the
host reaches anything in the pod. So put the listener port mapping on
app, not on envoy:
app:
image: kennethreitz/httpbin
ports:
- "10000:10000" # for Envoy's listener
- "9901:9901" # for Envoy's admin
Reads weird the first time, but it's correct: the network namespace lives
on app, so port mapping has to live there too.
The Envoy config
Inside the sidecar, the upstream cluster points at 127.0.0.1:80 —
because that's where the application is, from the sidecar's point of
view.
admin:
address: { socket_address: { address: 0.0.0.0, port_value: 9901 }}
static_resources:
listeners:
- name: in
address: { socket_address: { address: 0.0.0.0, port_value: 10000 }}
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: sidecar_in
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
route_config:
virtual_hosts:
- name: app
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: local_app }
clusters:
- name: local_app
type: STATIC
connect_timeout: 0.5s
load_assignment:
cluster_name: local_app
endpoints:
- lb_endpoints:
- endpoint: { address: { socket_address: { address: 127.0.0.1, port_value: 80 }}}
httpbin listens on port 80 internally; the sidecar reaches it at
127.0.0.1:80 because they share lo.
Hands-on: run a sidecar pair
Drop the compose file and the envoy.yaml above into a folder, then:
docker compose up
In another terminal:
curl -s localhost:10000/get | head -5
You're hitting the sidecar (port 10000), which forwards to the app
(httpbin) over localhost:80 inside the shared namespace. The
host never sees a direct connection to the app.
Now break it on purpose: comment out
network_mode: "service:app" on the envoy service, restart,
and try again. Envoy will fail to connect to 127.0.0.1:80 —
because without the shared namespace, that 127.0.0.1 is now the Envoy
container's own loopback, and there's nothing on port 80 there.
Restoring the line fixes it. That experiment proves the sidecar trick better than any explanation.
Replace the docker-compose with two containers in one Kubernetes pod
spec, drop network_mode (pods automatically share a netns),
and you have what Istio injects into every mesh-enabled pod. Same
mechanic.
How Istio actually intercepts traffic
You don't configure the app to talk to Envoy. Istio's init container installs iptables rules that transparently redirect every inbound and outbound TCP packet to the sidecar:
The app never knows the sidecar is there — that's why you don't have to change a single line of code to join an Istio mesh.
Up next
You've now run an Envoy sidecar by hand. The final lesson zooms out: how does a real control plane (Istio) generate and push this config to hundreds of sidecars at once? That's xDS.
In the docker-compose sidecar example, why does the Envoy cluster point at `127.0.0.1:80` even though the app is in another container?