Difference between revisions of "Pvc migration testing"
Jump to navigation
Jump to search
(Created page with "# Migrate many PVCs from one storage class to another in same namespace Use these scripts for migration and testing ## create-test-pvc.sh ``` #!/bin/bash set -eu NS=${1:-d...") |
(No difference)
|
Revision as of 19:40, 23 July 2025
Migrate many PVCs from one storage class to another in same namespace
Use these scripts for migration and testing
create-test-pvc.sh
#!/bin/bash
set -eu
NS=${1:-default}
SC=${2:-managed-csi-hdd}
PVC_NAME=test-pvc
POD_NAME=test-pod
cat <<EOF | kubectl apply -n $NS -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: $PVC_NAME
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 1Gi
EOF
cat <<EOF | kubectl apply -n $NS -f -
apiVersion: v1
kind: Pod
metadata:
name: $POD_NAME
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "3600"]
volumeMounts:
- name: vol
mountPath: /data
volumes:
- name: vol
persistentVolumeClaim:
claimName: $PVC_NAME
EOF
kubectl wait --for=condition=Ready pod/$POD_NAME -n $NS --timeout=60s
kubectl exec -n $NS $POD_NAME -- sh -c 'echo "This is a test." > /data/test.txt'
# kubectl delete pod/$POD_NAME pvc/$PVC_NAME -n $NS
kubectl delete pod/$POD_NAME -n $NS
swap-storage-class.sh
#!/bin/bash
set -euo pipefail
NS="${1:?Namespace required}"
SRC_SC="${2:?Source storage class required}"
DEST_SC="${3:?Destination storage class required}"
pvcs=$(kubectl get pvc -n "$NS" -o jsonpath='{range .items[*]}{.metadata.name}{"|"}{.spec.storageClassName}{"|"}{.spec.resources.requests.storage}{"\n"}{end}' | awk -F'|' -v sc="$SRC_SC" '$2==sc{print $1 "|" $3}')
if [[ -z "$pvcs" ]]; then
echo "No PVCs found in namespace $NS with storage class $SRC_SC"
exit 0
fi
for line in $pvcs; do
pvc=$(cut -d'|' -f1 <<<"$line")
size=$(cut -d'|' -f2 <<<"$line")
tmp_pvc="${pvc}-tmp"
echo "🔷 Scaling down deployments/statefulsets using PVC $pvc..."
# scale down deployments
for deploy in $(kubectl get deploy -n "$NS" -o name | xargs -n1); do
if kubectl get "$deploy" -n "$NS" -o json | grep -q "$pvc"; then
echo "Scaling $deploy to 0"
kubectl scale "$deploy" -n "$NS" --replicas=0
fi
done
# scale down statefulsets
for sts in $(kubectl get sts -n "$NS" -o name | xargs -n1); do
if kubectl get "$sts" -n "$NS" -o json | grep -q "$pvc"; then
echo "Scaling $sts to 0"
kubectl scale "$sts" -n "$NS" --replicas=0
fi
done
echo "🔷 Creating temporary PVC $tmp_pvc with $DEST_SC..."
cat <<EOF | kubectl apply -n "$NS" -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: $tmp_pvc
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: $size
storageClassName: $DEST_SC
EOF
echo "🔷 Migrating data from $pvc -> $tmp_pvc..."
pv-migrate \
--source "$pvc" \
-n "$NS" \
--dest "$tmp_pvc" \
-N "$NS" \
--compress \
--ignore-mounted
echo "🔷 Deleting original PVC $pvc..."
kubectl delete pvc "$pvc" -n "$NS"
echo "🔷 Re-creating original PVC $pvc with $DEST_SC..."
cat <<EOF | kubectl apply -n "$NS" -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: $pvc
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: $size
storageClassName: $DEST_SC
EOF
echo "🔷 Migrating data back from $tmp_pvc -> $pvc..."
pv-migrate \
--source "$tmp_pvc" \
-n "$NS" \
--dest "$pvc" \
-N "$NS" \
--compress \
--ignore-mounted
echo "🔷 Deleting temporary PVC $tmp_pvc..."
kubectl delete pvc "$tmp_pvc" -n "$NS"
echo "🔷 Scaling up deployments/statefulsets using PVC $pvc..."
for deploy in $(kubectl get deploy -n "$NS" -o name | xargs -n1); do
if kubectl get "$deploy" -n "$NS" -o json | grep -q "$pvc"; then
replicas=$(kubectl get "$deploy" -n "$NS" -o jsonpath='{.spec.replicas}')
echo "Scaling $deploy to $replicas"
kubectl scale "$deploy" -n "$NS" --replicas="$replicas"
fi
done
for sts in $(kubectl get sts -n "$NS" -o name | xargs -n1); do
if kubectl get "$sts" -n "$NS" -o json | grep -q "$pvc"; then
replicas=$(kubectl get "$sts" -n "$NS" -o jsonpath='{.spec.replicas}')
echo "Scaling $sts to $replicas"
kubectl scale "$sts" -n "$NS" --replicas="$replicas"
fi
done
echo "✅ Migration of PVC $pvc complete."
done
create-test-pod-and-mount.yaml
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-pvc-pod
spec:
volumes:
- name: my-pvc-storage
persistentVolumeClaim:
claimName: test-pvc # Replace with the name of your existing PVC
containers:
- name: ubuntu-container
image: ubuntu:latest
command: ["sleep", "infinity"]
volumeMounts:
- name: my-pvc-storage
mountPath: /data # Replace with the desired mount path inside the container (e.g., /mnt/data)