Skip to content
Go back

Week 9: Service Mesh with Linkerd

Edit page

🐳 Docker 360 β€” Week 9 of 16 | Service mesh with Linkerd β€” sidecars, mTLS & observability on k3d

🎯 Learning Objective

By the end of this week, you will understand what a service mesh actually is and why microservices need one. You will see how the sidecar pattern adds encryption, retries and metrics to your services without changing a single line of application code β€” and you will install Linkerd on a lightweight Kubernetes cluster running entirely inside Docker (k3d), then prove mTLS and traffic splitting work with your own hands.

🎨 Visual Diagrams

Without a service mesh β€” every app fends for itself:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    plain HTTP     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    plain HTTP     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ service Aβ”‚ ────────────────▢ β”‚ service Bβ”‚ ────────────────▢ β”‚ service Cβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  no encryption   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  no retries,      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              no metrics                    no timeouts,
                                            failures cascade

Iska matlab ye hai ke β€” har service ko khud encryption, retry logic aur monitoring likhni parti hai. Har language me dobara. Bohat toil.

With a service mesh β€” a tiny proxy (sidecar) sits next to every service:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚             Pod A              β”‚          β”‚             Pod B              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚   mTLS   β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ service A │──▢│  proxy   │──┼─────────▢┼─▢│  proxy   │──▢│ service B β”‚  β”‚
β”‚  β”‚  (app)    β”‚   β”‚ (sidecar)β”‚  β”‚ encryptedβ”‚  β”‚ (sidecar)β”‚   β”‚  (app)    β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚          β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–² app code unchanged β€” the proxy handles security & observability

Linkerd architecture β€” control plane vs data plane:

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚          CONTROL PLANE           β”‚
                    β”‚   identity Β· destination Β· policy β”‚
                    β”‚   (hands out configs + mTLS certs)β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β”‚ config + certificates
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                            β–Ό                            β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ proxy +   │◀─── mTLS ────▢│ proxy +   │◀──── mTLS ────▢│ proxy +   β”‚
  β”‚ app       β”‚   DATA PLANE  β”‚ app       β”‚   DATA PLANE   β”‚ app       β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🧠 Theory Deep Dive

What is a service mesh, really?

Jab aap ke paas 3–4 services hain, to un ke darmiyan baat-cheet simple hai. Jab 30–40 services hain β€” kuch Python me, kuch Go me, kuch Node me β€” to sawal paida hote hain:

Simple lafzon me: a service mesh is a dedicated infrastructure layer that handles service-to-service communication β€” security, reliability, observability β€” so your application code doesn’t have to.

The sidecar pattern

Sidecar ka concept bilkul motorcycle wali sidecar jaisa hai β€” chhoti si extra seat jo main sawari ke saath jurri hoti hai. Kubernetes me har Pod ke andar aap ke app container ke saath ek lightweight proxy container inject hota hai. App ko lagta hai wo seedha dusri service se baat kar raha hai; asal me uska saara traffic proxy se guzarta hai.

ResponsibilityApp containerSidecar proxy
Business logicβœ…βŒ
mTLS encryptionβŒβœ…
Retries & timeoutsβŒβœ…
Metrics & tracing headersβŒβœ…
Traffic shifting (canary)βŒβœ…

Yahi woh jagah hai jahan magic hota hai β€” aap ne app ka ek line bhi nahi badla, phir bhi usko enterprise-grade networking mil gayi.

mTLS β€” encryption without code changes

mTLS (mutual TLS) ka matlab hai ke dono taraf ek dusre ki identity verify karte hain β€” client server ko, server client ko. Service mesh ka control plane har proxy ko short-lived certificates deta hai aur unhe automatically rotate karta hai.

Iska faida? Aap ka http://service-b/ wala plain request Pod se bahar nikalte hi encrypted ho jata hai, aur dusri taraf sirf verified services hi connect kar sakti hain. Zero-trust networking β€” bina ek line TLS code likhe.

Observability for free β€” the golden signals

Jab saara traffic proxy se guzarta hai, to proxy har request ko measure kar sakta hai. Isi liye mesh lagate hi aap ko ye golden signals mil jate hain:

