如何在 kubernetes 容器 init 上访问图像内容

how to access image content on kubernetes container init

我有一个图像,其中包含 /usr/data/webroot 中的数据。此数据应在容器初始化时移动到 /var/www/html.

现在我偶然发现了 InitContainers。据我了解,它可用于执行容器初始化任务。

但我不知道任务是否在创建 amo-magento pods 之后执行,或者是否运行了 init 任务,然后创建了 magento pods。

我想当 initContainers 任务运行时,带有 magento 图像的容器不可用,因此没有内容可用于移动到新目录。

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

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.

肯定是后者,这就是为什么您能够为您的 initContainers: 任务指定一个 完全不同的 image: - 它们与一个相关另一个唯一的原因是它们 运行 在同一个节点上,并且如您所见,共享卷。好吧,我说的是 "for sure" 但你用词不当:之后创建了 magneto 容器 —— Pod 是 的集合每个 个并置容器,initContainers:container: 个容器

如果我理解你的问题,你的 Deployment 的修复只是将你的 initContainer: 中的 image: 更新为包含魔法 /usr/data/webroot 的那个然后更新您的 shell 命令以引用该图像中的正确路径:

  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

然后在 container[0] 启动时,PVC 将包含您期望的数据


也就是说,我实际上非常确定您想从这个故事中删除 PVC,因为——根据定义——它在 Pod 重新启动时是持久的,因此将只会随着时间的推移积累文件(因为您的 sh 命令当前不会在将文件移动到那里之前清理 /www )。如果您将所有这些 pvc 引用替换为 emptyDir: {} 引用,那么这些目录将始终是 "fresh",并且将始终仅包含您在 initContainer:[ 中声明的标记图像的内容=27=]