簡體   English   中英

Docker圖像內容在POD中不可用(Docker / Kubernetes)

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

我正在構建一個包含composer magento安裝的php圖像。 內容存儲在圖像的/ var / www / html中。 現在我有一個Php-Image和一些部署文件。 但是images / var / www / html文件夾的內容不會顯示在任何創建的POD中。 首先我想,我必須在PHP映像中創建一個卷,映射到/ var / www / html路徑。 但這沒有幫助(但對我來說似乎合乎邏輯)。

也許持續量索賠存在問題? 我讀過,我必須在php和nginx容器中使用相同的/ var / www / html路徑創建一個卷,這樣php內容就可以由nginx執行,所以我就這樣做了。 但現在我不確定這是否真的是這樣做,它會干擾PVC。

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

部署 1.持續卷索賠

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

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部署

    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

編輯:我意識到,當我在php-deployment.yaml中使用subPath時:

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

我的內容在PHP Pod中提供。 但我無法為nginx部署添加相同的邏輯,因為它會覆蓋內容並且文件夾再次為空。

現在,更進一步,但仍然是如何正確地做到這一點的問題。 我是否必須在nginx和php之間共享一個mountPath?

(1)您確實將實時數據烘焙到圖像中,但通過將卷安裝到同一路徑來覆蓋它。 如果在運行時不更改數據,請不要裝入卷。 如果是,請使用其他路徑並在初始化Pod時復制實時數據。

(2)你有一個帶有ReadWriteOnce PVC,但你的PHP部署屬於可以擴展的類型Deployment (用1個副本初始化它)。 一旦您嘗試將其擴展,除了第一個之外的所有Pod都將失敗,因為它們將無法通過寫訪問權限訪問相同的聲明。

(3)您有第二次部署並嘗試重用相同的ReadWriteOnce卷。 與(2)相同的問題將發生。

通過使用自己的PVC(您可以使用volumeClaimTemplates構建volumeClaimTemplates )自動生成您的Pod來實現所有這一切,這些PVC僅屬於單個Pod。 通過將其容器數據設置為“實時”來正確初始化每個Pod; 將其復制到可在運行時更改的安裝位置。 如果您需要在不同部署中的Pod之間保持文件的一致性,請啟動集群內NFS服務器以提供ReadWriteMany卷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM