# 获取当前集群支持的存储类信息,其中 xxx-StorageClass 为存储类代名,xxx-Provider 为提供商代名,下同。$ kubectl get scNAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGExxx-StorageClass xxx-Provider Delete Immediate true 3d3h...
...---kind: PersistentVolumeClaimapiVersion: v1metadata:name: nginx-logsnamespace: nginx-examplelabels:app: nginxspec:# Optional: 修改 PVC 的存储类的值为某云平台storageClassName: xxx-StorageClassaccessModes:- ReadWriteOnceresources:requests:storage: 20Gi # 由于该云平台限制存储最小为20Gi,本示例需要同步修改此值为20Gi...
$ kubectl apply -f with-pv.yamlnamespace/nginx-example createdpersistentvolumeclaim/nginx-logs createddeployment.apps/nginx-deployment createdservice/my-nginx created
/var/log/nginx
目录,作为服务的日志存储。本文示例通过在浏览器测试访问 Nginx 服务,为挂载的 PVC 生产日志数据,以便后续还原后进行数据比对。示例如下:$ kubectl exec -it nginx-deployment-5ccc99bffb-6nm5w bash -n nginx-examplekubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND]Defaulting container name to nginx.Use 'kubectl describe pod/nginx-deployment-5ccc99bffb-6nm5w -n nginx-example' to see all of the containers in this pod$ du -sh /var/log/nginx/84K /var/log/nginx/# 查看 accss.log 和 error.log 前两条日志$ head -n 2 /var/log/nginx/access.log192.168.0.73 - - [29/Dec/2020:03:02:31 +0000] "GET /?spm=5176.2020520152.0.0.22d016ddHXZumX HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"192.168.0.73 - - [29/Dec/2020:03:02:32 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://47.242.233.22/?spm=5176.2020520152.0.0.22d016ddHXZumX" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"$ head -n 2 /var/log/nginx/error.log2020/12/29 03:02:32 [error] 6#6: *597 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.73, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "47.242.233.22", referrer: "http://47.242.233.22/?spm=5176.2020520152.0.0.22d016ddHXZumX"2020/12/29 03:07:21 [error] 6#6: *1172 open() "/usr/share/nginx/html/0bef" failed (2: No such file or directory), client: 192.168.0.73, server: localhost, request: "GET /0bef HTTP/1.0"
kubectl api-resources --verbs=list -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found --all-namespaces
kubectl api-resources --namespaced=false --verbs=list -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found
kubectl api-resources --namespaced=true --verbs=list -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found --all-namespaces
$ kubectl get all -n nginx-exampleNAME READY STATUS RESTARTS AGEpod/nginx-deployment-5ccc99bffb-tn2sh 2/2 Running 0 2d19hNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/my-nginx LoadBalancer 172.21.1.185 x.x.x.x 80:31455/TCP 2d19hNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-deployment 1/1 1 1 2d19hNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-deployment-5ccc99bffb 1 1 1 2d19h$ kubectl get pvc -n nginx-exampleNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEnginx-logs Bound d-j6ccrq4k1moziu1l6l5r 20Gi RWO xxx-StorageClass 2d19h$ kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEd-j6ccrq4k1moziu1l6l5r 20Gi RWO Delete Bound nginx-example/nginx-logs xxx-StorageClass 2d19h
...annotations:# 备份 Hook 策略的注解表示:在开始备份之前将 nginx 日志目录设置为只读模式,备份完成后恢复读写模式pre.hook.backup.velero.io/container: fsfreezepre.hook.backup.velero.io/command: '["/sbin/fsfreeze", "--freeze", "/var/log/nginx"]'post.hook.backup.velero.io/container: fsfreezepost.hook.backup.velero.io/command: '["/sbin/fsfreeze", "--unfreeze", "/var/log/nginx"]'spec:volumes:- name: nginx-logspersistentVolumeClaim:claimName: nginx-logscontainers:- image: nginx:1.17.6name: nginxports:- containerPort: 80volumeMounts:- mountPath: "/var/log/nginx"name: nginx-logsreadOnly: false- image: ubuntu:bionicname: fsfreezesecurityContext:privileged: truevolumeMounts:- mountPath: "/var/log/nginx"name: nginx-logs...
apiVersion: velero.io/v1kind: Backupmetadata:name: migrate-backup# 必须得是 velero 安装的命名空间namespace: velerospec:# 仅包含 nginx-example 命名空间的资源includedNamespaces:- nginx-example# 包含不区分命名空间的资源includeClusterResources: true# 备份数据存储位置指定storageLocation: default# 卷快照存储位置指定volumeSnapshotLocations:- default# 使用 restic 备份卷defaultVolumesToRestic: true
$ kubectl apply -f backup.yamlbackup.velero.io/migrate-backup created$ velero backup getNAME STATUS ERRORS WARNINGS CREATED EXPIRES STORAGE LOCATION SELECTORmigrate-backup InProgress 0 0 2020-12-29 19:24:12 +0800 CST 29d default <none>$ velero backup getNAME STATUS ERRORS WARNINGS CREATED EXPIRES STORAGE LOCATION SELECTORmigrate-backup Completed 0 0 2020-12-29 19:24:28 +0800 CST 29d default <none>
kubectl patch backupstoragelocation default --namespace velero \\--type merge \\--patch '{"spec":{"accessMode":"ReadOnly"}}'
apiVersion: v1kind: ConfigMapmetadata:name: change-storage-class-confignamespace: velerolabels:velero.io/plugin-config: ""velero.io/change-storage-class: RestoreItemActiondata:# 存储类名映射到腾讯云动态存储类 cbsxxx-StorageClass: cbs
$ kubectl apply -f cm-storage-class.yamlconfigmap/change-storage-class-config created
$ Downloads % mkdir migrate-backup# 解压备份文件$ Downloads % tar -zxvf migrate-backup.tar.gz -C migrate-backup# 编辑修改需要自定义的资源,本示例为 Nginx 的 Deployment 资源添加 "jokey-test":"jokey-test" 的注解项$ migrate-backup % cat resources/deployments.apps/namespaces/nginx-example/nginx-deployment.json{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"jokey-test":"jokey-test",...# 重新打包修改后的备份文件$ migrate-backup % tar -zcvf migrate-backup.tar.gz *
apiVersion: velero.io/v1kind: Restoremetadata:name: migrate-restorenamespace: velerospec:backupName: migrate-backupincludedNamespaces:- nginx-example# 按需填写需要恢复的资源类型,nginx-example 命名空间下没有想要排除的资源,所以这里直接写 '*'includedResources:- '*'includeClusterResources: null# 还原时不包含的资源,这里额外排除 StorageClasses 资源类型。excludedResources:- storageclasses.storage.k8s.io# 使用 labelSelector 选择器选择具有特定 label 的资源,由于此示例中无须再使用 label 选择器筛选,这里先注释。# labelSelector:# matchLabels:# app: nginx# 设置命名空间关系映射策略namespaceMapping:nginx-example: defaultrestorePVs: true
$ kubectl apply -f restore.yamlrestore.velero.io/migrate-restore created$ velero restore getNAME BACKUP STATUS STARTED COMPLETED ERRORS WARNINGS CREATED SELECTORmigrate-restore migrate-backup Completed 2021-01-12 20:39:14 +0800 CST 2021-01-12 20:39:17 +0800 CST 0 0 2021-01-12 20:39:14 +0800 CST <none>
# 由于在还原时指定了 "nginx-example" 命名空间映射到 "default" 命名空间,所以还原的资源将运行在 "default" 命名空间下$ kubectl get all -n defaultNAME READY STATUS RESTARTS AGEpod/nginx-deployment-5ccc99bffb-6nm5w 2/2 Running 0 49sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kube-user LoadBalancer 172.16.253.216 10.0.0.28 443:30060/TCP 8dservice/kubernetes ClusterIP 172.16.252.1 <none> 443/TCP 8dservice/my-nginx LoadBalancer 172.16.254.16 x.x.x.x 80:30840/TCP 49sNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-deployment 1/1 1 1 49sNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-deployment-5ccc99bffb 1 1 1 49s
# 可以看到 PVC/PV 的存储类已经是 "cbs",说明存储类映射成功$ kubectl get pvc -n defaultNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEnginx-logs Bound pvc-bcc17ccd-ec3e-4d27-bec6-b0c8f1c2fa9c 20Gi RWO cbs 55s$ kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEpvc-bcc17ccd-ec3e-4d27-bec6-b0c8f1c2fa9c 20Gi RWO Delete Bound default/nginx-logs cbs 57s
# 获取注解 “jokey-test” 成功,说明自定义修改资源成功。$ kubectl get deployment.apps/nginx-deployment -o custom-columns=annotations:.metadata.annotations.jokey-testannotationsjokey-test
# 查看挂载的 PVC 数据目录中的数据大小,显示为88K,比迁移前多,原因是腾讯云 CLB 主动发起健康检查产生了一些日志$ kubectl exec -it nginx-deployment-5ccc99bffb-6nm5w -n default -- bashDefaulting container name to nginx.Use 'kubectl describe pod/nginx-deployment-5ccc99bffb-6nm5w -n default' to see all of the containers in this pod.$ du -sh /var/log/nginx88K /var/log/nginx# 查看前两条日志信息,和迁移前一致,大致说明 PVC 数据未丢失$ head -n 2 /var/log/nginx/access.log192.168.0.73 - - [29/Dec/2020:03:02:31 +0000] "GET /?spm=5176.2020520152.0.0.22d016ddHXZumX HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"192.168.0.73 - - [29/Dec/2020:03:02:32 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://47.242.233.22/?spm=5176.2020520152.0.0.22d016ddHXZumX" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "-"$ head -n 2 /var/log/nginx/error.log2020/12/29 03:02:32 [error] 6#6: *597 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.73, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "47.242.233.22", referrer: "http://47.242.233.22/?spm=5176.2020520152.0.0.22d016ddHXZumX"2020/12/29 03:07:21 [error] 6#6: *1172 open() "/usr/share/nginx/html/0bef" failed (2: No such file or directory), client: 192.168.0.73, server: localhost, request: "GET /0bef HTTP/1.0"
参数 | 参数含义 |
--include-resources | 指定需要包含的资源对象列表。 |
--include-namespaces | 指定需要包含的命名空间列表。 |
--include-cluster-resources | 指定是否要包含集群的资源。 |
--selector | 指定包含与标签选择器匹配的资源。 |
参数 | 参数含义 |
--exclude-namespaces | 指定需要排除的命名空间列表。 |
--exclude-resources | 指定需要排除的资源对象列表。 |
velero.io/exclude-from-backup=true | 此配置项为资源对象配置 label 属性,添加了此 label 配置项的资源对象将会排除在外。 |
kubectl -n <YOUR_POD_NAMESPACE> annotate <pod/YOUR_POD_NAME> backup.velero.io/backup-volumes=<YOUR_VOLUME_NAME_1,YOUR_VOLUME_NAME_2,...>
kubectl -n <YOUR_POD_NAMESPACE> annotate <pod/YOUR_POD_NAME> backup.velero.io/backup-volumes-excludes=<YOUR_VOLUME_NAME_1,YOUR_VOLUME_NAME_2,...>
kubectl -n velero get podvolumebackups -l velero.io/backup-name=<YOUR_BACKUP_NAME> -o yaml
kubectl -n velero get podvolumerestores -l velero.io/restore-name=<YOUR_RESTORE_NAME> -o yaml
本页内容是否解决了您的问题?