feedcode
1 天前
删掉 PV 后重建,记得 spec.claimRef 保持和之前的一样,PVC 会重新 Bound
while read share;do
echo "check $share"
kubectl get pv $share -o yaml > "$share-origin.yaml"
ns=$(yq '.spec.claimRef.namespace' "$share-origin.yaml")
pvc=$(yq '.spec.claimRef.name' "$share-origin.yaml")
echo "$ns $pvc"
if [ ! -f "all-pods-$ns.yaml" ] ;then
kubectl get pods -n $ns -o yaml > "all-pods-$ns.yaml"
fi
podname=$(yq ".items[]|select(.spec.volumes[].persistentVolumeClaim.claimName==\"$pvc\")|.metadata.name" "all-pods-$ns.yaml")
echo "pods $podname will be deleted"
# patch pv
kubectl patch pv $share -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}, "metadata":{"finalizers":null }}' --type=merge
kubectl delete pv $share --wait=false
sleep 5
kubectl delete pod -n $ns $podname
sleep 10
kubectl get pv $share || echo deleted
# recreate PV here
sed -e "REPLACE-HERE" "$share-origin.yaml" >"$share-new.yaml"
kubectl create -f "$share-new.yaml"
done<pv-shares.txt