Existing Wordpress Deployment on Kubernetes with Persistent volume returning error 404 on deployment

This is my PVC yaml file

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: my-pvc
spec:
    accessModes:
        - ReadWriteOnce
    resources:
        requests:
            storage: 1Gi
    storageClassName: do-block-storage

This is my deployment yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysite
  labels:
    tier: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mysite
      tier: backend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysite
        tier: backend
    spec:
      containers:
        - name: mysite
          image: my-image
          ports:
            - containerPort: 80
          volumeMounts:
            - name: config
              mountPath: /etc/nginx/sites-enabled
            - name: my-pvc
              mountPath: /var/www/app
      volumes:
        - name: my-pvc
          persistentVolumeClaim:
            claimName: my-pvc
        - name: config
          configMap:
            name: wordpress-nginx-config
            items:
              - key: config
                path: default.conf
      imagePullSecrets:
        - name: registry-secret

This is my wordpress config map

apiVersion: v1
kind: ConfigMap
metadata:
  name: wordpress-nginx-config
  labels:
    tier: backend
data:
  config: |
    server {
      listen 80;
      index index.php index.html;
      server_name _;
      error_log /dev/stdout info;
      access_log /dev/stdout;
      root var/www/app;
      location /.git {
         deny all;
         return 403;
      }
      location / {
          try_files $uri $uri/ /index.php?$args;
              }
      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_pass unix:/var/run/php-fpm.sock;
        }
        location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
                expires           5d;
        }
        location ^~ /.well-known {
          allow all;
          auth_basic off;
        }
 }

Visiting my service URL, I still get error 404. I will appreciate every help I can get. I deployed without the PVC and it worked, I know the issue is with my PVC configuration.

I suspect in your container your app is located in /var/www/app but you are also mounting the PV in to the same location, thus mounting over your application.

If that makes sense could you try mounting your PV to somewhere else like /var/www/app/data maybe?

Kind regards,
Stephen

Thank you @stephendotcarter. Using this approach, How will the wordpress not lose data after a pod is recreated?

Can you just confirm what is stored in /var/www/app?
Is it the index.php + the rest of your Wordpress stuff?

Kind regards,
Stephen

@stephendotcarter,

Yes it is. My full wordpress site

Thank you

So you definitely can’t mount the PV there because it’s effectively overwriting Wordpress itself.

I think you need to mount the PV elsewhere and then set the Wordpress params such as WP_CONTENT_DIR (and maybe others) such that it map to a directory in the PV.

That way Wordpress will continue to run from /var/www/app but when uploading content or plugins they will get written to folder inside the PV.

Sorry I can’t give exact details as I’m not that familiar with Wordpress but I think you will get the idea.

Kind regards,
Stephen

Thanks @stephendotcarter. I understand how this works better now. It’s my first foray and thanks to you, I understand now