Kubernetes Level 3 Task 4 Persistent Volumes in Kubernetes

I have attempted multiple times this question and at each time i’m unable to find out what went wrong.

below is the configuration i have used.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-devops
spec:
  storageClassName: manual
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/finance

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-devops
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

---

apiVersion: v1
kind: Pod
metadata:
  name: pod-devops
spec:
  containers:
    - name: container-devops
      image: nginx:latest
      volumeMounts:
        - mountPath: /var/www/html
          name: data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: pvc-devops

---

apiVersion: v1
kind: Service
metadata:
  name: web-devops
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30008

after clicking on the website it shows 502 bad gateway.
upon checking the service it is correctly mapped and i can verify from “k get svc” it shows “80:30008 TCP”.

help me on this issue.

I would try chaging

  hostPath:
    path: /mnt/finance

to

  hostPath:
    path: /mnt/finance
    type: DirectoryOrCreate

and see if that helps. You might also check the events in the default namespace and see if that tells you anything about that PV. But I’m guessing that’s the cause.

tried the above by changing the host path type, still I’m facing the issue. as suggested took a look at the events and found that the pvc provisioning is failed stating the storage class manual is not found.

but in the task it is mentioned as the storage class is manual. how to overcome this.

@rob_kodekloud @Alistair_KodeKloud please help me finish this task

The lab is fine. It might help if you post the YAML for what you have created.

Please be sure to use code blocks so it looks like this:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

And not like this, because it is illegible

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-xfusion
spec:
  storageClassName: manual
  capacity:
    storage: 4Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/finance
    type: DirectoryOrCreate

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-xfusion
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

---

apiVersion: v1
kind: Pod
metadata:
  name: pod-xfusion
spec:
  containers:
    - name: container-xfusion
      image: nginx:latest
      volumeMounts:
        - mountPath: /var/www/html
          name: data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: pvc-xfusion

---

apiVersion: v1
kind: Service
metadata:
  name: web-xfusion
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30008
---

@Alistair_KodeKloud this is the YAML. I have tried it again, still observing the error and unable to complete the task

Have you checked you have the correct mount path?

You must run a test container of nginx, then kubectl exec inside it and check the nginx configuration to know where its default documents directory is.

I can tell you that it is not /var/www/html - that is the default for Apache.

Check also the selectors on your service. Recall the easiest way to create a service for a pod is to run the pod first, then use kubectl expose pod

Updated the volumeMounts path to /usr/share/nginx/html, which is the default document root for the nginx container.
After changing this the lab successfully executed.