SignalSawal jiska jawab milta hai
LatencyRequest me kitna time laga? p50/p95/p99 kitna hai?
TrafficKitni requests per second aa rahi hain?
ErrorsKitni requests fail ho rahi hain?
SaturationService kitni busy hai?

Why Linkerd? Why k3d?

Service mesh ki duniya me Istio sab se mashhoor hai, lekin wo bhaari hai β€” seekhne ke liye ideal nahi. Linkerd isko β€œultralight” rakhta hai: chhota, tez, aur samajhne me aasaan. Concepts wahi hain jo har mesh me kaam aate hain.

Aur k3d kya hai? Ye k3s (lightweight Kubernetes) ko Docker containers ke andar chalata hai. Iska matlab ye hai ke poora Kubernetes cluster aap ke Docker daemon ke andar sirf containers ki shakal me chal raha hai β€” Docker 360 ke theme ke saath perfect match. Kubernetes seekhne ka sab se halka tareeqa.

ComponentRole
Control planeConfig, mTLS certificates, policy β€” decisions leta hai
Data planeSidecar proxies β€” asal traffic handle karte hain

πŸ’» Hands-On Lab

Prerequisites: Docker, kubectl, aur k3d installed. k3d install: curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash

Step 1 β€” Create a k3d cluster (Kubernetes inside Docker!)

k3d cluster create docker360 --agents 1

βœ… Expected result: Cluster 'docker360' created successfully! β€” aur docker ps me aap ko k3d-docker360-server-0 jaise containers nazar aayenge. Poora cluster Docker ke andar!

Step 2 β€” Install the Linkerd CLI

curl --proto '=https' --tlsv1.2 -sSfL https://run.linkerd.io/install | sh
export PATH=$PATH:~/.linkerd2/bin
linkerd version

βœ… Expected result: Client version: stable-X.Y.Z print hota hai.

Step 3 β€” Pre-flight check

linkerd check --pre

βœ… Expected result: Saari lines √. Ye confirm karta hai ke cluster Linkerd ke liye tayyar hai.

Step 4 β€” Install Linkerd control plane

linkerd install --crds | kubectl apply -f -
linkerd install | kubectl apply -f -

Ye do commands CRDs (custom resource definitions) aur phir control plane install karti hain. Thora waqt lagega β€” certificates generate hote hain.

Step 5 β€” Verify the installation

linkerd check

βœ… Expected result: Har check √ β€” linkerd-identity, linkerd-destination waghera sab running hain. Control plane tayyar!

Step 6 β€” Install the viz extension (observability dashboard)

linkerd viz install | kubectl apply -f -
linkerd viz check

Step 7 β€” Deploy the emojivoto demo app

kubectl apply -f https://run.linkerd.io/emojivoto.yml
kubectl -n emojivoto get pods

Emojivoto ek cute demo app hai β€” emoji voting wali microservices app.

Step 8 β€” Inject the Linkerd sidecars ✨

kubectl -n emojivoto get deploy -o yaml \
  | linkerd inject - \
  | kubectl apply -f -

Dekho kitna simple hai β€” hum ne YAML nikala, linkerd inject se har Pod me proxy container add karwaya, aur wapas apply kar diya. App ka code zero change.

Step 9 β€” Prove the mesh is working

linkerd viz stat -n emojivoto deploy

βœ… Expected result: Har deployment ke saamne MESHED 1/1 β€” matlab har Pod me proxy chal raha hai.

Ab mTLS verify karte hain:

linkerd viz edges deploy -n emojivoto

βœ… Expected result: Edges ki list me connections TLS ke saath marked β€” iska matlab ye hai ke service-to-service traffic encrypted hai, bina app ne kuch kiye!

Step 10 β€” Open the app and the dashboard

kubectl -n emojivoto port-forward svc/web-svc 8080:80 &

Browser me http://localhost:8080 kholo β€” emoji vote karo, traffic generate karo. Phir dashboard dekho:

linkerd viz dashboard &

Golden signals live nazar aayenge β€” request rate, latency, success rate. πŸŽ‰

Step 11 β€” Lab: traffic splitting (canary release)

Ab asli power dekhte hain. Hum do versions deploy karenge aur traffic 90/10 split karenge β€” bina app restart kiye.

