Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - Trying to create pod with init container

I am trying to play with init pods. I want to use init container to create file and default container to check if file exist and sleep for a while.

my yaml:

apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -e /workdir/test.txt ]; then sleep 99999; fi']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']

When I am trying to debug from alpine image I use the command to create:

kubectl run alpine --rm -ti --image=alpine /bin/sh

If you don't see a command prompt, try pressing enter.
/ # if [ -e /workdir/test.txt ]; then sleep 3; fi
/ # mkdir /workdir; echo>/workdir/test.txt
/ # if [ -e /workdir/test.txt ]; then sleep 3; fi
/ *here shell sleeps for 3 seconds
/ #

And it seems like commands working as expected.

But on my real k8s cluster I have only CrashLoopBackOff for main container.

kubectl describe pod init-test-pod

Shows me only that error:

Containers:
  myapp-container:
    Container ID:  docker://xxx
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:xxx
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      if [ -e /workdir/test.txt ]; then sleep 99999; fi
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
    Ready:          False
    Restart Count:  3
    Environment:    <none>
like image 970
golang Avatar asked Mar 25 '19 14:03

golang


People also ask

What is init container in Kubernetes?

This page provides an overview of init containers: specialized containers that run before app containers in a Pod. Init containers can contain utilities or setup scripts not present in an app image. You can specify init containers in the Pod specification alongside the containers array (which describes app containers).

Can we create pod without container?

It is possible to create a Pod as a standalone object. Kubenretes has controllers that can create and manage multiple Pods, handle replication and rollout and provide self-healing capabilities at cluster scope.

Is init container a sidecar?

Init containers run before applications containers run in a pod, and sidecar containers run alongside application containers in a pod. One use for init containers is to bootstrap Appian with RDBMS/JDBC drivers not included in the Webapp Docker image (for example, MySQL or IBM Db2).


2 Answers

The problem here is that your main container is not finding the folder you create. When your initial container completes running, the folder gets wiped with it. You will need to use a Persistent Volume to be able to share the folder between the two containers:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  volumes:
  - name: mypvc
    persistentVolumeClaim:
      claimName: mypvc
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -f /workdir/test.txt ]; then sleep 99999; fi']
    volumeMounts:
    - name: mypvc
      mountPath: /workdir
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']
    volumeMounts:
    - name: mypvc
      mountPath: /workdir

You can as well look at emptyDir, so you won't need the PVC:

apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  volumes:
  - name: mydir
    emptyDir: {}
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -f /workdir/test.txt ]; then sleep 99999; fi']
    volumeMounts:
    - name: mydir
      mountPath: /workdir
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']
    volumeMounts:
    - name: mydir
      mountPath: /workdir
like image 79
cookiedough Avatar answered Oct 13 '22 12:10

cookiedough


That's because your 2 containers have separate filesystems. You need to share this file using an emtyDir volume:

apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -e /workdir/test.txt ]; then sleep 99999; fi']
    volumeMounts:
    - mountPath: /workdir
      name: workdir
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']
    volumeMounts:
    - mountPath: /workdir
      name: workdir
  volumes:
  - name: workdir
    emptyDir: {}
like image 23
Grigory Ignatyev Avatar answered Oct 13 '22 14:10

Grigory Ignatyev