【问题标题】:how to access image content on kubernetes container init如何访问 kubernetes 容器 init 上的图像内容
【发布时间】:2019-05-19 22:08:08
【问题描述】:

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

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

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

我想当 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

【问题讨论】:

    标签: shell kubernetes containers init


    【解决方案1】:

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

    肯定是后者,这就是为什么您可以为您的initContainers: 任务指定一个完全不同的 image: -- 它们彼此相关只是因为它们运行在同一个如您所见,节点和共享卷。好吧,我说“肯定”,但你有一点用词不当:在创建 magneto containers 之后——Pod 是 every 同位容器的集合, 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: {} 引用,那么这些目录将始终是“新鲜的”,并且将始终仅包含在您的 initContainer: 中声明的标记图像中的内容

    【讨论】:

    • 解决方案确实不是为 InitContainers 使用忙盒,而是使用 amo-magento 映像。但是我没有使用空目录,而是加载了 pvc。不需要第二个坐骑。
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多