在Debian系統上備份和恢復Kubernetes(K8S)數據,可以使用Velero工具。Velero是一個開源的Kubernetes備份與恢復工具,支持對整個集群或選定資源進行備份、恢復,以及在不同Kubernetes集群之間遷移資源。以下是使用Velero進行備份和恢復的步驟:
kubectl create ns velero
apiVersion: v1
kind: PersistentVolume
metadata:
name: velero-pv
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.10
path: /velero-backups/k8s-dev
readOnly: false
persistentVolumeReclaimPolicy: Retain
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: velero-pvc
namespace: velero
spec:
storageClassName: ""
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Gi
volumeName: velero-pv
執行創建存儲卷的命令:
kubectl create -f storage.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: velero
name: minio
labels:
component: minio
spec:
strategy:
type: Recreates
selector:
matchLabels:
component: minio
template:
metadata:
labels:
component: minio
spec:
volumes:
- name: storage
persistentVolumeClaim:
claimName: velero-pvc
containers:
- name: minio
image: minio/minio:RELEASE.2023-03-20T20-16-18Z
imagePullPolicy: IfNotPresent
args:
- server
- /data
--config-dir=/config
--console-address=:9001
env:
- name: MINIO_ROOT_USER
value: "admin"
- name: MINIO_ROOT_PASSWORD
value: "minio123"
ports:
- containerPort: 9000
- containerPort: 9001
volumeMounts:
- name: storage
mountPath: /data
name: config
執行創建Minio部署的命令:
kubectl create -f minio.yaml
wget https://github.com/vmware-tanzu/velero/releases/download/v1.14.1/velero-v1.14.1-linux-amd64.tar.gz
tar xvf velero-v1.14.1-linux-amd64.tar.gz
cd velero-v1.14.1-linux-amd64
./install.sh --bucket velero --namespace velero --config ./credentials-velero --use-restic --use-node-agent --node-agent-pod-cpu-limit 2 --node-agent-pod-mem-limit 2048Mi --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.1.10:9000
velero backup create my-backup --include-namespaces my-namespace --include-resources deployment,service,configmap,persistentvolumeclaim
kubectl delete pod pod1
velero restore create my-restore --backup-name my-backup --namespace my-namespace
恢復完成后,可以通過kubectl get pods
等命令驗證資源是否恢復成功。
以上步驟提供了在Debian系統上使用Velero備份和恢復K8S數據的詳細指南。請注意,備份和恢復操作可能會受到集群狀態、網絡配置等多種因素的影響,建議在操作前詳細閱讀Velero的官方文檔,并在測試環境中先行驗證。