How to pause and resume kubernetes pods with data?

8/14/2019

I have multiple micro services in my project. I want to dynamically pause and resume them without losing the data.

For example: I am deploying an theia ide and user created a folder. I want to down this service for sometime and resume again with the data.

References: https://github.com/theia-ide/theia

I have already tried with reducing replicas to 0 and 1. It removes the data. I want the data to be persistent.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: servicetest
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: servicetest
  template:
    metadata:
      labels:
        run: servicetest
    spec:
      containers:
      - image: gcr.io/YYYY-ZZZZ-249311/test
        imagePullPolicy: IfNotPresent
        name: servicetest
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
            - name: data
              mountPath: /data/
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: service-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  labels:
    run: servicetest
  name: servicetest
spec:
  ports:
  - name: web
    port: 80
    protocol: TCP
    targetPort: 3000
  - name: deployport
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    run: servicetest
  type: LoadBalancer


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: service-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
-- Imrahamed
google-kubernetes-engine
kubernetes

1 Answer

8/14/2019

Whether your underlying storage gets deleted depends on the persistent volume's reclaim policy. If you set the policy to Retain, it should keep your pod's PV around for later rather than deleting it's contents and purging the volume.

Also worth looking into statefulset if you're using this deployment of size 1, because deployments are "at least N" as opposed to statefulsets being "at most N" replicas. Statefulsets also let you have a different volume associated with each replica.

-- Anirudh Ramanathan
Source: StackOverflow