使用velero工具对k8s进行备份和恢复

配置备份工具

参考网站:https://www.cnblogs.com/punchlinux/p/16543420.html

下载velero客户端 https://github.com/vmware-tanzu/velero/releases

1、部署备份工具

在dev01,devops01上操作

1、下载velero

./Seafile/服务和工具安装包/velero/velero-v1.9.1-linux-amd64.tar.gz

2、安装velero客户端

tar xf velero-v1.9.1-linux-amd64.tar.gz
cd velero-v1.9.1-linux-amd64
cp velero /bin/

3、安装minio

mkdir -p /service/software/minio/data
docker pull minio/minio:RELEASE.2024-03-03T17-50-39Z
 docker run \
  -itd \
  -p 9000:9000 \
  -p 9001:9001 \
  --name minio \
  -v /service/software/minio/data:/data \
  -e "MINIO_ROOT_USER=admin" \
  -e "MINIO_ROOT_PASSWORD=minioadmin" \
  minio/minio:RELEASE.2024-09-22T00-33-43Z      server /data --console-address ":9001"

4、安装服务端

sbx环境 dev01上操作

1、创建bucket

访问:http://10.250.22.11:9000

输入账号密码

点击Create Bucket

设置Bucket Name为 sbx

点击Create Bucket

2、开始安装velero服务端
 cd /tmp/

echo "
[default]
aws_access_key_id=admin
aws_secret_access_key=minioadmin
" > key.txt

bucket=uat
s3url=http://10.250.22.11:9000
velero install    \
     --provider aws   \
     --bucket ${bucket}   \
     --image velero/velero:v1.9.1  \
  --plugins velero/velero-plugin-for-aws:v1.2.1  \
  --namespace velero  \
  --secret-file key.txt  \
  --use-volume-snapshots=false \
    --use-restic \
  --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=${s3url}

2、设置备份策略

sbx 环境

env=uat
namespaces="uat pre mq uat-ingress-nginx monitor"

for i in $namespaces ; do
	  velero schedule create ${env}-${i} --schedule '30 */1 * * *' --include-cluster-resources=true --ttl 168h --include-namespaces ${i} 
done

# 查看备份策略
velero  schedule get

[root@dev01 ~]# velero  schedule get
NAME                STATUS    CREATED                         SCHEDULE       BACKUP TTL   LAST BACKUP   SELECTOR
sbx-monitor         Enabled   2022-08-22 14:12:40 +0800 CST   30 */1 * * *   0s           n/a           <none>
sbx-mq              Enabled   2022-08-22 14:12:39 +0800 CST   30 */1 * * *   0s           n/a           <none>
sbx-ingress-nginx   Enabled   2022-08-22 14:12:40 +0800 CST   30 */1 * * *   0s           n/a           <none>
sbx-sbx             Enabled   2022-08-22 14:12:39 +0800 CST   30 */1 * * *   0s           n/a           <none>

# 查看备份集
velero backup get
[root@dev01 ~]# velero backup get
NAME                               STATUS      ERRORS   WARNINGS   CREATED                         EXPIRES   STORAGE LOCATION   SELECTOR
sbx-ingress-nginx-20220822063047   Completed   0        0          2022-08-22 14:30:50 +0800 CST   29d       default            <none>
sbx-monitor                        Completed   0        0          2022-08-22 14:15:25 +0800 CST   29d       default            <none>
sbx-monitor-20220822063047         Completed   0        0          2022-08-22 14:30:47 +0800 CST   29d       default            <none>
sbx-mq                             Completed   0        0          2022-08-22 14:15:19 +0800 CST   29d       default            <none>
sbx-mq-20220822063047              Completed   0        0          2022-08-22 14:31:09 +0800 CST   29d       default            <none>
sbx-sbx                            Completed   0        0          2022-08-22 14:15:13 +0800 CST   29d       default            <none>
sbx-sbx-20220822063047             Completed   0        0          2022-08-22 14:30:54 +0800 CST   29d       default            <none>

# 手动创建备份
velero backup create uat-monitoring-2024010701 --include-namespaces monitoring



# 批量删除失败的备份
backups=$(velero backup get  | grep -v Completed | awk '{print $1')
for i in $backups;do velero backup delete $i --confirm;done

3、恢复数据

# 查看备份策略
velero  schedule get

NAME                               STATUS      ERRORS   WARNINGS   CREATED                         EXPIRES   STORAGE LOCATION   SELECTOR
sbx-ingress-nginx-20220822063047   Completed   0        0          2022-08-22 14:30:50 +0800 CST   29d       default            <none>
# 查看备份集
​```shell
[root@dev01 ~]# velero backup get
NAME                               STATUS      ERRORS   WARNINGS   CREATED                         EXPIRES   STORAGE LOCATION   SELECTOR
sbx-ingress-nginx-20220822063047   Completed   0        0          2022-08-22 14:30:50 +0800 CST   29d       default            <none>
sbx-monitor                        Completed   0        0          2022-08-22 14:15:25 +0800 CST   29d       default            <none>
sbx-monitor-20220822063047         Completed   0        0          2022-08-22 14:30:47 +0800 CST   29d       default            <none>
sbx-mq                             Completed   0        0          2022-08-22 14:15:19 +0800 CST   29d       default            <none>
sbx-mq-20220822063047              Completed   0        0          2022-08-22 14:31:09 +0800 CST   29d       default            <none>
sbx-sbx                            Completed   0        0          2022-08-22 14:15:13 +0800 CST   29d       default            <none>
sbx-sbx-20220822063047             Completed   0        0          2022-08-22 14:30:54 +0800 CST   29d       default            <none>

恢复

 velero restore create --from-backup  sbx-mq-20220822063047  
 # 手动恢复恢复到指定命名空间下
#示例
velero restore create --from-backup chouyin-211-2025-7-30 --namespace-mappings default:chouyin
 
 # 恢复sbx命名空间下app=abc 的deployment
 velero restore create --from-backup sbx-mq-20220822063047   --include-namespaces sbx --include-resources deployments.apps --selector app=abc

查看恢复进度

velero restore get

注意事项
1.sa恢复后token会发生改变,外部服务调用的token需要更新
2.恢复集群的时候如有ingress,必须优先恢复ingress-nginx控制器

posted @ 2025-07-30 15:55  Tenderness、  阅读(42)  评论(0)    收藏  举报