In this article, will see how to clone a git repository using init-container. For that, let’s use an init container to clone the git repository before running any other application container. The github repository contains application data, which will be used by application. After successfully cloning application data from the github repository, the init-container will be terminated. And finally, application will start to execute.
What is Init Container?
A pod can have Init Containers in addition to application containers. Init containers allow you to reorganize setup scripts and binding code.
An init container is the one that starts and executes before other containers in the same Pod. It’s meant to perform initialization logic for the main application hosted on the Pod.
Properties of Init container
- It contains utilities or setup scripts not present in an app image (making images light-weight)
- They always run to completion
- Init container executes sequentially and each of the init containers must succeed before the next can run.
- They support all the fields and features of app containers, including resource limits, volumes, and security settings.
- Init-containers do not support lifecycle, liveness probe, readiness probe, or startup probe because they must run to completion.
Init-container in use
Using emptyDir volume
#Pod with Init-container using emptyDir volume
apiVersion: v1
kind: Pod
metadata:
labels:
run: foxapp
name: foxapp
spec:
containers:
- image: foximage
name: fox-app
volumeMounts:
- name: app-data
mountPath: /usr/share/fox/html
initContainers:
- image: alpine/git
name: git
command:
- git
- clone
- https://github.com/xxxxxxxxx/yyyyyyyyyyyyy.git
- /temp-repo
volumeMounts:
- name: app-data
mountPath: /temp-repo
volumes:
- name: app-data
emptyDir: {}
Using hostPath volume
#Pod with Init-container using hostPath volume
apiVersion: v1
kind: Pod
metadata:
labels:
run: foxapp
name: foxapp
spec:
containers:
- image: foximage
name: fox-app
volumeMounts:
- name: app-data
mountPath: /usr/share/fox/html
initContainers:
- image: alpine/git
name: git
command:
- git
- clone
- https://github.com/xxxxxxxxx/yyyyyyyyyyyyy.git
- /temp-repo
volumeMounts:
- name: app-data
mountPath: /temp-repo
volumes:
- name: app-data
hostPath:
path: /root/app
type: DirectoryOrCreate
Use cases of Init container
- Init containers can contain utilities or custom code for the setup that are not present in an app image.
- They can be given access to Secrets that app containers cannot access, like pulling the secrets from vault or other apps.
- Performing an automated restore from backup in case the data volume is empty (initial setup after a major outage) or contains corrupt data (automatically recover the last working version).
- Doing database schema updates and other data maintenance tasks that depend on the main application not running.
- Ensure that the application data is in a consistent and usable state, repairing it if necessary.
- Clone a Git repository into a Volume
- It can be used to wait for a service to start that is to be used by the main app
- An init container is a good candidate for delaying the application initialization until one or more dependencies are available.