Blog

How to Migrate Persistent Volumes on Kubernetes Easily

Written By

Florian Pialoux

Have you faced a situation where you need to move data across two persistent volumes, and you weren't so sure how to achieve this? Recently, a German Cloud engineer Utku Özdemir decided to help the community with a CLI Tool/kubectl plugin, pv-migrate, to resolve the problem of migrating persistent volumes.

Table of Contents

Installing PV-Migrate

At the time of writing,pv-migrate v1.0.1 is the latest version and we are running the binary for Linux.

   
$ wget https://github.com/utkuozdemir/pv-migrate/releases/download/v0.6.0/pv-migrate_0.6.0_linux_x86_64.tar.gz

$ tar -xvzf pv-migrate_0.6.0_linux_x86_64.tar.gz

$ sudo mv pv-migrate /usr/local/bin

$ pv-migrate --help

   

Alternatively, you might be running it on MacOS/Windows or even just need a Docker image with this pv-migrate’s binary pre-installed. If that's the case here’re the installation instructions.

Methods of Migrating Persistent Volumes on Kubernetes

pv-migrate uses different methods to migrate persistent volumes on Kubernetes:

  • lbsvc: Load Balancer Service, this will run rsync+ssh over a Kubernetes Service type LoadBalancer. This is the method you want to use if you're migrating PVC from different Kubernetes clusters.
  • mnt2: Mounts both PVCs in a single pod and runs a regular rsync. This is only usable if source and destination PVCs are in the same namespace.
  • svc: Service, Runs rsync+ssh in a Kubernetes Service (ClusteRIP). Only applicable when the source and destination PVCs are in the same Kubernetes cluster.

Note that when you run the command explained below, you don't need to specify the method as it will try all of them and figure out on its own which one to use.

How to Migrate Persistent Volumes on Kubernetes with pv-migrate

For this guide, we had two PVCs similar to the diagram below that shows the migration operation we were trying to accomplish:

Migrating Persistent volumes on Kubernetes with pv-migrate

Once you have downloaded and installed the CLI, you may now run the following code snippet (don't hesitate to run pv-migrate migrate --help if you're looking at more parameters):

   
      pv-migrate migrate \                                                                                        
  --source-namespace other \
  --dest-namespace grafana \
  --ignore-mounted \
  grafana-two grafana
🚀  Starting migration
💡  PVC grafana-two is mounted to node minikube, ignoring...
💡  PVC grafana is mounted to node minikube, ignoring...
💭  Will attempt 3 strategies: mnt2,svc,lbsvc
🚁  Attempting strategy: mnt2
🦊  Strategy 'mnt2' cannot handle this migration, will try the next one
🚁  Attempting strategy: svc
🔑  Generating SSH key pair
🔑  Creating secret for the public key
🚀  Creating sshd pod
⏳  Waiting for the sshd pod to start running
🚀  Sshd pod started
🔑  Creating secret for the private key
🔗  Connecting to the rsync server
📂  Copying data... 100% |█████████████████████████████████████████████████████████████████████████| ()
🧹  Cleaning up
✨  Cleanup successful
✅  Migration succeeded
   

My two PVCs shown by k9s:

PVCs as shown by k9s

For a brief moment, you can see the pod created

pod created when migrating persistent volumes on Kubernetes with pv-migrate

Conclusion

For DevOps Engineers, Kubernetes is not an unknown territory. Some of you might have to deal with expanding a PVC where the StorageClass or provisioner doesn't support volume expansion Moving to another cloud provider, or even simply moving data across two PVCs in the same cluster like the example we've presented above.

We hope that pv-migrate will be useful to you as we’ve found it really resourceful for our projects.

You may also be interested in:

Docker Compose vs Kubernetes: The Top 4 Main Differences

Best CI/CD Tools for DevOps: A Review of the Top 10

Single-Cloud vs Multi-Cloud: 7 Key Differences

Why You Need a DevOps Consultant

How to Run a Docker Container on the Cloud: Top 5 CaaS Solutions

Container Security: Top 5 Best Practices for DevOps Engineers

Bluelight Consulting is a nearshore DevOps & Software Outsourcing company that helps startups, SaaS, and enterprises with cutting-edge solutions.

More cost-effective than hiring in-house, with Nearshore Boost, our nearshore software development service, you can ensure your business stays competitive with an expanded team and a bigger global presence, you can be flexible as you respond to your customers’ needs.

Learn more about our services by booking a free consultation with us today!

Let us solve your business’ biggest challenges

Book a free Consultation
Save 50+ hours of project time per developer on interviewing.
Tell us the skills you need and we'll find the best developer for your needs in days, not weeks.

Discuss your project with us today!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.