Multi Container Pod — Sidecar & Init Container — Kubernetes
A pod is the smallest deployable unit of Kubernetes. A pod can have either a single containerized application running in them or multiple containerized applications running in them.
The Pods with multiple containerized applications are known as Multi-Container Pods.
There are two main types of containers in a multi-container pod:
- Init Containers → These run and are complete before the main application containers start. They are typically used for setup tasks.
- Sidecar Containers → These run alongside the main application container and provide support, such as logging, monitoring, (or) proxying.
Initialization containers (or) Init containers
“Init Containers are special containers that run before the main application containers in a pod start”
They are designed to perform initialization tasks, such as setting up configuration files, initializing databases, (or) any other operation necessary for the proper functioning of the application.
Key Characteristics:
Sequential Execution
Single Responsibility
Transient Nature
Fail and Restart
Sidecar Containers:
“Sidecar Containers are additional containers that run alongside the main application container within the same pod”
They extend or enhance the functionality of the primary container by providing supporting processes, utilities, (or) services.
Key Characteristics:
Parallel Execution
Isolation of Concerns
Enhanced Functionality
Inter-Container Communication
Create Multiple Containers in a Pod
Create a Manifest file. If a person wants to do anything in Kubernetes first required to create a Manifest. Manifest is a file using the YAML(Yet Another Markup Language)
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app.kubernetes.io/name: MyApp
spec:
containers:
- name: myapp-container
image: busybox:1.28
env:
- name: FIRSTNAME
value: "Ibbus"
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c']
args: ['until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done']
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c']
args: ['until nslookup mydb.default.svc.cluster.local; do echo waiting for mydb; sleep 2; done']
Apply the manifest to your Kubernetes cluster
kubectl apply -f multi-container.yaml
Check the status of the pod to ensure it’s running
kubectl get pod
Check full pod
kubectl describe pod/myapp-pod
Stucking the Init state because it’s waiting for the service to be provisioned first.
Check pod logs
kubectl logs myapp-pod
Pause the value
kubectl logs myapp-pod -c init-myservice
Create a deployment
kubectl create deploy nginx-deploy --image nginx --port 80
kubectl get deploy
kubectl get pod
Expose the service
kubectl expose deploy nginx-deploy --name myservice --port 80
kubectl get pod
We can see that myapp-pod
is initializing.
Lets wait for a few seconds. Now, we can see that as soon as our service gets configured, our myapp-pod
container's state got changed to running.
Thank you 🙏 for taking the time to read our blog.