kubernetes: Multi-container pods & init containers






Hello Readers,hope you are doing good. In this article we are going to discuss about multi-container pods and after that init containers.


So far we have created countless number of pods but as you must have noticed that it always contained a single container. Sometimes it is required that two or more containers are dependent upon each other to provide the full functionality so yes it is possible to have one pod having two containers running inside it.






The multiple containers inside the pod are life partners :-)

  • They are created and killed together when a pod is created or deleted.

  • They share the same network space and can communicate with each other as localhost.

  • They have access to the same storage volumes.

[root@node1 kubernetes]# cat multi-continer-pod.yaml
## This yaml file creates a pod with 3 containers which have
# a simple busybox container image and echos different message
# when it starts.

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - bin/sh
    - -c
    - ls; sleep 3600
    image: busybox
    name: busybox1
    resources: {}
  - args:
    - bin/sh
    - -c
    - echo Hello world; sleep 3600
    image: busybox
    name: busybox2
    resources: {}
  - args:
    - bin/sh
    - -c
    - echo this is third container; sleep 3600
    image: busybox
    name: busybox3
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

#######################################################################
[root@node1 kubernetes]# kubectl get pods | grep -i busy
busybox                   3/3     Running     0          5m2s

init Containers

In a multi-container pod, each container is expected to run a process that stays alive as long as the POD's life cycle. For example in the multi-container pod that we talked about earlier that has a web application and logging agent, both the containers are expected to stay alive at all times. The process running in the log agent container is expected to stay alive as long as the web application is running. If any of them fails, the POD restarts.


But at times you may want to run a process that runs to completion in a container. For example a process that pulls a code or binary from a repository that will be used by the main web application. That is a task that will be run only  one time when the pod is first created. Or a process that waits  for an external service or database to be up before the actual application starts. That's where initContainers comes in.

An initContainer is configured in a pod like all other containers, except that it is specified inside a initContainers section,  like this:



apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'git clone https://github.com/linuxadvise/terraform_lab.git ; done;']

Let's put an end to this article here. Next article will be about kubernetes security. Important and interesting.








309 views0 comments

Recent Posts

See All
 

Subscribe Form

©2020 by Linux Advise