惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
小众软件
小众软件
博客园_首页
博客园 - 聂微东
V
V2EX
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
SegmentFault 最新的问题
J
Java Code Geeks
Last Week in AI
Last Week in AI
The Cloudflare Blog
月光博客
月光博客
雷峰网
雷峰网
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
博客园 - Franky
腾讯CDC
Jina AI
Jina AI
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
量子位
爱范儿
爱范儿
美团技术团队
T
Tailwind CSS Blog
博客园 - 【当耐特】
D
Docker
IT之家
IT之家
V
Visual Studio Blog
P
Proofpoint News Feed
L
LangChain Blog
Engineering at Meta
Engineering at Meta
C
Check Point Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Recorded Future
Recorded Future

Devoriales - DevOps and Python Tutorials

Cloud & DevOps & AI Digest: The Week of Jun 28, 2026 Cloud & DevOps & AI Digest: The Week of Jun 20, 2026 Ansible for DevOps Engineers: Architecture, Core Concepts, and Hands-On Lab Login Must-Have Kubernetes CLI Tools Every Platform Engineer Should Know Login Login Login Why Your Best Engineers Are Quitting (And How to Stop It) Login ArgoCD Vulnerability: How the ServerSideDiff Feature Exposes Kubernetes Secrets Login How Kubernetes Controls What Your Containers Can Do Login Multi-AZ Is Not Disaster Recovery: What the AWS Bahrain Outage Finally Proved Trivy Supply Chain Attack: When Your Security Scanner Becomes the Threat Is Claude Opus 4.6 Fast Mode Really Worth 6× the Price? Login Unlocking Higher Pod Density in EKS with Prefix Delegation AWS Regional NAT Gateway: What It Is and Why You Should Care Kubernetes 1.35 Timbernetes Release AWS re:Invent 2025: The Future of Kubernetes on EKS Debate Series: How Do We Control Deployment Order in Kubernetes? Debate Series: Should We Eliminate Kubernetes Secrets Entirely? Kubernetes CRDs Explained: A Beginner-Friendly Guide to Extending the Kubernetes API Reduce Cloud Cross-Zone Data Transfer Costs with Kubernetes 1.33 trafficDistribution Building Custom Bitnami Images: A Guide for Self-Hosted Container Images New Features in Kubernetes 1.34: An Overview From Free to Fee: How Broadcom's Bitnami Monetization Disrupts DevOps Infrastructure Claude Code Cheat Sheet: The Reference Guide Kubernetes Loses Enterprise Slack Status: Discord Among Platforms Being Considered Understanding Container Security: A Guide to Docker and Pod Security AWS Launches Serverless MCP Server: AI-Powered Development Gets a Serverless Boost Valve Responds to Alleged Steam Data Breach Reports: What Users Need to Know ArgoCD 3.0: The Evolution Toward Secure GitOps Redis Returns to Open Source: The AGPLv3 Licensing Decision New Features in Kubernetes 1.33: An Overview Prometheus: How We Slashed Memory Usage IngressNightmare: Critical Ingress-NGINX Vulnerabilities and How to Check Your Exposure New Features in Kubernetes 1.32: An Overview What to Consider If You're Not Signing Up for Bitnami Premium Certified Kubernetes Administrator (CKA) Exam Updates for 2025 DeepSeek AI and the Question of the AI Bubble Python Tops the Tiobe Index: The Most Popular Programming Languages - January 2025 2024 in Review: IT Trends, Startups, and What’s Next Inside Argo: The Open-Source Journey Captured in a CNCF Documentary Running Docker on macOS Without Docker Desktop - updated with Kubernetes installation HashiCorp Rolls Out Terraform 2.0 at HashiConf, Keeps IBM Acquisition in the Shadows Is the EU Falling Behind in the Global AI Race? Prometheus Essentials: Node Exporter And System Monitoring Prometheus Essentials: Install and Start Monitoring Your App Prometheus Essentials: Introduction To Metric Types Kubernetes Pod Scheduling Explained: Taints, Tolerations, and Node Affinity Retrieval Augmented Generation (RAG) Explained for Beginners Like Me Using Sealed Secrets with Your Kubernetes Applications
Container Patterns in Kubernetes: Init Containers, Sidecars, and Co-located Containers Explained
Aleksandro Matejic · 2025-06-06 · via Devoriales - DevOps and Python Tutorials

Container orchestration in Kubernetes offers multiple patterns for deploying applications, each serving specific architectural needs. Understanding when and how to use init containers, sidecar containers, and co-located containers is essential for building robust, maintainable applications.

This guide explores these three patterns, their use cases, and implementation strategies.

Understanding Pod Architecture

Before examining specific container patterns, it's crucial to understand that a Pod represents the smallest deployable unit in Kubernetes. A Pod can contain one or multiple containers that share the same network namespace, storage volumes, and lifecycle. This shared environment enables different container patterns to work together effectively.

All containers within a Pod communicate through localhost, share the same IP address, and can access shared volumes. This design principle forms the foundation for the container patterns we'll explore.

Containers sharing network and storage within the pod

Init Containers: Preparation and Setup

Init containers run to completion before the main application containers start. They handle initialization tasks such as database migrations, configuration setup, or dependency downloads. Unlike regular containers, init containers run sequentially and must be completed successfully before the Pod proceeds to start its main containers.

Common Use Cases for Init Containers

Database schema initialization and migration scripts represent a typical init container scenario. Configuration file generation from external sources, dependency downloading, and security credential setup also benefit from this pattern.

apiVersion: v1
kind: Pod
metadata:
  name: web-app-with-init
spec:
  initContainers:
  - name: database-setup
    image: postgres:13
    command: ['sh', '-c', 'psql -h db-host -U admin -c "CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY);"']
    env:
    - name: PGPASSWORD
      valueFrom:
        secretKeyRef:
          name: database-credentials
          key: password
  - name: config-generator
    image: busybox
    command: ['sh', '-c', 'echo "server_name=prod" > /shared/config.properties']
    volumeMounts:
    - name: config-volume
      mountPath: /shared
  containers:
  - name: web-server
    image: nginx:latest
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    emptyDir: {}

This example demonstrates sequential initialization where the database setup completes before configuration generation, and both finish before the web server starts.

Pod Lifecycle with Init Containers

Init Container Pattern

Init containers run sequentially and must be completed successfully before the main containers start. Perfect for setup tasks, database migrations, and dependency preparation.

Sidecar Containers: Enhanced Functionality

Sidecar containers extend the functionality of main applications without modifying the application code. These containers run alongside the main application throughout the Pod's lifecycle, providing services such as logging, monitoring, security proxying, or data synchronization.

Kubernetes v1.29 introduced native sidecar support by implementing them as restartable init containers with restartPolicy: Always. This ensures proper startup order while maintaining continuous operation.

Example of Sidecar Containers

apiVersion: v1
kind: Pod
metadata:
  name: app-with-monitoring-sidecar
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  - name: metrics-data
    emptyDir: {}
  
  initContainers:
  - name: metrics-collector
    image: prom/node-exporter:latest
    restartPolicy: Always
    ports:
    - containerPort: 9100
    volumeMounts:
    - name: metrics-data
      mountPath: /metrics
    command: ['/bin/node_exporter', '--path.rootfs=/host']
  
  - name: log-shipper
    image: fluent/fluent-bit:latest
    restartPolicy: Always
    volumeMounts:
    - name: shared-data
      mountPath: /var/log
    command: ['/fluent-bit/bin/fluent-bit', '-c', '/fluent-bit/etc/fluent-bit.conf']
  
  containers:
  - name: main-application
    image: my-app:latest
    volumeMounts:
    - name: shared-data
      mountPath: /app/logs
    - name: metrics-data
      mountPath: /app/metrics

Sidecar containers enhance main application functionality without code changes. They start first, run continuously, and can restart independently. Common for logging, monitoring, and proxying. 

Sidecar container pattern


The sidecar pattern provides several advantages including separation of concerns, independent scaling of auxiliary services, and standardized operational tooling across different applications.

Co-located Containers: Collaborative Applications

Co-located containers work together as equal partners within the same Pod to deliver application functionality. Unlike sidecars that enhance a primary application, co-located containers share responsibility for the overall application behavior. This pattern suits multi-process applications, microservice communication, and helper service scenarios.

Co-located Container Example

apiVersion: v1
kind: Pod
metadata:
  name: content-management-system
spec:
  volumes:
  - name: shared-content
    emptyDir: {}
  - name: shared-cache
    emptyDir: {}
  
  containers:
  - name: content-api
    image: content-api:v1.2
    ports:
    - containerPort: 8080
    volumeMounts:
    - name: shared-content
      mountPath: /data/content
    - name: shared-cache
      mountPath: /cache
    env:
    - name: CACHE_DIR
      value: "/cache"
  
  - name: content-processor
    image: content-processor:v1.2
    volumeMounts:
    - name: shared-content
      mountPath: /data/content
    - name: shared-cache
      mountPath: /cache
    env:
    - name: CONTENT_DIR
      value: "/data/content"
  
  - name: cache-manager
    image: redis:6-alpine
    ports:
    - containerPort: 6379
    volumeMounts:
    - name: shared-cache
      mountPath: /data
    command: ['redis-server', '--dir', '/data']

In this example, three containers collaborate to provide a content management system. The content API serves requests, the content processor handles background tasks, and the cache manager provides shared caching services.

Co-located containers pattern

Co-located containers work as equal partners with no defined startup order. They collaborate to provide application functionality through shared resources and direct communication.

Choosing the Right Pattern

Feature Init Containers Sidecar Containers Co-located Containers
Primary Role Setup & Preparation Enhance Main App Equal Collaboration
Startup Order Sequential Before Main No Order
Lifecycle Run to Completion Continuous Independent
Common Use Cases DB Migration, Setup Logging, Monitoring Multi-process Apps
Restart Behavior Pod Restart Independent Restart Per Container

Conclusion

Container patterns in Kubernetes provide powerful architectural options for building scalable, maintainable applications. Init containers handle preparation tasks, sidecar containers extend functionality, and co-located containers enable collaborative applications.

The introduction of native sidecar support in Kubernetes v1.29 simplifies implementation while providing better lifecycle control. Choosing the appropriate pattern depends on your specific use case, architectural requirements, and operational needs.

Success with these patterns requires careful planning of resource allocation, security contexts, and health monitoring. By understanding the strengths and limitations of each approach, you can build robust Kubernetes applications that scale effectively and maintain operational excellence.

Ref: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/