Skip to main content

title: Easily migrate kubernetes storage backend date: 2023-07-10 draft: true tags:

  • kubernetes
  • storage
  • linux
  • homelab
  • longhorn

I was having some nasty performance issues with some of the applications I host in my homelab. Looking at the logs the application was freezing up because the SQLite database backend was locking. Some research reveleved that this was likley caused because my persistent storage backend was NFS.

As I’m not a hyperscaler, I didn’t have any other options for storage backends until I found LONGHORN. Longhorn allow you to create PersistentVolumeClaims that are based on disk images spread accross mutliple nodes for redundancy. With a simple kubectl apply I had Longhorn running.

Some of my containers had some data that I didn’t really care about, so I tested in “production” here. The concept is this:

  • Alert my yaml file to add the new storage backend
  • Change the container configuration to point to the NEW, blank storage backend
  • Create an initContainer that will migrate the data
  • Remove the initContainer once the migration is done

This is what it looked like.

The initContainers section:

initContainers:
        - name: rsync-migrator
          image: eeacms/rsync
          command: ["rsync", "-av", "/config-nfs/","/config/"]
          volumeMounts:
            - name: app-data-lh
              mountPath: /config
            - name: app-data
              mountPath: /config-nfs

The new volume:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: longhorn
  resources:
    requests:
      storage: 2Gi