Home > Mobile >  How do I define a persistent storage claim for my deployment?
How do I define a persistent storage claim for my deployment?

Time:12-17

I get this error message:

Deployment.apps "nginxapp" is invalid: spec.template.spec.containers[0].volumeMounts[0].name: Not found: "nginx-claim"

Now, I thought deployment made a claim to a persistant storage, so these are det files I've run in order:

First, persistant volume to /data as that is persistent on minikube (https://minikube.sigs.k8s.io/docs/handbook/persistent_volumes/):

apiVersion: v1
kind: PersistentVolume

metadata:
  name: small-pv

spec:

  capacity:
    storage: 1Gi

  accessModes:
  - ReadWriteOnce

  persistentVolumeReclaimPolicy: Retain

  storageClassName: local-storage

  local:
    path: /data

  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - my-node

Then, for my nginx deployment I made a claim:

apiVersion: v1
kind: PersistentVolumeClaim

metadata:
  name: nginx-claim

spec:
  storageClassName: manual

  accessModes:
    - ReadWriteOnce

  resources:
    requests:
      storage: 1Gi

Before service I run the deployment, which is the one giving me the error above, looks like this:

apiVersion: apps/v1
kind: Deployment

metadata:
  labels:
    app: nginxapp

  name: nginxapp

spec:
  replicas: 1

  volumes:
    - persistentVolumeClaim:
        claimName: nginx-claim

  selector:
    matchLabels:
      app: nginxapp

  template:
    metadata:
      labels:
        app: nginxapp

    spec:
      containers:
      - name: nginx
        image: nginx:latest
        
        ports:
        - containerPort: 80

        volumeMounts:
        - mountPath: "/data/www"
          name: nginx-claim
  1. Where did I go wrong? Isnt it deployment -> volume claim -> volume?

  2. Am I doing it right? The persistent volume is pod-wide (?) and therefore generally named. But the claim is per deployment? So thats why I named it nginx-claim. I might be mistaken here, but should not bug up this simple run doh.

  3. In my deployment i set mountPath: "/data/www", this should follow the directory already set in persistent volume definition, or is building on that? So in my case I get /data/data/www?

CodePudding user response:

Looks like name: is missing under volumes: in the deployment manifest. Can you try the following in the deployment manifest:

  volumes:
    - name: nginx-claim
      persistentVolumeClaim:
        claimName: nginx-claim

Here is the documentation.

CodePudding user response:

Try change to:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-claim
spec:
  storageClassName: local-storage  # <-- changed
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

At your deployment spec add:

...
spec:
  replicas: 1
  volumes:
  - name: nginx-claim  # <-- added
    persistentVolumeClaim:
      claimName: nginx-claim
...
  • Related