0

I am having a problem related with PV and PVC and I really do not know how to fix it. Could someone give some insight about it?

Github -> https://github.com/Sarony11/wordpress-k8s/tree/main/k8s

Files: wordpress-pv.yaml (added after comments)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: wp1-pv
  labels:
    app: wordpress
spec:
  capacity:
    storage: 5Gi
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /tmp/data

wordpress-pv-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp1-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: standard
  volumeName: wp1-html

wordpress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wp1-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
      component: app
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
        component: app
    spec:
      containers:
      - image: wordpress:5.6.0-apache
        name: wp1
        env:
          - name: WORDPRESS_DB_HOST
            value: "35.204.214.81"
          - name: WORDPRESS_DB_USER
            value: wordpressdb
          - name: WORDPRESS_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-pass
                key: password
        ports:
        - containerPort: 80
          name: wp1-port
        volumeMounts:
        - name: wp1-pv
          mountPath: /var/www/html
      volumes:
      - name: wp1-pv
        persistentVolumeClaim:
          claimName: wp1-pv-claim

COMANDS

administrator@master-ubuntu:~/learning/wordpress-k8s$ microk8s.kubectl apply -k k8s
secret/mysql-pass-7m4b7ft482 created
service/wp1-clusterip-service created
ingress.networking.k8s.io/ingress-service created
persistentvolume/wp1-pv created
persistentvolumeclaim/wp1-pv-claim created



administrator@master-ubuntu:~/learning/wordpress-k8s$ microk8s.kubectl get 
pv,pvc -n default
NAME                      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
persistentvolume/wp1-pv   5Gi        RWO            Retain           Available           standard                74s

NAME                                 STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/wp1-pv-claim   Pending   wp1-html   0                         standard  

 74s

administrator@master-ubuntu:~/learning/wordpress-k8s$ microk8s.kubectl describe pv wp1-pv
Name:            wp1-pv
Labels:          app=wordpress
Annotations:     <none>
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    standard
Status:          Available
Claim:
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        5Gi
Node Affinity:   <none>
Message:
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /tmp/data
    HostPathType:
Events:            <none>


administrator@master-ubuntu:~/learning/wordpress-k8s$ microk8s.kubectl describe pvc wp1-pv-claim
Name:          wp1-pv-claim
Namespace:     default
StorageClass:  standard
Status:        Pending
Volume:        wp1-html
Labels:        app=wordpress
Annotations:   <none>
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      0
Access Modes:
VolumeMode:    Filesystem
Mounted By:    wp1-app-54ddf5fb78-7f8j6
               wp1-app-54ddf5fb78-hgqmj
               wp1-app-54ddf5fb78-sxn9j
Events:        <none>

The result is still Pending for both PV and PVS and of course, pods are in Pending.

3 Answers3

0

Please check/share the status of pvc and pv.

check the status

#kubectl get pvc,pv -n <namespace>

Describe the pvc and pv.

#kubectl describe pvc <PVC-name> -n namespace
#kubectl describe pv <PV-name> -n namespace
Manjul
  • 44
  • 2
0

So the problem here is that you're not creating any persistent volume.
That's why your pvc remains pending as is your pod.

To make it work, you need to provide a PV that matches the pvc spec

spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: "wp-html"

Below is an example for a PV that should match your pvc

apiVersion: v1
kind: PersistentVolume
metadata:
  name: wp-html
  labels:
    app: wordpress
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/pv/wp-html

This PV will store the data under the /data/pv/wp-html folder on your local node.

Marc ABOUCHACRA
  • 3,155
  • 12
  • 19
0

I have tested it on my environment and can give you some advices. Before starting read how to properly configure dynamic provisioning in Kubernetes - dynamics-provisioning-kubernetes.

  1. Your pv yaml is ok.
  2. Delete volumeName field from you pvc yaml file:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp1-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: standard

Thanks to this your pvc will be bound successfully to existing pv.

  1. Change volumeMounts value in your Deployment yaml file, for example to wp1-pv-storage:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wp1-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
      component: app
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
        component: app
    spec:
      containers:
      - image: wordpress:5.6.0-apache
        name: wp1
        env:
          - name: WORDPRESS_DB_HOST
            value: "35.204.214.81"
          - name: WORDPRESS_DB_USER
            value: wordpressdb
          - name: WORDPRESS_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-pass
                key: password
        ports:
        - containerPort: 80
          name: wp1-port
        volumeMounts:
        - name: wp1-pv-storage
          mountPath: /var/www/html
      volumes:
      - name: wp1-pv-storage
        persistentVolumeClaim:
          claimName: wp1-pv-claim
Malgorzata
  • 6,409
  • 1
  • 10
  • 27