Pehle ek namespace banao jahan auto-injection on ho:

kubectl create namespace split-demo
kubectl annotate namespace split-demo linkerd.io/inject=enabled

Do versions deploy karo (file: whoami.yaml):

cat > whoami.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: whoami-v1
  namespace: split-demo
spec:
  replicas: 1
  selector:
    matchLabels: {app: whoami, version: v1}
  template:
    metadata:
      labels: {app: whoami, version: v1}
    spec:
      containers:
      - name: whoami
        image: hashicorp/http-echo:1.0
        args: ["-text=v1-response", "-listen=:5678"]
        ports: [{containerPort: 5678}]
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: whoami-v2
  namespace: split-demo
spec:
  replicas: 1
  selector:
    matchLabels: {app: whoami, version: v2}
  template:
    metadata:
      labels: {app: whoami, version: v2}
    spec:
      containers:
      - name: whoami
        image: hashicorp/http-echo:1.0
        args: ["-text=v2-response", "-listen=:5678"]
        ports: [{containerPort: 5678}]
---
apiVersion: v1
kind: Service
metadata:
  name: whoami-v1
  namespace: split-demo
spec:
  selector: {app: whoami, version: v1}
  ports: [{port: 80, targetPort: 5678}]
---
apiVersion: v1
kind: Service
metadata:
  name: whoami-v2
  namespace: split-demo
spec:
  selector: {app: whoami, version: v2}
  ports: [{port: 80, targetPort: 5678}]
---
apiVersion: v1
kind: Service
metadata:
  name: whoami-svc
  namespace: split-demo
spec:
  selector: {app: whoami}
  ports: [{port: 80, targetPort: 5678}]
EOF
kubectl apply -f whoami.yaml

Ab TrafficSplit rule lagao:

cat > trafficsplit.yaml <<'EOF'
apiVersion: split.smi-spec.io/v1alpha2
kind: TrafficSplit
metadata:
  name: whoami-split
  namespace: split-demo
spec:
  service: whoami-svc
  backends:
  - service: whoami-v1
    weight: 900m
  - service: whoami-v2
    weight: 100m
EOF
kubectl apply -f trafficsplit.yaml

Yahan trick ye hai: TrafficSplit mesh ke andar kaam karta hai β€” is liye hum cluster ke andar se hi ek meshed client chalayenge:

kubectl -n split-demo run curl-client --image=curlimages/curl \
  --restart=Never -it --rm -- sh -c \
  'for i in $(seq 1 20); do curl -s http://whoami-svc/; echo; done | sort | uniq -c'

βœ… Expected result: taqreeban 18 v1-response aur 2 v2-response β€” 90/10 split kaam kar raha hai! Yehi canary releases ka foundation hai.

Step 12 β€” Cleanup

k3d cluster delete docker360

βœ… Checkpoint

Q1: Sidecar pattern me proxy kahan chalta hai? A: Har Pod ke andar, app container ke saath ek alag container ki shakal me. App ka traffic transparently us se guzarta hai.

Q2: mTLS normal TLS se kaise mukhtalif hai? A: Normal TLS me sirf client server ko verify karta hai; mTLS me dono taraf ek dusre ko certificates se verify karte hain β€” is liye sirf trusted services hi aapas me baat kar sakti hain.

Q3: Linkerd ka control plane aur data plane me kya farq hai? A: Control plane (identity, destination) config aur certificates deta hai β€” decisions leta hai. Data plane (sidecar proxies) asal request traffic handle karta hai.

Q4: TrafficSplit rule apply karne ke liye client ka meshed hona kyun zaroori hai? A: Kyun ke split ko enforce karne wala client-side ka Linkerd proxy hai. Agar caller ke paas proxy nahi, to rule parhne wala koi nahi β€” is liye hum ne cluster ke andar meshed curl pod use kiya.

πŸ”œ Next Week

Next week: API Gateway with Kong β€” edge pe routing, authentication aur rate limiting, sab Docker me.


Edit page
Share this post on:

Previous Post
Week 10: API Gateway with Kong
Next Post
Week 8: CI/CD Pipeline for Docker Apps