简体   繁体   中英

kubernetes: mounting volume from within init container

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+?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM