简体   繁体   English

Docker图像内容在POD中不可用(Docker / Kubernetes)

[英]Docker Image content not available in POD (Docker/Kubernetes)

I am building an php image containing a composer magento installation. 我正在构建一个包含composer magento安装的php图像。 The content is stored in /var/www/html of the image. 内容存储在图像的/ var / www / html中。 Now I have a Php-Image and some deployment files. 现在我有一个Php-Image和一些部署文件。 But the contents of the images /var/www/html folder are not shown in any created POD. 但是images / var / www / html文件夹的内容不会显示在任何创建的POD中。 First I was thinking, that I have to create a volume in the PHP image, mapping to the /var/www/html path. 首先我想,我必须在PHP映像中创建一个卷,映射到/ var / www / html路径。 But that did not help (but it seems logical to me). 但这没有帮助(但对我来说似乎合乎逻辑)。

Maybe there is a problem with the persistent volume claim? 也许持续量索赔存在问题? I read, that I have to create a volume in the php and nginx container with the same /var/www/html path, so that php content can be executed by nginx, so I did that. 我读过,我必须在php和nginx容器中使用相同的/ var / www / html路径创建一个卷,这样php内容就可以由nginx执行,所以我就这样做了。 But now I am not sure if that is really the way to do it and it interferes with the PVC. 但现在我不确定这是否真的是这样做,它会干扰PVC。

PHP Docker-Image PHP Docker-Image

# image
FROM php:7.1-fpm

# envs
ENV INSTALL_DIR /var/www/html

# install composer
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer

# install libraries
... shortended ...

# set memory limits
RUN echo "memory_limit=2048M" > /usr/local/etc/php/conf.d/memory-limit.ini

# clean apt-get
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# www-data should own /var/www
RUN chown -R www-data:www-data /var/www

# switch user to www-data 
USER www-data

# copy sources with proper user
COPY --chown=www-data ./magento2/composer $INSTALL_DIR

# set working dir
WORKDIR $INSTALL_DIR

RUN composer install

# chmod directories
RUN chmod u+x bin/magento

# switch back
USER root

VOLUME $INSTALL_DIR

Deployments 1. Persistent Volume Claim 部署 1.持续卷索赔

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-volume-magento
    spec:
      capacity:
        storage: 50Gi
       accessModes:
        - ReadWriteOnce

2. PHP Deployment (using the build image with the web application) 2. PHP部署(使用构建映像与Web应用程序)

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: php
      labels:
        app: php
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: php
      template:
        metadata:
          labels:
            app: php
            tier: frontend
        spec:
          containers:
            - name: php-mage
              image: php-mage:latest 
              imagePullPolicy: Never
          volumeMounts:
            - name: magento2-persistent-storage
              readOnly: false
              mountPath: /var/www/html
           volumes:
             - name: magento2-persistent-storage
               persistentVolumeClaim:
               claimName: magento2-volumeclaim

3. Nginx Deployment 3. Nginx部署

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
          app: nginx
          tier: frontend
        spec:
          containers:
            - name: nginx
              image: nginx:latest
              ports:
                - containerPort: 80
                - containerPort: 443

              volumeMounts:
                - name: magento2-persistent-storage
                  readOnly: false
                  mountPath: /var/www/html

                - name: nginx-config-volume
                  mountPath: /etc/nginx/nginx.conf
                  subPath: nginx.conf

                - name: nginx-site-config-volume
                  mountPath: /etc/nginx/sites-enabled/default.conf
                  subPath: default.conf

                - name: fastcgi-snippet-volume
                  mountPath: /etc/nginx/snippets/fastcgi-php.conf
                  subPath: fastcgi-php.conf

    volumes:
      - name: magento2-persistent-storage
        persistentVolumeClaim:
          claimName: magento2-volumeclaim

       - name: nginx-config-volume
         configMap:
           name: nginx-config

        - name: nginx-site-config-volume
          configMap:
            name: nginx-site-config

        - name: fastcgi-snippet-volume
          configMap:
            name: nginx-fastcgi-config

EDIT: I realized, when I am using subPath in the php-deployment.yaml like that: 编辑:我意识到,当我在php-deployment.yaml中使用subPath时:

              volumeMounts:
                - name: magento2-persistent-storage
                  readOnly: false
                  mountPath: /var/www
                  subPath: html

My content is available in the PHP Pod. 我的内容在PHP Pod中提供。 But I can't add the same logic to the nginx deployment, because it overwrites the contents and the folder is empty again. 但我无法为nginx部署添加相同的逻辑,因为它会覆盖内容并且文件夹再次为空。

Now, a step further, but still the question how to do this correctly. 现在,更进一步,但仍然是如何正确地做到这一点的问题。 Do I have to share a mountPath between nginx and php? 我是否必须在nginx和php之间共享一个mountPath?

(1) You do have live data baked into your image but override it by mounting a volume to the same path. (1)您确实将实时数据烘焙到图像中,但通过将卷安装到同一路径来覆盖它。 If data is not to be changed during runtime, don't mount a volume. 如果在运行时不更改数据,请不要装入卷。 If it is, use another path and copy live data over on initialization of your Pod. 如果是,请使用其他路径并在初始化Pod时复制实时数据。

(2) You have a single PVC with ReadWriteOnce , but your PHP deployment is of kind Deployment that can be scaled (you initialize it with 1 replica). (2)你有一个带有ReadWriteOnce PVC,但你的PHP部署属于可以扩展的类型Deployment (用1个副本初始化它)。 As soon as you try to scale it up all its Pods other than the first one will fail because they won't be able to access the same claim with write access. 一旦您尝试将其扩展,除了第一个之外的所有Pod都将失败,因为它们将无法通过写访问权限访问相同的声明。

(3) You have a second deployment and try to reuse the same ReadWriteOnce volume. (3)您有第二次部署并尝试重用相同的ReadWriteOnce卷。 Same problem as (2) will occur. 与(2)相同的问题将发生。

Circumvent all of this by spawning your Pods with their own individual PVCs (you can have them built from volumeClaimTemplates ) automatically that only belong to a single Pod. 通过使用自己的PVC(您可以使用volumeClaimTemplates构建volumeClaimTemplates )自动生成您的Pod来实现所有这一切,这些PVC仅属于单个Pod。 Properly initialize each Pod by setting its container data "live"; 通过将其容器数据设置为“实时”来正确初始化每个Pod; copy it to the mount where it can be changed during runtime. 将其复制到可在运行时更改的安装位置。 If you need consistency of files between Pods in different deployments, spin up an in-cluster NFS server to provide ReadWriteMany volumes. 如果您需要在不同部署中的Pod之间保持文件的一致性,请启动集群内NFS服务器以提供ReadWriteMany卷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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