Sidecar
Sidecar Pattern
Places an auxiliary container next to the main container. The basis of the service mesh.
Sidecar Pattern
Places an auxiliary container next to the main container. The basis of the service mesh.
The Sidecar Pattern is a microservice design pattern that separates common functionality by placing an auxiliary container next to the main application container. Like a sidecar attached to a motorcycle, it shares its lifecycle with the main container and shares the network namespace and volumes.
Core concepts:
Key use cases:
Role in a Service Mesh:
A Service Mesh such as Istio or Linkerd automatically injects a sidecar proxy (such as Envoy) into every service Pod. This proxy intercepts all inbound/outbound traffic and provides mTLS encryption, load balancing, circuit breakers, and distributed tracing. Network-level features can be applied transparently, without modifying application code.
Kubernetes 1.28+ Sidecar Container: Previously, sidecars were also treated as regular containers, which made it difficult to control startup/shutdown order. The Sidecar Container (initContainers + restartPolicy: Always), which has been officially supported since 1.28, starts before the main container and terminates after it, enabling more stable lifecycle management.
# logging-sidecar.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-with-logging
spec:
containers:
# ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
- name: app
image: my-app:latest
volumeMounts:
- name: logs
mountPath: /var/log/app
resources:
limits:
memory: "512Mi"
cpu: "500m"
# Sidecar: ๋ก๊ทธ ์์ง (Fluentd)
- name: log-collector
image: fluent/fluentd:v1.16
volumeMounts:
- name: logs
mountPath: /var/log/app
readOnly: true
- name: fluentd-config
mountPath: /fluentd/etc
resources:
limits:
memory: "128Mi"
cpu: "100m"
volumes:
- name: logs
emptyDir: {}
- name: fluentd-config
configMap:
name: fluentd-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: fluentd-config
data:
fluent.conf: |
<source>
@type tail
path /var/log/app/*.log
pos_file /var/log/app/fluentd.pos
tag app.logs
<parse>
@type json
</parse>
</source>
<match app.logs>
@type elasticsearch
host elasticsearch.logging.svc
port 9200
index_name app-logs
</match>
# vault-sidecar.yaml
apiVersion: v1
kind: Pod
metadata:
name: app-with-vault
annotations:
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/role: "my-app-role"
vault.hashicorp.com/agent-inject-secret-db: "secret/data/db/config"
vault.hashicorp.com/agent-inject-template-db: |
{{ with secret "secret/data/db/config" -}}
export DB_HOST="{{ .Data.data.host }}"
export DB_USER="{{ .Data.data.username }}"
export DB_PASS="{{ .Data.data.password }}"
{{- end }}
spec:
serviceAccountName: my-app-sa
containers:
- name: app
image: my-app:latest
command: ["/bin/sh", "-c"]
args:
- source /vault/secrets/db && ./start.sh
volumeMounts:
- name: vault-secrets
mountPath: /vault/secrets
# Vault Injector๊ฐ ์๋์ผ๋ก Sidecar ์ถ๊ฐ
---
# Kubernetes 1.28+ Native Sidecar Container
apiVersion: v1
kind: Pod
metadata:
name: app-with-native-sidecar
spec:
initContainers:
# Native Sidecar: ๋ฉ์ธ๋ณด๋ค ๋จผ์ ์์, ๋์ค์ ์ข
๋ฃ
- name: vault-agent
image: hashicorp/vault:1.15
restartPolicy: Always # ์ด๊ฒ ํต์ฌ! Sidecar๋ก ๋์
args:
- agent
- -config=/etc/vault/agent.hcl
volumeMounts:
- name: secrets
mountPath: /vault/secrets
- name: vault-config
mountPath: /etc/vault
containers:
- name: app
image: my-app:latest
volumeMounts:
- name: secrets
mountPath: /vault/secrets
readOnly: true
volumes:
- name: secrets
emptyDir:
medium: Memory # RAM์ ์ ์ฅ (๋ณด์)
- name: vault-config
configMap:
name: vault-agent-config
# Namespace์ Istio injection ํ์ฑํ
kubectl label namespace my-app istio-injection=enabled
# Deployment ๋ฐฐํฌ ์ Envoy Sidecar ์๋ ์ฃผ์
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-service
namespace: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-service
template:
metadata:
labels:
app: my-service
# ํน์ Pod์์ Sidecar ๋นํ์ฑํ
# annotations:
# sidecar.istio.io/inject: "false"
spec:
containers:
- name: app
image: my-service:latest
ports:
- containerPort: 8080
# ๋ฐฐํฌ ํ Pod ํ์ธ - Envoy Sidecar๊ฐ ์๋ ์ถ๊ฐ๋จ
# kubectl get pods -n my-app
# NAME READY STATUS RESTARTS
# my-service-5d4f6c7b8-abc12 2/2 Running 0
# ^^^
# app + istio-proxy (Envoy)
# Istio VirtualService๋ก ํธ๋ํฝ ์ ์ด
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: my-service
spec:
hosts:
- my-service
http:
- route:
- destination:
host: my-service
subset: v1
weight: 90
- destination:
host: my-service
subset: v2
weight: 10 # Canary: 10% ํธ๋ํฝ์ v2๋ก
Senior: "Logging and tracing code is duplicated across every service. And the languages are different too..."
Developer: "How about separating it with the Sidecar pattern? If we collect logs with a Fluentd Sidecar and send traces with a Jaeger Agent Sidecar, we can take the infrastructure logic out of the app code."
Senior: "What about the resource overhead?"
Developer: "Each Sidecar takes about 50-100MB of memory. In exchange, the SDK initialization overhead in each app goes away, and version upgrades only require changing the Sidecar, so operations get easier."
Interviewer: "Please explain what the Sidecar Proxy does in Istio."
Candidate: "The Envoy Sidecar intercepts all inbound/outbound traffic. It redirects traffic to Envoy using iptables rules. It encrypts service-to-service communication with mTLS, and applies load balancing, circuit breakers, and retry policies. It also collects metrics and traces for every request to provide observability."
Interviewer: "What about a Service Mesh without a Sidecar?"
Candidate: "There are eBPF-based Service Meshes like Cilium. They handle traffic at the kernel level, so there is no Sidecar overhead. However, they require an eBPF-capable kernel, and L7 features may be more limited than Envoy's."
Operations team: "The Pod is Ready, but the app container can't make external API calls."
Developer: "It might be because of the Istio Sidecar. Envoy hasn't received its xDS configuration yet, so it can't route traffic. If you set holdApplicationUntilProxyStarts: true, you can delay the app container's startup until Envoy is ready."
Operations team: "Ah, so the Sidecar has to be ready first."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free