C-nan
C-nan

Reputation: 364

Kubernetes share storage between replicas

We have Kubernetes running on our own servers. For Persistent Storage we have a NFS server. This works great.

Now we want to deploy an application with multiple replicas that should have shared storage between them, but the storage should not be persistent. When the pods are deleted, the data should be gone as well.

I was hoping I could achieve it with the following

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: shared-data
        emptyDir: {}

      containers:
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        name: nginx
        volumeMounts:
        - name: shared-data
          mountPath: /shared-data
        resources:
          limits:
            memory: 500Mi

All replicas have /shared-data, but when 1 replica stores data in that folder, the other replica's cannot see the file, so it is not shared.

What are my options?

Upvotes: 0

Views: 1176

Answers (1)

Emruz Hossain
Emruz Hossain

Reputation: 5528

You can use a PVC to share data between the pods. Then, you can setup a preStop lifecycle hook for the pods to cleanup the data when the pod gets deleted.

Here, is an example of adding preStop hook on a pod: https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/

Upvotes: 2

Related Questions