๐Ÿ—๏ธ Architecture

Sidecar

Sidecar Pattern

Places an auxiliary container next to the main container. The basis of the service mesh.

๐Ÿ“– Detailed Explanation

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:

  • Same Pod: In Kubernetes, the main container and the sidecar are placed in the same Pod
  • localhost communication: They share the same network namespace and communicate over localhost
  • Volume sharing: The file system can be shared through emptyDir and similar volumes
  • Independent deployment: Updating the sidecar does not require changing the main app code

Key use cases:

  • Service Mesh Proxy: Istio Envoy, Linkerd - traffic routing, mTLS, observability
  • Log collection: Fluentd, Filebeat - send log files to a central logging system
  • Configuration synchronization: Consul Template, Confd - detect and apply configuration changes
  • Secret injection: Vault Agent - inject secrets securely
  • Proxy: OAuth2 Proxy, Ambassador - handle authentication/authorization

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.

๐Ÿ’ป Code Example

Log Collection Sidecar (Fluentd)

# 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 Agent Sidecar (Secret Injection)

# 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

Istio Envoy Sidecar (Automatic Injection)

# 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๋กœ

๐Ÿ—ฃ๏ธ Examples of Practical Conversations

Architecture Review

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."

Technical Interview

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."

Troubleshooting

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

โš ๏ธ Cautions

๐Ÿ”— Related Terms

Service Mesh Istio Envoy Ambassador Pattern Init Container mTLS

๐Ÿ“š Learn More