Last active
June 29, 2022 14:08
-
-
Save pascaliske/52c722d321c16a655f219df7c3b6a65d to your computer and use it in GitHub Desktop.
Kubernetes PVC migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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