Persistent Volumes in Kubernetes - not accessible

I appreciate any hint or help.


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

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

apiVersion: v1
kind: Pod
metadata:
name: pod-devops
spec:
volumes:
- name: storage-devops
persistentVolumeClaim:
claimName: pvc-devops
containers:
- name: container-devops
image: httpd:latest
ports:
- containerPort: 80
volumeMounts:
- name: storage-devops
mountPath: /usr/local/apache2/htdocs

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

The problem is not, or not entirely with the volume; the error message in the screenshot says the website is not accessible and the reason is that the selector is incorrect.

You have:

selector:
  pod: pod-devops

But there is no label pod defined in the Pod definition.

if you add to the Pod in the metadata block:

  labels:
    pod: pod-devops

as in:

apiVersion: v1
kind: Pod
metadata:
  name: pod-devops
spec:
...

It should work.

Small note, when you paste code, use </> button or add three backtick ``` before and after.

Your code will look like this:

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

---

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

--- 

apiVersion: v1
kind: Pod
metadata:
  name: pod-devops
  labels:
    pod: pod-devops
spec:
  volumes:
    - name: storage-devops
      persistentVolumeClaim:
        claimName: pvc-devops
  containers:
    - name: container-devops
      image: httpd:latest
      ports:
        - containerPort: 80
      volumeMounts:
        - name: storage-devops
          mountPath: /usr/local/apache2/htdocs


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

1 Like