spring-bootkubernetesnfspersistent-volumespersistent-volume-claims

How to Write File from PersistentVolumeClaims in SpringBoot


I have a NFS server, It can access nfs server with kubernetes Storageclass. I want to write and read File from my Spring Boot project to nfs server. how can I do that?

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: fileserver-nfs-pv
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: fileserver-nfs-pvc
  nfs:
    server: 10.0.75.1
    path: /export/k8s-data/fileserver

pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: fileserver-nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi

My spring boot project deployment yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: authentication
  name: authentication
  namespace: auth
spec:
  selector:
    matchLabels:
      app: authentication
  strategy:
    rollingUpdate:
      maxSurge: 15%
      maxUnavailable: 0
    type: RollingUpdate
  replicas: 1
  template:
    metadata:
      labels:
        app: authentication
    spec:
      containers:
        - name: authentication
          image: ip:8080/authentication:0.0.0
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          resources:
            limits:
              cpu: "0.9"
              memory: 1Gi
            requests:
              cpu: "0.6"
              memory: 512Mi
   

My spring boot project is kubernetese deploy. I want to read and write to nfs server using java.nio package. How should I define my deployment.yaml file?


Solution

  • After creating PersistentVolumeClaim and PersistentVolume, adding

    volumeMounts:
    - mountPath: /dms
      name: nfs-pvc-volume 
    

    and

    volumes:
    - name: nfs-pvc-volume
      persistentVolumeClaim:
        claimName: dms-auth-nfs-pvc
    

    commands to my deployment.yaml file solved my problem.

    deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: authentication
      name: authentication
      namespace: auth
    spec:
      selector:
        matchLabels:
          app: authentication
      strategy:
        rollingUpdate:
          maxSurge: 15%
          maxUnavailable: 0
        type: RollingUpdate
      replicas: 1
      template:
        metadata:
          labels:
            app: authentication
        spec:
          containers:
            - name: authentication
              image: ip:8080/authentication:0.0.0
              imagePullPolicy: Always
              ports:
                - containerPort: 8080
              resources:
                limits:
                  cpu: "0.9"
                  memory: 1Gi
                requests:
                  cpu: "0.6"
                  memory: 512Mi
              volumeMounts:
              - mountPath: /dms
                name: nfs-pvc-volume
          volumes:
            - name: nfs-pvc-volume
              persistentVolumeClaim:
                claimName: dms-auth-nfs-pvc
    

    I created PersistentVolumeClaim and PersistentVolume on kubernetes

    PersistentVolumeClaim

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: dms-auth-nfs-pvc
      namespace: auth
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 30Gi
    

    PersistentVolume

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: dms-auth-nfs-pv
    spec:
      capacity:
        storage: 30Gi
      accessModes:
        - ReadWriteMany
      claimRef:
        apiVersion: v1
        kind: PersistentVolumeClaim
        name: dms-auth-nfs-pvc
      nfs:
        server: nfs:ip
        path: /dms