how to access image content on kubernetes container init

5/19/2019

I have an image which contains data inside /usr/data/webroot. This data should be moved on container init to /var/www/html.

Now I stumbeled upon InitContainers. As I understand it, it can be used to execute tasks on container initialization.

But I don't know if the task is beeing excuted after the amo-magento pods are created, or if the init task runs, and after that the magento pods are created.

I suppose that the container with the magento image is not available when the initContainers task runs, so no content is available to move to the new directory.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: amo-magento
  labels:
    app: amo-magento
spec:
  replicas: 1
  selector:
    matchLabels:
      app: amo-magento
  template:
    metadata:
      labels:
        app: amo-magento
        tier: frontend
    spec:
      initContainers:
        - name: setup-magento
          image: busybox:1.28
          command: ["sh", "-c", "mv -r /magento/* /www"]

          volumeMounts:
            - mountPath: /www
              name: pvc-www

            - mountPath: /magento
              name: magento-src

      containers:
        - name: amo-magento
          image: amo-magento:0.7 # add google gcr.io path after upload
          imagePullPolicy: Never

          volumeMounts:
            - name: install-sh
              mountPath: /tmp/install.sh
              subPath: install.sh

            - name: mage-autoinstall
              mountPath: /tmp/mage-autoinstall.sh
              subPath: mage-autoinstall.sh

            - name: pvc-www
              mountPath: /var/www/html

            - name: magento-src
              mountPath: /usr/data/webroot

            # maybe as secret - can be used as configMap because it has not to be writable
            - name: auth-json
              mountPath: /var/www/html/auth.json
              subPath: auth.json

            - name: php-ini-prod
              mountPath: /usr/local/etc/php/php.ini
              subPath: php.ini

#            - name: php-memory-limit
#              mountPath: /usr/local/etc/php/conf.d/memory-limit.ini
#              subPath: memory-limit.ini

      volumes:
        - name: magento-src
          emptyDir: {}

        - name: pvc-www
          persistentVolumeClaim:
            claimName: magento2-volumeclaim

        - name: install-sh
          configMap:
            name: install-sh

        # kubectl create configmap mage-autoinstall --from-file=build/docker/mage-autoinstall.sh
        - name: mage-autoinstall
          configMap:
            name: mage-autoinstall

        - name: auth-json
          configMap:
            name: auth-json

        - name: php-ini-prod
          configMap:
            name: php-ini-prod

#        - name: php-memory-limit
#          configMap:
#            name: php-memory-limit
-- John Jameson
containers
init
kubernetes
shell

1 Answer

5/20/2019

But I don't know if the task is beeing excuted after the amo-magento pods are created, or if the init task runs, and after that the magento pods are created.

For sure the latter, that's why you are able to specify an entirely different image: for your initContainers: task -- they are related to one another only in that they run on the same Node and, as you have seen, share volumes. Well, I said "for sure" but you have a slight misnomer: after that the magneto containers are created -- the Pod is the collection of every colocated container, initContainers: and container: containers

If I understand your question, the fix to your Deployment is just to update the image: in your initContainer: to be the one which contains the magic /usr/data/webroot and then update your shell command to reference the correct path inside that image:

  initContainers:
    - name: setup-magento
      image: your-magic-image:its-magic-tag
      command: ["sh", "-c", "mv -r /usr/data/webroot/* /www"]
      volumeMounts:
        - mountPath: /www
          name: pvc-www
        # but **removing** the reference to the emptyDir volume

and then by the time the container[0] starts up, the PVC will contain the data you expect


That said, I am actually pretty sure that you want to remove the PVC from this story, since -- by definition -- it is persistent across Pod reboots and thus will only accumulate files over time (since your sh command does not currently clean up /www before moving files there). If you replaced all those pvc references to emptyDir: {} references, then those directories would always be "fresh" and would always contain just the content from the tagged image declared in your initContainer:

-- mdaniel
Source: StackOverflow