claim volume without data loss using volumeclaimtemplate

1.4k views Asked by At

I have a application of workload deployment and need to change it to statefulset

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  labels:
    app: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: app
        image: nginx:1.14.2
        ports:
        - containerPort: 80 
        volumeMounts:
        - name: data
          subPath: app/log
          mountPath: /opt/app/log
      volumes:
      - name: data
        peristentVolumeClaim:
           claimName: pv-app-claim

PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-app
  labels:
    pv: app
spec:
  storageClassName: "default"
  capacity:
    storage: 8Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeclaimPolicy: Retain
  nfs:
     server: someIP
     path: "/somepath"

PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-app-claim
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 8Gi
  selector:
    matchLabels:
        pv: app

I have tried to change the required file but struck in a place where I need the data in the volume to be there even when I'm moving to statefulset. In statefulset we use volumeclaimtemplate this is where i'm struck how to retain the data and claim with volumeclaimtemplate.

Note: I'm going to use only one pod

1

There are 1 answers

0
confused genius On
  • If you want to use existing PVC in statefulset then you should not mention it under volumeclaimtemplate as volumeclaimtemplate will create a new PVC .
  • You should mention it under pod spec just like you mention in a deployment

Example:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web1
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: "nginx"
  replicas: 1 
  template:
    metadata:
      labels:
        app: nginx 
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx1
        imagePullPolicy: IfNotPresent
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web1
        volumeMounts:
        - name: www1
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www1
        peristentVolumeClaim:
           claimName: pv-app-claim