Data written to PVC using job pod is not available in main POD

68 Views Asked by At

Data written to PVC using job pod is not available in main POD,

I have a job pod with a bash script that executes and writes data to the PVC, however, the same data is not available when I check in from the main POD, both the deployment & job use the same PVC, it is Read Write Many.

Deployment files

apiVersion: apps/v1 
kind: Deployment
metadata:
  name: example-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magento-service
  template:
    metadata:
      labels:
        app: magento-service
    spec:
      containers:
        - name: magento
          image: adobemagento2/magento473
          ports:
            - containerPort: 80
            - containerPort: 9000
          envFrom:
           - configMapRef:
               name: mag-config
          volumeMounts:
            - name: web
              mountPath: "/var/www/html/pub/media"

      volumes:
        - name: web
          persistentVolumeClaim:
            claimName: magento-pvc
      imagePullSecrets:
        - name: regcred

jobs files

apiVersion: batch/v1 
kind: Job
metadata:
  name: example-job
spec:
  template:
    metadata:
      name: example-job
    spec:
      containers:
      - name: example-container
        image: adobemagento2/magento473
        command: ["sh", "-c", "bin/install.sh"]
        envFrom:
         - configMapRef:
             name: mag-config
        volumeMounts:
        - name: web
          mountPath: "/var/www/html/pub/media"
          


      restartPolicy: Never
      imagePullSecrets:
        - name: regcred
      volumes:
      - name: web
        persistentVolumeClaim:
          claimName: magento-pvc

Dockerfile

FROM php:8.2.12-fpm@sha256:2aa938b6d62f7415e9c84d867d9ceed18ef8ec3cf3944d389e088c93f9678a84 
ENV DOC_ROOT /var/www/html
ENV COMPOSER_HOME=/tmp/composer
WORKDIR /var/www/html
#ENV MAGENTO_VERSION 2.4.6-p3


#RUN apt-get update && apt-get install -y --no-install-recommends gnupg \
#RUN apt-get install -y gnupg1
#RUN echo "deb http://nginx.org/packages/mainline/debian buster nginx" >> /etc/apt/sources.list.d/nginx.list
#RUN curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add -
#RUN apt-get update && apt-get install -y nginx=1.24.*



RUN apt-get update && apt-get install -y --no-install-recommends gnupg \
    netcat-openbsd \
    nginx \
    sudo \
    libicu-dev \
    libfreetype6-dev \
    libjpeg-dev \
    libpng-dev \
    libsodium-dev \
    libxml2-dev \
    libxslt-dev \
    libzip-dev \
    rsync \
    supervisor \
    unzip \
    cron \
    default-mysql-client \
    git \
    gnupg \
    gzip \
    libbz2-dev \
    libfreetype6-dev \
    libicu-dev \
    libjpeg62-turbo-dev \
    libmagickwand-dev \
    libmcrypt-dev \
    libonig-dev \
    libpng-dev \
    libsodium-dev \
    libssh2-1-dev \
    libwebp-dev \
    libxslt1-dev \
    libzip-dev \
    lsof \
    mailutils \
    msmtp \
    procps \
    vim \
    zip \
  && rm -rf /var/lib/apt/lists/*



#RUN echo "deb http://nginx.org/packages/mainline/debian buster nginx" >> /etc/apt/sources.list.d/nginx.list
#RUN curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add -
#RUN apt-get update && apt-get install -y nginx

RUN pecl install apcu-5.1.22

RUN docker-php-ext-configure \
    gd --with-freetype --with-jpeg --with-webp \
  && docker-php-ext-install \
    bcmath \
    bz2 \
    calendar \
    exif \
    gd \
    gettext \
    intl \
    mbstring \
    mysqli \
    opcache \
    pcntl \
    pdo_mysql \
    soap \
    sockets \
    sodium \
    sysvmsg \
    sysvsem \
    sysvshm \
    xsl \
    zip 



RUN mkdir -p /var/run && \
    chown -R www-data:www-data /var/run
#WORKDIR /var/www/html


COPY  magento /etc/cron.d/cronjob
COPY  sslcript.sh /usr/local/bin
RUN chmod +x /usr/local/bin/sslcript.sh
RUN chown www-data:www-data /etc/cron.d/cronjob
#COPY auth.json /var/www/html/magento/auth.json 
RUN chmod 0644 /etc/cron.d/cronjob
RUN crontab /etc/cron.d/cronjob
RUN touch /var/log/cron.log

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

#RUN docker-php-ext-enable apcu

RUN echo "memory_limit=1G" >> /usr/local/etc/php/conf.d/zz-memory-limit-php.ini
RUN echo "apc.enable=1" >> /usr/local/etc/php/conf.d/zz-apcu.ini
RUN echo "apc.enable_cli=1" >> /usr/local/etc/php/conf.d/zz-apcu.ini
RUN echo "opcache.memory_consumption=512MB" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.max_accelerated_files=60000" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.consistency_checks=0" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.validate_timestamps=0" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.enable_cli=1" >> /usr/local/etc/php/conf.d/zz-opcache.conf

RUN mkdir -p /var/lib/nginx/{body,fastcgi} && \
    chown -R www-data:www-data /var/lib/nginx

RUN touch /run/nginx.pid && \
    chown www-data:www-data /run/nginx.pid
RUN chown www-data:www-data /var/log/nginx/error.log
RUN chown www-data:www-data /var/log/nginx/access.log



COPY etc/nginx /etc/nginx




ARG COMPOSER_AUTH

COPY --from=composer:2.2 /usr/bin/composer /usr/bin/composer
COPY --chown=www-data magento.
RUN cd /var/www/html  && composer install 

COPY --chown=www-data bin bin


ENV MAGE_MODE=developer
EXPOSE 80

CMD service cron start && service nginx start && php-fpm

tried to change PVC and working directory

1

There are 1 best solutions below

0
Gupta On

To fix this issue, you should remove the subPath: pub/media from the volume mount configuration of the Job. This will ensure that data is written to the correct path (/var/www/html/pub/media) and is accessible to both the Deployment and the Job.

apiVersion: batch/v1 
kind: Job
metadata:
  name: example-job
spec:
  template:
    metadata:
      name: example-job
    spec:
      containers:
      - name: example-container
        image: adobemagento2/magento473
        command: ["sh", "-c", "bin/install.sh"]
        envFrom:
         - configMapRef:
             name: mag-config
        volumeMounts:
        - name: web
          mountPath: "/var/www/html/pub/media"
      restartPolicy: Never
      imagePullSecrets:
        - name: regcred
      volumes:
      - name: web
        persistentVolumeClaim:
          claimName: magento-pvc

Note: With this change, both the Deployment and the Job will be using the same path (/var/www/html/pub/media), and the data written by the Job should be accessible to the Deployment.