Skip to content

Instantly share code, notes, and snippets.

@pascaliske
Last active June 29, 2022 14:08
Show Gist options
  • Save pascaliske/52c722d321c16a655f219df7c3b6a65d to your computer and use it in GitHub Desktop.
Save pascaliske/52c722d321c16a655f219df7c3b6a65d to your computer and use it in GitHub Desktop.
Kubernetes PVC migration
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# usage:
# ./pvc-migrate <forward|backward> <namespace> [<source> [<dest>]]
DIRECTION="${1}"
NAMESPACE="${2}"
# provide defaults for source and dest if not given
if [ "$DIRECTION" = "forward" ]; then
SOURCE="${3:-$NAMESPACE}"
DEST="${4:-pvc-temp}"
elif [ "$DIRECTION" = "backward" ]; then
SOURCE="${4:-pvc-temp}"
DEST="${3:-$NAMESPACE}"
fi
# do migration
echo "Migrating data in namespace ${NAMESPACE} from ${SOURCE} to ${DEST}"
pv-migrate migrate "${SOURCE}" "${DEST}" \
--source-kubeconfig ~/.kube/infrastructure/config \
--source-context default \
--source-namespace "${NAMESPACE}" \
--dest-kubeconfig ~/.kube/infrastructure/config \
--dest-context default \
--dest-namespace "${NAMESPACE}" \
--ignore-mounted
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-temp
namespace: <namespace> # set to namespace of target pvc
spec:
resources:
requests:
storage: 10Gi # ensure same size as source pvc
volumeMode: Filesystem
storageClassName: local-path
accessModes:
- ReadWriteOnce
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pvc-temp
namespace: <namespace> # set to namespace of target pvc
spec:
selector:
matchLabels:
app: pvc-temp
template:
metadata:
labels:
app: pvc-temp
spec:
containers:
- name: pvc-temp
image: alpine:latest
command: ['tail', '-f', '/dev/null']
resources:
limits:
memory: 128Mi
cpu: 500m
volumeMounts:
- name: pvc-temp
mountPath: /data
readOnly: true
volumes:
- name: pvc-temp
persistentVolumeClaim:
claimName: pvc-temp
tolerations: [] # optional set tolerations if needed
nodeSelector:
kubernetes.io/hostname: '' # optional set target node if needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment