How to have input data ready for apps on K8S

10/8/2019

How to have the input data ready before I deploy a POD on K8S? As I understand, persistent volume is dynamically created using PVC (persistent volume claim), so in a POD yaml file, we can set the PVC and mount path like this:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: nginx
      volumeMounts:
      - mountPath: "/var/www/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim

The problem is, how can I upload the data before I deploy a POD? What I want is to have the data ready and persistent somewhere on K8S, and then when I deploy the POD, and expose it as service, the service can immediately access the data.

-- Jaylin
google-cloud-storage
kubernetes
kubernetes-pod
kubernetes-pvc
persistent-volumes

3 Answers

10/11/2019

Thank you Guys, so following coderanger and Rodrigo Loza's suggestions, I was able to create a NAS file system and mount it onto multiple PODs. One POD can be used to pre-load the data. Other POD then can access it when data is ready. I'm from HPC background, knowing clearly where is storage is my hobby.

-- Jaylin
Source: StackOverflow

10/9/2019

According to your description, you need is a persistent volume. An example of this would be a NFS persistence for which you would define the following yaml.

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: $PV_NAME
  namespace: $NAMESPACE
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  nfs:
    path: /nfs
    server: $SERVER_ADDRESS # 10.128.15.222 for instance
  persistentVolumeReclaimPolicy: Retain

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: $PVC_NAME
  creationTimestamp: null
  labels:
    app: $PVC_NAME
  namespace: $NAMESPACE
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

After creating a PV and PVC, you would mount it in a deployment like this.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: $POD_NAME
  labels:
    app: $POD_NAME
  namespace: $NAMESPACE
spec:
  replicas: 1
  selector:
    matchLabels:
      app: $POD_NAME
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: $POD_NAME
    spec:
      containers:
      - name: $POD_NAME
        image: $DOCKER_IMAGE
        volumeMounts:
          - mountPath: /testing-path
            name: $VOLUME_NAME
      volumes:
      - name: $VOLUME_NAME
        persistentVolumeClaim:
          claimName: $PVC_NAME
-- Rodrigo Loza
Source: StackOverflow

10/9/2019

Mount it on another pod somewhere that does the pre-load. Alternatively you could do some fancy stuff with an initContainer.

-- coderanger
Source: StackOverflow