How to Import Existing Data into Longhorn Volumes on a Kubernetes Cluster
Recently, I deployed my Kubernetes cluster in my homelab and decided to use Longhorn as the storage provider. One question that came up was: How do I import my existing data into Longhorn volumes?
In this guide, I’ll walk you through the process of exporting data from an existing container, creating a Longhorn volume, and importing that data into the new volume. Let’s get started!
First, we need to export the data from the existing container.
docker exec to open a shell in the container you want to export data from:sudo docker exec -it gitea /bin/shcd /data/lsgit gitea sshtar -czf gitea.tar.gz .docker cp to copy the tarball to your host:docker cp gitea:/data/gitea.tar.gz .Successfully copied 10.3MB to /home/remco/.Now that we have exported the data, let's move on to creating the Longhorn volume where we will import this data.
There are different ways to create a volume in Longhorn. For simplicity, we'll use the Longhorn UI to create a Persistent Volume Claim (PVC).


kubectl get pvc -n giteaNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEgitea-data Bound gitea-data 10Gi RWO longhorn-static 22sTo copy the data to the Longhorn volume, we’ll need a pod that can mount the volume. For this example, we’ll use a simple busybox pod, but you can use any container you prefer.
---apiVersion: v1kind: Podmetadata: name: busybox labels: app: busyboxspec: containers: - image: busybox name: busybox command: - sleep - "3600" volumeMounts: - name: data mountPath: "/data" volumes: - name: data persistentVolumeClaim: claimName: gitea-datakubectl apply -f busybox.yaml -n gitea$ kubectl get pods -n giteaNAME READY STATUS RESTARTS AGEbusybox 1/1 Running 0 70sgitea-5df8b6db65-6fkc8 1/1 Running 0 25mNow that the pod is running, we can proceed with copying the data.
kubectl cp command to copy the tar file into the busybox pod, specifically to the /data directory:kubectl cp -n gitea ./gitea.tar.gz busybox:/data/kubectl exec -n gitea --stdin --tty busybox -- /bin/shcd /datatar -xzf gitea.tar.gz$ kubectl delete -f busybox.yaml -n giteaYou’ve now imported your existing data into a Longhorn volume. You can deploy your applications and attach the Longhorn volume as needed.
With this approach, you can easily migrate your data from old containers or volumes to new Longhorn-backed PVCs in your Kubernetes cluster.
Feel free to customize this process depending on your setup. Longhorn makes managing persistent storage in Kubernetes seamless, even in homelabs!