Debian如何备份和恢复K8S数据

557
2025/3/23 6:32:14
栏目: 智能运维
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Debian系统上备份和恢复Kubernetes(K8S)数据,可以使用Velero工具。Velero是一个开源的Kubernetes备份与恢复工具,支持对整个集群或选定资源进行备份、恢复,以及在不同Kubernetes集群之间迁移资源。以下是使用Velero进行备份和恢复的步骤:

安装Velero和Minio

  1. 创建Velero的命名空间
kubectl create ns velero
  1. 创建存储卷(这里使用NFS格式的存储卷,读者可根据实际环境自行修改PersistentVolume配置):
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
  1. 安装Minio
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

备份K8S集群数据

  1. 安装Velero客户端
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
  1. 执行备份命令
velero backup create my-backup --include-namespaces my-namespace --include-resources deployment,service,configmap,persistentvolumeclaim

恢复K8S集群数据

  1. 删除需要恢复的Pod或资源
kubectl delete pod pod1
  1. 执行恢复命令
velero restore create my-restore --backup-name my-backup --namespace my-namespace

恢复完成后,可以通过kubectl get pods等命令验证资源是否恢复成功。

以上步骤提供了在Debian系统上使用Velero备份和恢复K8S数据的详细指南。请注意,备份和恢复操作可能会受到集群状态、网络配置等多种因素的影响,建议在操作前详细阅读Velero的官方文档,并在测试环境中先行验证。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Debian域名更新频率