DEV Community

Avesh
Avesh

Posted on

2

Kubernetes Multi-Container Pods: Sidecar, Adapter and Ambassador Patterns

Kubernetes pods are the smallest deployable units, and while most applications use single-container pods, Kubernetes also supports multi-container pods. These pods house multiple containers that share resources and interact closely to achieve a common goal.

In this article, we explore multi-container pods and three important design patterns: Sidecar, Adapter, and Ambassador. Alongside explanations, we’ll walk through hands-on examples to implement these patterns.


Why Use Multi-Container Pods?

Multi-container pods are useful when containers:

  • Require close communication (e.g., shared data via a volume or inter-container networking).
  • Need to collaborate to perform specific tasks (e.g., logging, monitoring, proxying).
  • Benefit from shared lifecycle and resource management.

Key Concepts of Multi-Container Pods

  1. Shared Storage: Containers in a pod can share the same volume.
  2. Shared Network: Containers within the same pod share the same IP address and can communicate via localhost.
  3. Coordinated Lifecycle: All containers in a pod are managed together, starting, stopping, and restarting in unison.

Common Design Patterns for Multi-Container Pods

1. Sidecar Pattern

The Sidecar Pattern is used when a secondary container enhances or extends the functionality of the primary application. Common use cases include:

  • Log collection (e.g., Fluentd).
  • Data synchronization.
  • Service discovery or configuration updates.

Example: Nginx with a Sidecar for Log Shipping

Step 1: Define the Pod YAML file.

apiVersion: v1
kind: Pod
metadata:
  name: sidecar-pod
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /var/log/nginx
      name: shared-logs
  - name: log-collector
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["tail -f /var/log/nginx/access.log"]
    volumeMounts:
    - mountPath: /var/log/nginx
      name: shared-logs
  volumes:
  - name: shared-logs
    emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

Step 2: Deploy the Pod.

kubectl apply -f sidecar-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify Logs.

kubectl logs sidecar-pod -c log-collector
Enter fullscreen mode Exit fullscreen mode

The log-collector container reads logs generated by the nginx container from the shared volume.


2. Adapter Pattern

The Adapter Pattern transforms or standardizes the output of the primary application to make it compatible with another system. Common use cases include:

  • Metrics collection and transformation.
  • Bridging communication protocols.

Example: Adapter for Converting Metrics

Step 1: Define the Pod YAML file.

apiVersion: v1
kind: Pod
metadata:
  name: adapter-pod
spec:
  containers:
  - name: app
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["while true; do echo '{\"metric\": 100}' > /data/metrics.json; sleep 5; done"]
    volumeMounts:
    - mountPath: /data
      name: shared-data
  - name: adapter
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["while true; do cat /data/metrics.json | jq '.metric'; sleep 5; done"]
    volumeMounts:
    - mountPath: /data
      name: shared-data
  volumes:
  - name: shared-data
    emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

Step 2: Deploy the Pod.

kubectl apply -f adapter-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify Metrics Conversion.

kubectl logs adapter-pod -c adapter
Enter fullscreen mode Exit fullscreen mode

The adapter container reads and processes the metrics data generated by the app container.


3. Ambassador Pattern

The Ambassador Pattern acts as a proxy between the primary container and external systems or services. This pattern is commonly used for:

  • API communication.
  • Service proxies.
  • Custom routing.

Example: Ambassador for External API Calls

Step 1: Define the Pod YAML file.

apiVersion: v1
kind: Pod
metadata:
  name: ambassador-pod
spec:
  containers:
  - name: app
    image: busybox
    command: ["/bin/sh", "-c"]
    args: ["while true; do wget -qO- http://localhost:8080/api; sleep 5; done"]
  - name: ambassador
    image: nginx
    ports:
    - containerPort: 8080
    volumeMounts:
    - mountPath: /etc/nginx/conf.d
      name: nginx-config
  volumes:
  - name: nginx-config
    configMap:
      name: ambassador-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: ambassador-config
data:
  default.conf: |
    server {
      listen 8080;
      location /api {
        proxy_pass http://api.example.com;
      }
    }
Enter fullscreen mode Exit fullscreen mode

Step 2: Deploy the ConfigMap and Pod.

kubectl apply -f ambassador-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify Proxy Behavior.

kubectl logs ambassador-pod -c app
Enter fullscreen mode Exit fullscreen mode

The app container communicates with the ambassador container, which proxies the request to http://api.example.com.


Hands-On Summary

Steps to Implement Multi-Container Pods:

  1. Identify the pattern that fits your use case.
  2. Configure shared resources (e.g., volumes, network).
  3. Define the Pod spec with multiple containers, detailing their responsibilities.
  4. Deploy the configuration and test the interaction between containers.

Best Practices for Multi-Container Pods

  1. Define Clear Responsibilities: Avoid overlapping roles between containers.
  2. Use Shared Resources Efficiently: Leverage shared volumes and networking for container interaction.
  3. Monitor Performance: Multi-container pods can be resource-intensive; monitor resource usage to prevent bottlenecks.
  4. Avoid Overloading Pods: Limit the number of containers to maintain simplicity and reliability.

Conclusion

Multi-container pods in Kubernetes offer a versatile way to design applications that require tightly coupled components. By adopting patterns like Sidecar, Adapter, and Ambassador, you can extend application functionality, standardize data, or simplify external communication. These patterns, along with practical examples, enable you to effectively manage complex workloads in Kubernetes.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay