Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Kubernetes

Kubernetes: Use Init Containers for Setup Tasks

- 11.07.26 - ErcanOPAK

⚙️ Init Containers = Setup Tasks

Apps need setup before start. Init containers run before main containers. Database migrations, permissions, downloads.

📝 Init Container Basics

apiVersion: v1
kind: Pod
metadata:
    name: app-with-init
spec:
    initContainers:
    - name: init-db
        image: busybox
        command: ['sh', '-c', 'echo "Initializing database..." && sleep 5']
    - name: init-permissions
        image: busybox
        command: ['sh', '-c', 'chmod -R 777 /data']
    containers:
    - name: main-app
        image: myapp:latest
        volumeMounts:
        - name: data
            mountPath: /data
    volumes:
    - name: data
        emptyDir: {}

🎯 Init Container Examples

# Database migration
apiVersion: apps/v1
kind: Deployment
metadata:
    name: app
spec:
    replicas: 1
    template:
        spec:
            initContainers:
            - name: migrate-db
                image: myapp:migration
                env:
                - name: DB_HOST
                    value: postgres-service
                command: ['sh', '-c', 'npm run migrate']
            containers:
            - name: app
                image: myapp:latest
                env:
                - name: DB_HOST
                    value: postgres-service

# Download assets
apiVersion: v1
kind: Pod
metadata:
    name: app
spec:
    initContainers:
    - name: download-assets
        image: curlimages/curl
        command: ['sh', '-c', 'curl -o /assets/data.json https://example.com/data.json']
        volumeMounts:
        - name: assets
            mountPath: /assets
    containers:
    - name: app
        image: myapp:latest
        volumeMounts:
        - name: assets
            mountPath: /app/assets
    volumes:
    - name: assets
        emptyDir: {}

# Wait for service
apiVersion: v1
kind: Pod
metadata:
    name: app
spec:
    initContainers:
    - name: wait-for-db
        image: busybox
        command: ['sh', '-c', 'until nc -z postgres-service 5432; do echo "Waiting for database..."; sleep 2; done']
    containers:
    - name: app
        image: myapp:latest

💡 Init Container Tips

  • Use for database migrations
  • Use for asset downloads
  • Use for permission setup
  • Use for waiting on dependencies
  • Run in order, one at a time

“Init containers run before main. Setup, migrations, dependencies. Essential for app initialization.”

— DevOps Engineer

Related posts:

Kubernetes: Fix a Pod Restarting in a Loop Because Its Liveness Probe Timeout Is Shorter Than the Ap...

Kubernetes: Fix a Pod Restarting in a Loop Because Its Liveness Probe Timeout Is Shorter Than the Ap...

Kubernetes: Fix a PodDisruptionBudget Blocking a Node Drain Forever Because minAvailable Equals the ...

Kubernetes: Fix a PodDisruptionBudget Blocking a Node Drain Forever Because minAvailable Equals the ...

Kubernetes: Set Resource Limits for Stability

Kubernetes: Set Resource Limits for Stability

Views: 9
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

WordPress: Create Custom Gutenberg Blocks
Docker: Understand Image Layers for Optimization

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com