Sobes.tech
Middle

What is an init container in Kubernetes?

sobes.tech AI

Answer from AI

A special container in a pod that completes its work before the main containers in the pod start.

Its purposes:

  • Initialize the environment before the main application starts.
  • Load configuration or scripts.
  • Wait for external services (databases, APIs) to be ready.
  • Configure the filesystem or access rights.

Main characteristics:

  • Executed in the order specified in the pod's specification.
  • Each init container must successfully complete before the next one starts or the main containers begin their work.
  • If an init container fails, Kubernetes restarts the pod until the init container completes successfully (depending on the pod restart policy).
  • They have access to a shared volume of the pod, allowing data transfer to main containers.

Pod specification in YAML:

// Example of a pod specification with an init container
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  initContainers: // List of init containers
  - name: init-myservice // Name of the init container
    image: my-init-image // Image of the init container
    command: ["sh", "-c", "echo 'Initializing...' && sleep 5"] // Command to execute
  containers: // List of main containers
  - name: my-app // Name of the main container
    image: my-app-image // Image of the main container
    ports:
    - containerPort: 80