π³ 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:
- Service A ko service B se encrypted baat karni hai β har language me TLS code kaun likhega?
- Service B slow hai β timeout aur retry ka logic har app me copy-paste karenge?
- Production me request fail hui β kis service ne der lagayi, ye kaise pata chalega?
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.
| Responsibility | App container | Sidecar 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:
| Signal | Sawal jiska jawab milta hai |
|---|---|
| Latency | Request me kitna time laga? p50/p95/p99 kitna hai? |
| Traffic | Kitni requests per second aa rahi hain? |
| Errors | Kitni requests fail ho rahi hain? |
| Saturation | Service 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.
| Component | Role |
|---|---|
| Control plane | Config, mTLS certificates, policy β decisions leta hai |
| Data plane | Sidecar proxies β asal traffic handle karte hain |
π» Hands-On Lab
Prerequisites: Docker,
kubectl, aurk3dinstalled. 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!β aurdocker psme aap kok3d-docker360-server-0jaise 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.Zprint 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-destinationwaghera sabrunninghain. 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
TLSke 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-responseaur2 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.