K8S的资源备份Velero+Minio
一、安装minio
mkdir -p /minio/data
cd /opt/minio && vim docker-compose.yaml
version: '3' services: minio-01: container_name: minio restart: always image: quay.io/minio/minio deploy: resources: limits: cpus: '2' memory: 4000M reservations: cpus: '0.5' memory: 1000M ports: - 9000:9000 - 9001:9001 environment: - MINIO_JAVA_OPTS="-Xms3g -Xmx3g" - MINIO_PROMETHEUS_AUTH_TYPE=public - MINIO_ROOT_USER=myminioadmin # minio登录的用户名 - MINIO_ROOT_PASSWORD=myminioadmin # minio登录密码 entrypoint: - /bin/sh - -c - | /usr/bin/docker-entrypoint.sh server --address ":9000" --console-address ":9001" \ /data volumes: - /minio/data:/data
docker compose -f docker-compose.yaml up -d
访问:http://ip:9001 用户名/密码:myminioadmin/myminioadmin
二、在Minio上面创建存储桶Buckets

三、在k8s集群上新建minio的认证信息
mkdir -p /opt/velero && vim /opt/velero/credentials-velero
[default] aws_access_key_id=myminioadmin aws_secret_access_key=myminioadmin
四、安装velero
velero install \ --kubeconfig /root/.kube/config \ --provider aws \ --plugins velero/velero-plugin-for-aws:v1.9.0,velero/velero-plugin-for-csi:v0.7.0 \ --use-volume-snapshots=true \ --uploader-type=restic \ --use-node-agent \ --bucket k8s-backup-70.110 \ --secret-file=./credentials-velero \ --snapshot-location-config region=minio \ --features=EnableCSI \ --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.70.120:9000
参数解释: velero install: 这是命令的基础,用于安装 Velero。 --kubeconfig /root/.kube/config: 指定 Kubernetes 的配置文件路径,这个文件通常包含了集群的访问信息。 --provider aws: 指定使用的云提供商,这里是 Amazon Web Services (AWS)。 --plugins velero/velero-plugin-for-aws:v1.9.0,velero/velero-plugin-for-csi:v0.7.0: 指定要使用的 Velero 插件。velero-plugin-for-aws 用于 AWS 环境的备份和恢复,velero-plugin-for-csi 用于支持容器存储接口(CSI)。 --use-volume-snapshots=true: 表示启用卷快照功能,这样可以在备份过程中创建持久卷的快照。 --uploader-type=restic: 指定使用 Restic 作为上传工具,Restic 是一个高效的备份程序。 --use-node-agent: 允许使用节点代理,这样在节点上可以运行备份和恢复操作。 --bucket k8s-backup-70.110: 指定所使用的 S3 存储桶的名称,用于保存备份数据。 --secret-file=./credentials-velero: 指定包含访问 S3 存储桶所需凭证的文件路径。 --snapshot-location-config region=minio: 指定快照位置的配置,这里设置区域为 minio,表示使用 MinIO 服务。 --features=EnableCSI: 启用 CSI (Container Storage Interface) 功能,允许从支持 CSI 的存储中备份和恢复。 --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.70.120:9000: 配置备份位置,包括区域设置、强制路径样式,以及 MinIO 的 URL。 region=minio: 指定区域为 MinIO。 s3ForcePathStyle="true": 强制使用路径样式的 S3 请求,适用于 MinIO。 s3Url=http://192.168.70.120:9000: 指定 MinIO 服务的访问 URL。
kubectl get ns kubectl get po -n velero kubectl api-resources --api-group=velero.io
五、资源备份与灾难恢复
(1)创建测试文件
vim demo.yaml
apiVersion: v1 kind: Namespace metadata: name: nginx-example --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: nginx-example spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.27 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: my-nginx namespace: nginx-example spec: type: LoadBalancer selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80
kubectl apply -f demo.yaml
kubectl get all -n nginx-example
(2)创建全局资源备份
velero backup create 2024-07-23-backup --include-namespaces "*" --include-cluster-resources
velero backup get

(3)模拟灾难
kubectl delete namespace nginx-example kubectl get all -n nginx-example

(4)资源恢复
资源多的话,时间比较久,需要耐心等待
velero backup get velero restore create --from-backup 2024-07-23-backup

(5)资源检查
velero restore get kubectl get all -n nginx-example

(6)定时备份与定时清理
在 Kubernetes 集群中创建一个每天凌晨 1 点备份所有命名空间的作业,并且保留备份 7 天。
velero schedule create daily-backup --schedule="0 1 * * *" --include-namespaces '*' --ttl=168h
velero schedule get

注:可以指定备份资源,备份命名空间为nginx-example的资源
velero backup create 2024-7-23-backup --include-namespaces nginx-example

浙公网安备 33010602011771号