# Get the storage class information supported by the current cluster, where xxx-StorageClass is the storage class code name, and xxx-Provider is the provider code name (the same below).$ 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: modify the value of the PVC storage class to the cloud platform of cluster A.storageClassName: xxx-StorageClassaccessModes:- ReadWriteOnceresources:requests:storage: 20Gi # Since the minimum storage of this cloud platform is 20 Gi, you need to modify the storage to 20 Gi in this sample....
$ kubectl apply -f with-pv.yamlnamespace/nginx-example createdpersistentvolumeclaim/nginx-logs createddeployment.apps/nginx-deployment createdservice/my-nginx created
/var/log/nginx
directory of the Nginx container as the log storage of service. The sample here will test and access the Nginx service in the browser to generate log data for the mounted PVC for data comparison after restoration, as shown below:$ 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/# View the first two logs of accss.log and 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:# The annotation of the backup hook strategy indicates that the nginx log directory is set to read-only mode before starting the backup, and is restored to read/write mode after the backup is completed.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# Must be the namespace installed by velero.namespace: velerospec:# The resources that only contains the nginx-example namespace.includedNamespaces:- nginx-example# The resources that do not distinguish namespace.includeClusterResources: true# Specify the storage location of the backup data.storageLocation: default# Specify the storage location of the volume snapshot.volumeSnapshotLocations:- default# Use restic to back up the volume.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:# Storage class name is mapped to Tencent cloud dynamic storage class cbs.xxx-StorageClass: cbs
$ kubectl apply -f cm-storage-class.yamlconfigmap/change-storage-class-config created
$ Downloads % mkdir migrate-backup# Decompress the backup file.$ Downloads % tar -zxvf migrate-backup.tar.gz -C migrate-backup# Edit the resources that need to be customized. In the sample below, "jokey-test" is added to the Deployment resource of Nginx: "jokey-test" annotation.$ migrate-backup % cat resources/deployments.apps/namespaces/nginx-example/nginx-deployment.json{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"jokey-test":"jokey-test",...# Repack the modified backup files.$ migrate-backup % tar -zcvf migrate-backup.tar.gz *
apiVersion: velero.io/v1kind: Restoremetadata:name: migrate-restorenamespace: velerospec:backupName: migrate-backupincludedNamespaces:- nginx-example# Fill in the resource type to be restored as needed. There is no resource to be excluded under the nginx-example namespace, so enter '*' here.includedResources:- '*'includeClusterResources: null# Resources not included in the restoration. Here storageClasses resource types are excluded.excludedResources:- storageclasses.storage.k8s.io# Use the labelSelector selector to select the resource with a specific label. Since there is no need to use the label selector to filter in this sample, please make an annotation here.# labelSelector:# matchLabels:# app: nginx# Set the relationship mapping strategy of the namespace.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>
# Since the "nginx-example" namespace is specified to map to the "default" namespace when restoration, the restored resource will run under the "default" namespace.$ 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
# You can find that the storage class of PVC/PV is already "cbs", indicating that the storage class mapping is successful.$ 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
If the storage class of PVC/PV is "cbs", the storage class mapping is successful. From the execution result of the above command, you can find that the storage class mapping is successful.
# Obtain the annotation "jokey-test" successfully, indicating that the custom modification of the resource is successful.$ kubectl get deployment.apps/nginx-deployment -o custom-columns=annotations:.metadata.annotations.jokey-testannotationsjokey-test
# Check the data size in the mounted PVC data directory. The data size is 88K, which is more than the size before the migration. The reason is that Tencent Cloud CLB actively initiated a health check and generated some logs.$ 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# Check the first two log information, which is the same as the log before the migration, indicating that the PVC data is not lost.$ 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"
Parameters | Description |
--include-resources | Specify a list of resource objects to include. |
--include-namespaces | Specify a list of namespaces to include. |
--include-cluster-resources | Specify whether to include resources of the cluster. |
--selector | Specify to include the resources that match the label selector. |
Parameters | Description |
--exclude-namespaces | Specify a list of namespaces to be excluded. |
--exclude-resources | Specify a list of resource objects to be excluded. |
velero.io/exclude-from-backup=true | This configuration item will configure this label attribute for the resource object, and the resource object with this label will be excluded. |
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
Apakah halaman ini membantu?