Effortlessly migrate Kubernetes PersistentVolumeClaims (PVCs) with a single command. Say goodbye to complicated PVC migration workflows and hello to a fast, customizable, and API-powered solution that just works. Whether you're migrating across clusters, within a single cluster, or even in airgapped environments, easy-migrate-pvc has you covered.
- The script is designed for clarity and simplicity.
- Minimal dependencies: all you need is
kubectl
, akubeconfig
, and permission to create pods.
- Data is piped through API connections, ensuring secure and efficient transfers.
- Optimized for quick migrations, even for large volumes of data.
- Tailor the migration process to your specific needs.
- Use the script out-of-the-box or modify it to fit your environment.
- Cross-cluster migrations: Transfer PVCs between two clusters without requiring them to directly communicate.
- In-cluster migrations: Move PVCs between namespaces within the same cluster.
- Airgapped environments: Migrate data even in isolated environments with no direct internet access.
- Parallelization: Migrate multiple PVCs simultaneously for faster overall transfer. The process is bottlenecked only by the network bandwidth from the workstation to the API servers.
- API-Powered: No need for rsync, SCP, or other tools. Data transfer is handled entirely via Kubernetes API calls.
- Versatile Use Cases: Suitable for migrations involving live workloads, staging environments, or complex multi-cluster setups.
- Minimal Setup: Requires only
kubectl
and proper permissions. - Handles Edge Cases: Works in scenarios where traditional rsync solutions over the internet aren't possible.
-
Cross-Cluster Migrations
- You're migrating from an on-premises Kubernetes cluster to a cloud-based cluster but can't set up direct network connectivity between the two.
-
Namespace Cleanup and Reorganization
- Consolidating resources into a new namespace within the same cluster while retaining PVC data.
-
Disaster Recovery in Airgapped Environments
- Transferring PVCs between environments that are fully isolated from external networks.
-
Test Environment Cloning
- Duplicate PVCs from production to a staging or testing environment for debugging or development purposes.
-
Cluster Upgrades
- Migrate workloads to a fresh cluster during an upgrade, without worrying about data loss or downtime.
Below is a high-level diagram illustrating the migration process:
- Source Cluster: Temporary pod reads the data from the source PVC.
- Workstation: The script streams data through kubectl commands, piping stdout from the source to stdin of the destination.
- Destination Cluster: Temporary pod writes the data to the destination PVC.
+------------------+ +-------------+ +--------------------+
| | | | | |
| Source Cluster | API | Workstation | API | Destination Cluster|
| (src kubeconfig)|-------->| (kubectl) |-------->| (dst kubeconfig) |
| | | | | |
+------------------+ +-------------+ +--------------------+
| |
[SRC PVC] [DST PVC]
This ensures no direct connectivity is needed between the source and destination clusters.
- Run the script with the required variables provided directly in the execution command.
- The script creates temporary pods to transfer data between source and target volumes via
kubectl
. - Data is securely piped through the Kubernetes API, with no direct connectivity required between clusters.
- The source and destination PVCs must already exist before running the script.
- The source and destination PVCs must not be mounted by other pods during the migration to ensure data consistency.
kubectl
installed and configured.- Access to
kubeconfig
files for both source and destination clusters (if applicable). - Permissions to create and delete pods in both source and destination namespaces.
SRC_KUBECONFIG=~/.kube/config-src \
SRC_NAMESPACE=prod \
SRC_PVC_NAME=app-data \
DST_KUBECONFIG=~/.kube/config-dst \
DST_NAMESPACE=prod \
DST_PVC_NAME=app-data \
easy-migrate-pvc.sh
MIG_CONTAINER_IMAGE=ubuntu \ # Can be any image that has the 'tar' command utility. Defaults to 'ubuntu'.
DEBUG=true \ # Set to 'true' if troubleshooting is needed.
Download the script directly:
curl -LO https://raw.githubusercontent.com/mogindi/easy-migrate-pvc/main/easy-migrate-pvc.sh
chmod +x easy-migrate-pvc.sh
Provide the required variables directly in the execution command:
SRC_KUBECONFIG=~/.kube/config-src \
SRC_NAMESPACE=prod \
SRC_PVC_NAME=app-data \
DST_KUBECONFIG=~/.kube/config-dst \
DST_NAMESPACE=prod \
DST_PVC_NAME=app-data \
easy-migrate-pvc.sh
SRC_KUBECONFIG=~/.kube/config-src \
SRC_NAMESPACE=prod \
SRC_PVC_NAME=app-data \
DST_KUBECONFIG=~/.kube/config-dst \
DST_NAMESPACE=prod \
DST_PVC_NAME=app-data \
easy-migrate-pvc.sh
SRC_KUBECONFIG=~/.kube/config \
SRC_NAMESPACE=dev \
SRC_PVC_NAME=dev-db-data \
DST_KUBECONFIG=~/.kube/config \
DST_NAMESPACE=staging \
DST_PVC_NAME=staging-db-data \
easy-migrate-pvc.sh
Note: This process should be run from a location that has access to both clusters' API endpoints.
-
Deploy Application Manifests at Destination
- Ensure the application manifests (e.g., Deployment, StatefulSet, Service) are deployed in the destination namespace.
-
Snapshot Source PVC (Optional)
- If your storage provider supports it, create a snapshot of the source PVC for additional safety.
-
Set Replicas to 0
- Scale down the application at both source and destination to avoid data inconsistency:
kubectl scale deployment my-app --replicas=0 --kubeconfig=$SRC_KUBECONFIG --namespace=$SRC_NAMESPACE kubectl scale deployment my-app --replicas=0 --kubeconfig=$DST_KUBECONFIG --namespace=$DST_NAMESPACE
- Scale down the application at both source and destination to avoid data inconsistency:
-
Run easy-migrate-pvc
- Execute the migration script with the appropriate variables:
SRC_KUBECONFIG=~/.kube/config-src \ SRC_NAMESPACE=prod \ SRC_PVC_NAME=app-data \ DST_KUBECONFIG=~/.kube/config-dst \ DST_NAMESPACE=prod \ DST_PVC_NAME=app-data \ easy-migrate-pvc.sh
- Execute the migration script with the appropriate variables:
-
Scale Up Application
-
Once the migration is complete, scale the application back up at the destination:
kubectl scale deployment my-app --replicas=3 --kubeconfig=$DST_KUBECONFIG --namespace=$DST_NAMESPACE
-
Update DNS and networking configurations as needed to point to the new application endpoint.
-
- Temporary pods are automatically created and cleaned up during the process.
- Ensure your destination PVC has adequate storage capacity to handle the data from the source PVC.
- Test migrations in non-production environments before using the script in production.
No Differential Copies: The destination PVC is completely wiped and recopied in every run. This makes the script ideal for first-time data migrations but not for incremental updates.
Found a bug or have an idea for an improvement? Contributions are welcome! Feel free to submit a pull request or open an issue on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.