Network
Service mesh with Istio
A proxy beside every app
Some things every app needs when it calls another: encryption, proof of who is calling, retries, traffic splits, measurements. A service mesh takes them out of the code. It puts a proxy next to each app, sends all the app's traffic through it, and runs one control plane that tells every proxy what to do. Here is how Istio does it, from the first injected proxy to a mesh with no sidecars at all.
- Data plane
- Envoy proxies · carry the traffic
- Control plane
- istiod · configures the proxies
- The apps
- web calls api · code unchanged
- Version
- Istio 1.32
Read the whole lesson as text
1. Every app on its own
web calls api, the way Pods have called each other in every lesson so far. It works, but look at what's missing. The request crosses the network in plain text. api knows its caller only by an IP address. When api answers with an error, web's code has to decide whether to try again. And nobody counts those errors unless someone wrote code for it. You could build all of that into web, then again into api, in every language your teams use. A service mesh does it once, outside the apps.
- web calls api. Anyone on the path between the nodes can read the request.
- api answers 503. Whether to try again is web's problem.
2. Install the control plane
A mesh has two halves. The data plane is the proxies, which carry the traffic. The control plane is istiod, which configures them. You install istiod: a Deployment like any other, here one Pod on node-1. It also registers a webhook, so the API server asks it about every new Pod in the namespaces you choose. You choose one by labelling it. Nothing that's already running changes.
- You install Istio. Its control plane is one Deployment, istiod.
- It also registers a webhook, so the API server will ask istiod about new Pods.
- You label the default namespace. The Pods already running there stay as they are.
3. A proxy is added
To join the mesh, Pods have to be created again, so you restart the Deployments. The ReplicaSet controller asks for an ordinary one-container Pod. Before saving it, the API server sends it to istiod, and istiod answers with a patch that adds two things: istio-init, which sets up the network once and exits, and istio-proxy, an Envoy proxy that stays. Your Deployment doesn't mention either. The change is made to each Pod, on its way in.
- You restart web and api, so their Pods are created again.
- A new web Pod is asked for. Before saving it, the API server shows it to istiod.
- istiod answers with a patch that adds a setup container and a proxy.
- The patched Pods start and replace the old ones. Each one now has two containers.
4. Every connection, redirected
This is what's inside the two Pods now. The app doesn't know about the proxy, so something has to send its traffic there. istio-init does it with iptables rules in the Pod's own network namespace: TCP connections going out are redirected to port 15001, connections coming in to 15006, and Envoy listens on both. One exception keeps it from looping: whatever the proxy itself sends, recognized by its user ID 1337, goes straight out.
- istio-init runs first. It writes the redirect rules, then exits.
- istio-proxy starts next, and the app waits until it's ready.
5. An identity for each Pod
Before any traffic, the proxy needs to prove who it is. It makes a private key and sends istiod a signing request, along with the Pod's ServiceAccount token. istiod has the API server check the token, then signs a certificate that names the ServiceAccount: spiffe://cluster.local/ns/default/sa/web. The key never leaves the Pod. The certificate is good for 24 hours and replaced around the halfway mark, with no restart.
- The proxy asks istiod for a certificate, with the Pod's token as proof.
- istiod signs a certificate that names the ServiceAccount, good for a day.
6. Configuration, pushed
An Envoy proxy starts out knowing nothing. istiod watches the API server for Services, endpoints and Istio's own objects, works out what each proxy needs, and sends it down a stream that stays open. The protocol is called xDS. When something changes, istiod waits 100 ms in case more is coming, then pushes the parts it affects. No request goes through istiod, so if it stops, calls keep working with the last configuration.
- istiod sends the proxy a map of the whole cluster: what exists and how to reach it.
- A new api Pod becomes ready. istiod pushes one small update, not the whole map.
7. The call goes out
web opens a connection to api's Service address, 10.96.45.10:8080, as it always did. The redirect hands it to Envoy on 15001, and Envoy asks the kernel where it was really going. It reads the HTTP request, matches the Host header to a route, and picks a Pod itself, so the Service IP is never dialed and kube-proxy isn't involved. Then the two proxies open a TLS connection in which both show a certificate. That's mutual TLS, or mTLS.
- web connects to the api Service. The rules send the connection to the proxy.
- Envoy finds the original address, reads the request, and picks an api Pod itself.
- web's proxy opens a TLS connection straight to the api Pod.
- Each proxy checks the other's certificate. web proves it's web, api proves it's api.
- The request crosses the network encrypted. The proxy's own packets aren't redirected.
8. The call comes in
The packets arrive for port 8080, and the api Pod's rules send them to Envoy on 15006. Envoy decrypts, knows from the certificate that this is web, checks its access rules, and passes the request to the app over localhost as plain HTTP. api sees a connection from 127.0.0.6 and a header that says who called. On the way back, both proxies write down what happened, so you get the same metrics for every app without touching any of them.
- The api Pod's rules send what arrives on 8080 to its proxy.
- The proxy decrypts the request, notes the caller, and passes it to the app.
- api answers, and the reply goes back the same way. web gets a plain 200.
- Both proxies count the call, with names on it. Prometheus collects that from port 15020.
9. Split and retry
Now the proxies can do things you'd otherwise code. You apply two objects. A DestinationRule names groups of Pods by label: v1 and v2. A VirtualService sends 90% of requests to v1, 10% to v2, and retries failures twice. istiod turns them into Envoy config and pushes it to the callers. One thing surprises people: Envoy picks the group first, and retries stay inside it. A retry can step around one broken Pod, but if all of v2 is broken, its 10% still fails.
- You apply both objects. 100 ms later the new routes are in web's proxy.
- About one request in ten goes to v2. The other nine go to v1.
- Now one of the two v1 Pods is broken, and a request lands on it.
- It answers 503. The proxy keeps that to itself and waits a few milliseconds.
- The second try goes to the other v1 Pod and works. web only ever sees the 200.
- When v2 itself is broken, its share still fails: retries stay within v2.
10. Who may call
Since every caller has a proven identity, you can write rules about it. Two objects do that. PeerAuthentication STRICT says: refuse anything that isn't mTLS. AuthorizationPolicy says: only sa/web may call api, and only with GET on /orders. api's own proxy enforces both, right before the app. A caller in the mesh with the wrong identity gets a 403. A caller without a proxy has no identity at all, and its connection is cut.
- You apply both policies, and istiod pushes them to the proxy they protect.
- busybox is in the mesh too. Its certificate is valid, but it says sa/default.
- api's proxy answers 403 itself. The request never reaches the app.
- Started again without a sidecar, busybox sends plain HTTP. The connection is reset.
11. A mesh without sidecars
A proxy in every Pod has a price: each one reserves 100m CPU and 128Mi, and adding or upgrading it means restarting Pods. Istio's ambient mode splits the job in two. A ztunnel on each node does encryption, identity and connection-level rules for all its Pods. Where you also need HTTP features such as splits and retries, you add a waypoint, an Envoy shared by a whole Service or namespace. You label the namespace and the running Pods join as they are.
- You label the namespace. On each node, istio-cni sets up the redirect inside the running Pods.
- web calls api as before. This time the connection goes to the node's ztunnel.
- ztunnel carries it to the other node in an mTLS tunnel, on port 15008.
- The other ztunnel opens the tunnel and hands the bytes to api.
- For HTTP rules you add a waypoint. ztunnel sends api's traffic there first.
- The waypoint applies the rules, then sends the request on to api's node.
Summary
A service mesh is two simple parts. Proxies carry every call, so they can encrypt it, prove who made it, retry it, split it and count it. A control plane turns your objects into configuration and certificates for those proxies, and stays out of the traffic. The apps didn't change. In return you run more proxies, and one more system that can be configured wrong, so a mesh pays off when many services need the same things.
Next lesson: Starting a container