Practical Use Cases for Init Containers
Configuration Management
Init Containers can be used to fetch and apply configuration files, secrets, or other resources required by the application. This can be especially useful when the application needs to be configured differently in different environments.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
initContainers:
- name: fetch-config
image: busybox
command:
[
"sh",
"-c",
"wget https://example.com/config.yaml -O /etc/app/config.yaml"
]
volumeMounts:
- name: config
mountPath: /etc/app
containers:
- name: app-container
image: my-app:v1
volumeMounts:
- name: config
mountPath: /etc/app
volumes:
- name: config
emptyDir: {}
Database Initialization
Init Containers can be used to set up databases, create tables, or perform other database-related tasks before the application starts. This can be useful when the application requires a specific database schema or data to be present.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
initContainers:
- name: init-db
image: postgres:12
command:
["sh", "-c", 'psql -h my-db -U myuser -c "CREATE DATABASE myapp;"']
containers:
- name: app-container
image: my-app:v1
Dependency Installation
Init Containers can be used to install required software, libraries, or dependencies before the application starts. This can be useful when the application requires specific versions or configurations of dependencies that are not included in the main application image.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
initContainers:
- name: install-dependencies
image: ubuntu:22.04
command: ["sh", "-c", "apt-get update && apt-get install -y libfoo-dev"]
containers:
- name: app-container
image: my-app:v1
Proxy Setup
Init Containers can be used to set up network proxies, SSL/TLS certificates, or other networking-related tasks before the application starts. This can be useful when the application needs to communicate with external services through a proxy or secure connection.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
initContainers:
- name: setup-proxy
image: alpine
command:
[
"sh",
"-c",
"apk add --no-cache socat && socat -DD TCP-LISTEN:8080,fork TCP:proxy.example.com:80 &"
]
containers:
- name: app-container
image: my-app:v1
env:
- name: HTTP_PROXY
value: "http://localhost:8080"