如何安全的备份Kubernetes
969 字
5 分钟
如何安全的备份Kubernetes
一, 备份ETCD
1 备份脚本
cat /data/etcd_backup_dir/etcd_backup.sh#!/bin/bashdate;CACERT="/etc/ssl/etcd/ssl/ca.pem"CERT="/etc/ssl/etcd/ssl/admin-${hostname}.pem" # 证书路径,因为我的文件名里有主机名这里些好吃呢个变量EKY="/etc/ssl/etcd/ssl/admin-${hostname}-key.pem"IP=$(hostname -I | awk '{print $1}')ENDPOINTS="${IP}:2379"ETCDCTL_API=3 etcdctl \--cacert="${CACERT}" --cert="${CERT}" --key="${EKY}" \--endpoints=${ENDPOINTS} \snapshot save /data/etcd_backup_dir/etcd-snapshot-`date +%Y%m%d`.db# 备份到/data/etcd_backup_dir/下# 备份保留30天find /data/etcd_backup_dir/ -name *.db -mtime +30 -exec rm -f {} \;2 定时任务
crontab -e0 23 * * 6 sh /data/etcd_backup_dir/etcd_backup.sh二, 使用Velero备份
[!hint] 主要优势
- 部分恢复而非全集群恢复
- 跨集群迁移资源
- 更灵活的备份策略管理
- 选择性备份特定资源
Velero包含:在集群上运行的服务器与本地运行的命令行客户端这两部分
1 客户端安装
tar -zxvf velero-v1.16.0-linux-amd64.tar.gzmv velero-v1.16.0-linux-amd64/velero /usr/bin/velero versionClient: Version: v1.16.0 Git commit: 8f31599fe4af5453dee032beaf8a16bd75de91a5<error getting server version: no matches for kind "ServerStatusRequest" in version "velero.io/v1"># 命令补全velero completion bash >/etc/profile.d/velero.sh2 服务端安装
2.1 镜像准备
docker pull ccr.ccs.tencentyun.com/ccops/all:velero-v1.16.0docker pull ccr.ccs.tencentyun.com/ccops/all:velero-plugin-for-aws-v1.12.02.2 存储准备
[!attention] 只要是符合s3协议即可,官方带了个minio部署到k8s的yaml 带的这个没做数据持久化,并且还是弱密码,修改下
cat velero-v1.16.0-linux-amd64/examples/minio/00-minio-deployment.yaml---apiVersion: v1kind: Namespacemetadata: name: velero
---apiVersion: apps/v1kind: Deploymentmetadata: namespace: velero name: minio labels: component: miniospec: strategy: type: Recreate selector: matchLabels: component: minio template: metadata: labels: component: minio spec: nodeSelector: minio.storage.sys: "yes" volumes: - name: storage hostPath: path: /data/server/velero-minio/data type: DirectoryOrCreate - name: config hostPath: path: /data/server/velero-minio/config type: DirectoryOrCreate containers: - name: minio image: quay.io/minio/minio:latest imagePullPolicy: IfNotPresent args: - server - /storage - --config-dir=/config env: - name: MINIO_ACCESS_KEY value: "velero-minio" - name: MINIO_SECRET_KEY value: "1qaz@WSX" ports: - containerPort: 9000 volumeMounts: - name: storage mountPath: "/storage" - name: config mountPath: "/config"
---apiVersion: v1kind: Servicemetadata: namespace: velero name: minio labels: component: miniospec: type: ClusterIP ports: - port: 9000 targetPort: 9000 protocol: TCP selector: component: minio
---apiVersion: batch/v1kind: Jobmetadata: namespace: velero name: minio-setup labels: component: miniospec: template: metadata: name: minio-setup spec: restartPolicy: OnFailure nodeSelector: minio.storage.sys: "yes" volumes: - name: config hostPath: path: /data/server/velero-minio/config type: DirectoryOrCreate containers: - name: mc image: quay.io/minio/mc:latest imagePullPolicy: IfNotPresent command: - /bin/sh - -c - "mc --config-dir=/config config host add velero http://minio:9000 velero-minio 1qaz@WSX && mc --config-dir=/config mb -p velero/velero" volumeMounts: - name: config mountPath: "/config"# 给节点打标签kubectl label nodes <node-name> minio.storage.sys=yes
kubectl apply -f velero-v1.16.0-linux-amd64/examples/minio/00-minio-deployment.yamlnamespace/velero createddeployment.apps/minio createdservice/minio createdjob.batch/minio-setup created
kubectl get pod -n veleroNAME READY STATUS RESTARTS AGEminio-cd47fcf59-sm2gn 1/1 Running 0 103sminio-setup-smb9p 0/1 Completed 0 103s2.3 安装服务端
[!attention] 建议存到文件里 网上都是直接命令行,如果以后升级不好维护,建议存储到文件里
cat velero-install.sh
#!/bin/bashcat > credentials-velero << EOF[default]aws_access_key_id = velero-minioaws_secret_access_key = 1qaz@WSXEOF
velero install \ --image ccr.ccs.tencentyun.com/ccops/all:velero-v1.16.0 \ --plugins ccr.ccs.tencentyun.com/ccops/all:velero-plugin-for-aws-v1.12.0 \ --provider aws \ --bucket velero \ --namespace velero \ --secret-file ./credentials-velero \ --use-volume-snapshots=false \ --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://minio.velero:9000
sh velero-install.shCustomResourceDefinition/backuprepositories.velero.io: attempting to create resourceCustomResourceDefinition/backuprepositories.velero.io: attempting to create resource clientCustomResourceDefinition/backuprepositories.velero.io: created......Deployment/velero: attempting to create resource clientDeployment/velero: createdVelero is installed! ⛵ Use 'kubectl logs deployment/velero -n velero' to view the status.# 看到这行说明安装没问题kubectl get pod -n veleroNAME READY STATUS RESTARTS AGEminio-cd47fcf59-sm2gn 1/1 Running 0 36mminio-setup-smb9p 0/1 Completed 0 36mvelero-76454754b9-fqqnw 1/1 Running 0 86s3 测试
3.1 安装测试nginx
[!attention] 建议存到文件里 官网测试的nginx例子pull不下来镜像,自己需要手动改下
kubectl apply -f velero-v1.16.0-linux-amd64/examples/nginx-app/base.yamlkubectl get pod -n nginx-exampleNAME READY STATUS RESTARTS AGEnginx-deployment-7f796bc6dc-hxqqd 1/1 Running 0 37snginx-deployment-7f796bc6dc-pjzxp 1/1 Running 0 21s3.2 备份
velero backup create nginx-backup --selector app=nginxBackup request "nginx-backup" submitted successfully.Run `velero backup describe nginx-backup` or `velero backup logs nginx-backup` for more details.3.3 还原
3.3.1 模拟丢失配置
kubectl delete namespace nginx-examplekubectl get ns nginx-exampleError from server (NotFound): namespaces "nginx-example" not found3.3.2 还原
velero restore create --from-backup nginx-backupRestore request "nginx-backup-20250423165832" submitted successfully.Run `velero restore describe nginx-backup-20250423165832` or `velero restore logs nginx-backup-20250423165832` for more details.# 查看还原velero restore getNAME BACKUP STATUS STARTED COMPLETED ERRORS WARNINGS CREATED SELECTORnginx-backup-20250423165832 nginx-backup Completed 2025-04-23 16:58:32 +0800 CST 2025-04-23 16:58:33 +0800 CST 0 1 2025-04-23 16:58:32 +0800 CST <none># 查看恢复的podkubectl get pod -n nginx-exampleNAME READY STATUS RESTARTS AGEnginx-deployment-7f796bc6dc-hxqqd 1/1 Running 0 23snginx-deployment-7f796bc6dc-pjzxp 1/1 Running 0 23s4 高级备份功能
# 每天1点备份一次nginx-example空间资源velero schedule create nginx-daily --schedule="0 1 * * *" --include-namespaces nginx-example# 备份除"backup=ignore"这个标签的所有资源velero backup create nginx-backup --selector 'backup notin (ignore)'# 每周日0点备份一次所有集群资源,并且设置保留时间2160小时velero create schedule cluster-all --schedule="0 0 * * 0" --ttl 2160h文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
相关文章 智能推荐
1
Kubernetes命令总结
🐳云原生 在使用Kubernetes中,命令使用到的总结
2
Kubernetes自定义权限
🐳云原生 Kubernetes权限管理,实现访问控制
3
Kubespray 国内安装k8s集群
🐳云原生 最可靠的k8s安装工具
4
k8s收集审计日志
🐳云原生 k8s收集审计日志
5
记录kubelet问题
🎡问题记录 记录kubelet问题,遇到新问题会记录到这里
随机文章 随机推荐