Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes: mounting volume from within init container

Tags:

kubernetes

I'm trying to make use of init container to prepare some files before the main container starts up. In the init container I'd like to mount a hostPath volume so that I can share prepare some file for the main container.

My cluster is using pre 1.6 version of kubernetes so I'm using the meta.annotation syntax:

pod.beta.kubernetes.io/init-containers: '[
    {
        "name": "init-myservice",
        "image": "busybox",
        "command": ["sh", "-c", "mkdir /tmp/jack/ && touch cd /tmp/jack && touch a b c"],
        "volumeMounts": [{
          "mountPath": "/tmp/jack",
          "name": "confdir"
        }]
    }
]'

But it doesn't seem to work. The addition of volumeMounts cause the container init-myserver go into CrashLoop. Without it the pod gets created successfully but it doesn't achieve what I want.

Is it not possible in <1.5 to mount volume in init container? What about 1.6+?

like image 325
lang2 Avatar asked May 22 '17 09:05

lang2


People also ask

Can Kubernetes automatically mount the storage of your choice?

Storage orchestration Kubernetes allows you to automatically mount a storage system of your choice, such as local storages, public cloud providers, and more.

Does init container support readiness probes?

Also, init containers do not support lifecycle , livenessProbe , readinessProbe , or startupProbe because they must run to completion before the Pod can be ready.


1 Answers

You don't need to do hostPath volume to share data generated by init-container with the containers of Pod. You can use emptyDir to achieve same result. The benefit of using emptyDir is that you don't need to do anything on host and this will work on any kind of cluster even if you don't have access to nodes on that cluster.

Another set of problems with using hostPath is setting proper permissions on that folder on host also if you are using any SELinux enabled distro you have to setup the right context on that directory.

apiVersion: v1
kind: Pod
metadata:
  name: init
  labels:
    app: init
  annotations:
    pod.beta.kubernetes.io/init-containers: '[
        {
            "name": "download",
            "image": "axeclbr/git",
            "command": [
                "git",
                "clone",
                "https://github.com/mdn/beginner-html-site-scripted",
                "/var/lib/data"
            ],
            "volumeMounts": [
                {
                    "mountPath": "/var/lib/data",
                    "name": "git"
                }
            ]
        }
    ]'
spec:
  containers:
  - name: run
    image: docker.io/centos/httpd
    ports:
      - containerPort: 80
    volumeMounts:
    - mountPath: /var/www/html
      name: git
  volumes:
  - emptyDir: {}
    name: git

Checkout the above example where the init-container and the container in pod are sharing same volume named git. And the type of volume is emptyDir. I just want the init-container to pull the data everytime this pod comes up and then it is served from the httpd container of pod.

HTH.

like image 52
surajd Avatar answered Sep 28 '22 07:09

surajd