How to create a volume in kubernetes that is not destroyed when the pods die?

8/13/2018

I have a docker image that when created should check if the volume is empty, in case it should initialize it with some data. This saved data must remain available for other pods with the same or different image.

enter image description here

What do you recommend me to do?

-- Hector Oliveros
devops
gcloud
kubernetes

3 Answers

8/21/2018

Looking at the answers, would it not be simpler to create an NFS Persistent Volume and then allow the pods to mount the PV's?

You can use the writemany which should alleviate a deadlock.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: shared-volume
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: ""
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

Persistent Volumes

-- AussieBunyip
Source: StackOverflow

8/13/2018

You have 2 options:

  1. First option is to mount the pod into the node and save the data in the node so when new pod will create in the same node it will have an access to the same volume (persistent storage location).

Potential problem: 2 pods on the same node can create deadlock for the same resource (so you have to manage the resource).

  1. Shared storage meaning create one storage and every pod will claim storage in the same storage.

I strongly suggest that you will take the next 55 minutes and see the webinar below: https://www.youtube.com/watch?v=n06kKYS6LZE

-- Oron Golan
Source: StackOverflow

8/13/2018

I assume you create your pods using Deployment object in Kubernetes. What you want to look into is a StatefulSet, which, in opposite to deployments, retains some identity aspects for recreated pods including to some extent network and storage.

It was introduced specifically as a means to run services that need to keep their state in kube cluster (ie. running databases queues etc.)

-- Radek 'Goblin' Pieczonka
Source: StackOverflow