自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何優(yōu)雅的使用 Velero 快速備份和還原 Kubernetes 集群

系統(tǒng) Linux
每個 Velero 的操作(比如按需備份、計劃備份、還原)都是 CRD 自定義資源,Velero 可以備份或還原集群中的所有對象,也可以按類型、namespace 或標(biāo)簽過濾對象。

 [[438414]]

 

Velero(https://velero.io)(可以提供備份和還原 Kubernetes 集群資源和持久卷的能力,你可以在公有云或本地搭建的私有云環(huán)境安裝 Velero,可以為你提供以下能力:

  •  備份集群數(shù)據(jù),并在集群故障的情況下進(jìn)行還原
  •  將集群資源遷移到其他集群
  •  將生產(chǎn)集群復(fù)制到開發(fā)和測試集群

Velero 包含一個在集群上運(yùn)行的服務(wù)器端和在本地運(yùn)行的命令行客戶端。

原理

每個 Velero 的操作(比如按需備份、計劃備份、還原)都是 CRD 自定義資源,Velero 可以備份或還原集群中的所有對象,也可以按類型、namespace 或標(biāo)簽過濾對象。Velero 是 Kubernetes 用來災(zāi)難恢復(fù)的理想選擇,也可以在集群上執(zhí)行系統(tǒng)操作(比如升級)之前對應(yīng)用程序狀態(tài)進(jìn)行快照的理想選擇。

按需備份

按需備份操作可以將復(fù)制的 Kubernetes 對象的壓縮文件上傳到云對象存儲中,也可以調(diào)用云環(huán)境提供的 API 來創(chuàng)建持久化卷的磁盤快照。我們可以選擇指定在備份期間執(zhí)行的備份 hook,比如你可能需要在拍攝快照之前告訴數(shù)據(jù)庫將其內(nèi)存中的緩沖區(qū)刷新到磁盤。

需要注意的是集群備份并不是嚴(yán)格的原子備份,如果在備份時創(chuàng)建或編輯 Kubernetes 對象,則它們可能不會被包含在備份中,是可能出現(xiàn)這種狀況的。

定時備份

通過定時操作,我們可以定期備份數(shù)據(jù),第一次創(chuàng)建日程表時將執(zhí)行第一次備份,隨后的備份將按日程表指定的間隔進(jìn)行備份,這些間隔由 Cron 表達(dá)式指定。

定時備份保存的名稱為 <SCHEDULE NAME>-<TIMESTAMP>,其中 <TIMESTAMP> 格式為 YYYYMMDDhhmmss。

備份還原

通過還原操作,我們可以從以前創(chuàng)建的備份中還原所有對象和持久卷,此外我們還可以僅還原對象和持久卷的子集,Velero 支持多個命名空間重新映射。例如在一次還原操作中,可以在命名空間 def 下重新創(chuàng)建命名空間 abc 中的對象,或在 456 之下重新創(chuàng)建名稱空間 123 中的對象。

還原的默認(rèn)名稱為 <BACKUP NAME>-<TIMESTAMP>,<TIMESTAMP> 格式為 YYYYMMDDhhmmss,還可以指定自定義名稱,恢復(fù)的對象還包括帶有鍵 velero.io/restore-name 和值的標(biāo)簽 <RESTORE NAME>。

默認(rèn)情況下,備份存儲位置以讀寫模式創(chuàng)建,但是,在還原期間,可以將備份存儲位置配置為只讀模式,這將禁用該存儲位置的備份創(chuàng)建和刪除,這對于確保在還原方案期間不會無意間創(chuàng)建或刪除任何備份非常有用。此外我們還可以選擇指定在還原期間或還原資源后執(zhí)行的還原 hook,例如可能需要在數(shù)據(jù)庫應(yīng)用程序容器啟動之前執(zhí)行自定義數(shù)據(jù)庫還原操作。

備份流程

執(zhí)行命令 velero backup create test-backup 的時候,會執(zhí)行下面的操作:

  •  Velero 客戶端調(diào)用 Kubernetes APIServer 創(chuàng)建 Backup 這個 CRD 對象
  •  Backup 控制器 watch 到新的 Backup 對象被創(chuàng)建并執(zhí)行驗(yàn)證
  •  Backup 控制器開始執(zhí)行備份,通過查詢 APIServer 來獲取資源收集數(shù)據(jù)進(jìn)行備份
  •  Backup 控制器調(diào)用對象存儲服務(wù),比如 S3 上傳備份文件

默認(rèn)情況下 velero backup create 支持任何持久卷的磁盤快照,可以通過指定其他參數(shù)來調(diào)整快照,可以使用 --snapshot-volumes=false 選項(xiàng)禁用快照。

設(shè)置備份過期時間

創(chuàng)建備份時,可以通過添加標(biāo)志 --ttl 來指定 TTL,如果未指定,則將默認(rèn)的 TTL 值為30天,如果 Velero 檢測到有備份資源已過期,它將刪除以下相應(yīng)備份數(shù)據(jù):

  •  備份資源
  •  來自云對象存儲的備份文件
  •  所有 PersistentVolume 快照
  •  所有關(guān)聯(lián)的還原

同步對象存儲

Velero 將對象存儲視為資源的來源,它不斷檢查以確保始終存在正確的備份資源,如果存儲桶中有格式正確的備份文件,但 Kubernetes APIServer 中沒有相應(yīng)的備份資源,則 Velero 會將信息從對象存儲同步到 Kubernetes,這使還原功能可以在集群遷移方案中工作,在該方案中,新集群中不存在原始的備份對象。同樣,如果備份對象存在于 Kubernetes 中,但不存在于對象存儲中,則由于備份壓縮包不再存在,它將從 Kubernetes 中刪除。

備份存儲位置和卷快照位置

Velero 有兩個自定義資源 BackupStorageLocation 和 VolumeSnapshotLocation,用于配置 Velero 備份及其關(guān)聯(lián)的持久卷快照的存儲位置。

  •  BackupStorageLocation:定義為存儲區(qū),存儲所有 Velero 數(shù)據(jù)的存儲區(qū)中的前綴以及一組其他特定于提供程序的字段,后面部分會詳細(xì)介紹該部分所包含的字段。
  •  VolumeSnapshotLocation:完全由提供程序提供的特定的字段(例如AWS區(qū)域,Azure資源組,Portworx快照類型等)定義。

用戶可以預(yù)先配置一個或多個可能的 BackupStorageLocations 對象,也可以預(yù)先配置一個或多個 VolumeSnapshotLocations 對象,并且可以在創(chuàng)建備份時選擇應(yīng)該存儲備份和相關(guān)快照的位置。

此配置設(shè)計支持許多不同的用法,包括:

  •  在單個 Velero 備份中創(chuàng)建不止一種持久卷的快照。例如,在同時具有 EBS 卷和 Portworx 卷的集群中
  •  在不同地區(qū)將數(shù)據(jù)備份到不同的存儲中
  •  對于支持它的卷提供程序(例如Portworx),可以將一些快照存儲在本地集群中,而將其他快照存儲在云中

安裝

在 Github Release 頁面(https://github.com/vmware-tanzu/velero/releases)下載指定的 velero 二進(jìn)制客戶端安裝包,比如這里我們下載最新穩(wěn)定版本 v1.6.3, 

  1. ➜  ~ wget https://github.com/vmware-tanzu/velero/releases/download/v1.6.3/velero-v1.6.3-darwin-amd64.tar.gz  
  2. # 如果有限制,也可以替換成下面的 URL 加速下載  
  3. # wget https://download.fastgit.org/vmware-tanzu/velero/releases/download/v1.6.3/velero-v1.6.3-darwin-amd64.tar.gz  
  4. ➜  ~ tar -zxvf velero-v1.6.3-darwin-amd64.tar.gz && cd velero-v1.6.3-darwin-amd64  
  5. ➜  ~ tree .  
  6.  
  7. ├── LICENSE  
  8. ├── examples  
  9. │   ├── README.md  
  10. │   ├── minio  
  11. │   │   └── 00-minio-deployment.yaml  
  12. │   └── nginx-app  
  13. │       ├── README.md  
  14. │       ├── base.yaml  
  15. │       └── with-pv.yaml  
  16. └── velero  
  17. 3 directories, 7 files 

將根目錄下面的 velero 二進(jìn)制文件拷貝到 PATH 路徑下面: 

  1. ➜  ~ cp velero /usr/local/bin && chmod +x /usr/local/bin/velero  
  2. ➜  ~ velero version  
  3. Client:  
  4.         Version: v1.6.3  
  5.         Git commit: 5fe3a50bfddc2becb4c0bd5e2d3d4053a23e95d2 
  6. <error getting server version: no matches for kind "ServerStatusRequest" in version "velero.io/v1"> 

安裝 MINIO

這里我們可以使用 minio 來代替云環(huán)境的對象存儲,在上面解壓的壓縮包中包含一個 examples/minio/00-minio-deployment.yaml 的資源清單文件,為了測試方便可以將其中的 Service 更改為 NodePort 類型,我們可以配置一個 console-address 來提供一個 console 頁面的訪問入口,完整的資源清單文件如下所示: 

  1. apiVersion: v1  
  2. kind: Namespace  
  3. metadata:  
  4.   name: velero  
  5. --- 
  6. apiVersion: apps/v1  
  7. kind: Deployment  
  8. metadata:  
  9.   namespace: velero  
  10.   name: minio  
  11.   labels:  
  12.     component: minio  
  13. spec:  
  14.   strategy:  
  15.     type: Recreate  
  16.   selector:  
  17.     matchLabels:  
  18.       component: minio  
  19.   template:  
  20.     metadata:  
  21.       labels:  
  22.         component: minio  
  23.     spec:  
  24.       volumes:  
  25.       - name: storage  
  26.         emptyDir: {}  
  27.       - name: config  
  28.         emptyDir: {}  
  29.       containers:  
  30.       - name: minio  
  31.         image: minio/minio:latest  
  32.         imagePullPolicy: IfNotPresent  
  33.         args:  
  34.         - server  
  35.         - /storage  
  36.         - --config-dir=/config  
  37.         - --console-address=:9001  
  38.         env:  
  39.         - name: MINIO_ACCESS_KEY  
  40.           value: "minio"  
  41.         - name: MINIO_SECRET_KEY  
  42.           value: "minio123"  
  43.         ports:  
  44.         - containerPort: 9000  
  45.         - containerPort: 9001  
  46.         volumeMounts:  
  47.         - name: storage  
  48.           mountPath: "/storage"  
  49.         - name: config  
  50.           mountPath: "/config"  
  51. ---  
  52. apiVersion: v1  
  53. kind: Service  
  54. metadata:  
  55.   namespace: velero  
  56.   name: minio  
  57.   labels:  
  58.     component: minio  
  59. spec:  
  60.   type: NodePort  
  61.   ports:  
  62.     - name: api  
  63.       port: 9000  
  64.       targetPort: 9000  
  65.     - name: console  
  66.       port: 9001  
  67.       targetPort: 9001  
  68.   selector:  
  69.     component: minio  
  70. ---  
  71. apiVersion: batch/v1  
  72. kind: Job  
  73. metadata:  
  74.   namespace: velero  
  75.   name: minio-setup  
  76.   labels:  
  77.     component: minio  
  78. spec:  
  79.   template:  
  80.     metadata:  
  81.       name: minio-setup  
  82.     spec: 
  83.        restartPolicy: OnFailure  
  84.       volumes:  
  85.       - name: config  
  86.         emptyDir: {}  
  87.       containers:  
  88.       - name: mc  
  89.         image: minio/mc:latest  
  90.         imagePullPolicy: IfNotPresent  
  91.         command:  
  92.         - /bin/sh  
  93.         - -c  
  94.         - "mc --config-dir=/config config host add velero http://minio:9000 minio minio123 && mc --config-dir=/config mb -p velero/velero"  
  95.         volumeMounts:  
  96.         - name: config  
  97.           mountPath: "/config" 

然后直接部署在 Kubernetes 集群中即可: 

  1. ➜  ~ kubectl apply -f examples/minio/00-minio-deployment.yaml  
  2. namespace/velero created  
  3. deployment.apps/minio created  
  4. service/minio created  
  5. job.batch/minio-setup created  
  6. ➜  ~ kubectl get pods -n velero  
  7. NAME                     READY   STATUS      RESTARTS   AGE  
  8. minio-5b96ffddf8-x8s7p   1/1     Running     0          2m48s  
  9. minio-setup-rhc4d        0/1     Completed   1          2m48s  
  10. ➜  ~ kubectl get svc -n velero  
  11. NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                         AGE  
  12. minio   NodePort   10.103.132.34   <none>        9000:32036/TCP,9001:31925/TCP   3m56s 

然后我們可以通過 http://<nodeip>:31925 訪問 minio 的 console 頁面,使用 minio 與 minio 進(jìn)行登錄即可:

當(dāng)然如果需要在不同 Kubernetes 和存儲池集群備份與恢復(fù)數(shù)據(jù),需要將 minio 服務(wù)端安裝在 Kubernetes 集群外,保證在集群發(fā)生災(zāi)難性故障時,不會對備份數(shù)據(jù)產(chǎn)生影響,可以通過二進(jìn)制的方式進(jìn)行安裝。

在待安裝 minio 的服務(wù)器上下載二進(jìn)制包 

  1. ➜  ~ wget https://dl.minio.io/server/minio/release/linux-amd64/minio  
  2. ➜  ~ chmod +x minio  
  3. ➜  ~ sudo mv minio /usr/local/bin/  
  4. ➜  ~ minio --version 

準(zhǔn)備對象存儲的磁盤,這里我們跳過該步驟,可以使用 systemd 來方便管理 minio 服務(wù),對于使用 systemd init 系統(tǒng)運(yùn)行系統(tǒng)的人,請創(chuàng)建用于運(yùn)行 minio 服務(wù)的用戶和組: 

  1. ➜  ~ sudo groupadd --system minio  
  2. ➜  ~ sudo useradd -s /sbin/nologin --system -g minio minio 

為 /data(上述步驟準(zhǔn)備好的磁盤掛載位置)目錄提供 minio 用戶所有權(quán): 

  1. ➜  ~ sudo chown -R minio:minio /data/v 

為 minio 創(chuàng)建 systemd 服務(wù)單元文件: 

  1. ➜  ~ vi /etc/systemd/system/minio.service  
  2. [Unit]  
  3. Description=Minio  
  4. Documentation=https://docs.minio.io  
  5. Wants=network-online.target  
  6. After=network-online.target  
  7. AssertFileIsExecutable=/usr/local/bin/minio  
  8. [Service] 
  9. WorkingDirectory=/data 
  10. User=minio  
  11. Group=minio  
  12. EnvironmentFile=-/etc/default/minio  
  13. ExecStartPre=/bin/bash -c "if [ -z \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /etc/default/minio\"; exit 1; fi"  
  14. ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES  
  15. # Let systemd restart this service always  
  16. Restart=always  
  17. # Specifies the maximum file descriptor number that can be opened by this process 
  18.  LimitNOFILE=65536  
  19. # Disable timeout logic and wait until process is stopped  
  20. TimeoutStopSec=infinity  
  21. SendSIGKILL=no  
  22. [Install]  
  23. WantedBy=multi-user.target 

創(chuàng)建 minio 環(huán)境文件 /etc/default/minio: 

  1. # Volume to be used for Minio server.  
  2. MINIO_VOLUMES="/data"  
  3. # Use if you want to run Minio on a custom port.  
  4. MINIO_OPTS="--address :9000"  
  5. # Access Key of the server.  
  6. MINIO_ACCESS_KEY=minio  
  7. # Secret key of the server.  
  8. MINIO_SECRET_KEY=minio123 

其中 MINIO_ACCESS_KEY 為長度至少為3個字符的訪問密鑰,MINIO_SECRET_KEY 為最少8個字符的密鑰。重新加載 systemd 并啟動 minio 服務(wù): 

  1. ➜  ~ sudo systemctl daemon-reload  
  2. ➜  ~ sudo systemctl start minio 

關(guān)于 minio 的更多使用方法可以參考官方文檔 https://docs.min.io/ 了解更多。

安裝 velero 服務(wù)端

我們可以使用 velero 客戶端來安裝服務(wù)端,也可以使用 Helm Chart 來進(jìn)行安裝,比如這里我們用客戶端來安裝,velero 命令默認(rèn)讀取 kubectl 配置的集群上下文,所以前提是 velero 客戶端所在的節(jié)點(diǎn)有可訪問集群的 kubeconfig 配置。

首先準(zhǔn)備密鑰文件,在當(dāng)前目錄建立一個空白文本文件,內(nèi)容如下所示: 

  1. [default]  
  2. aws_access_key_id=<access key id>  
  3. aws_secret_access_key=<secret access key> 

替換為之前步驟中 minio 的對應(yīng) access key id 和 secret access key如果 minio 安裝在 kubernetes 集群內(nèi)時按照如下命令安裝 velero 服務(wù)端: 

  1. ➜  ~ velero install    \  
  2.      --provider aws   \  
  3.      --bucket velero   \  
  4.      --image velero/velero:v1.6.3  \  
  5.   --plugins velero/velero-plugin-for-aws:v1.2.1  \  
  6.   --namespace velero  \  
  7.   --secret-file ./credentials-velero  \  
  8.   --use-volume-snapshots=false \  
  9.      --use-restic \  
  10.   --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://minio.velero.svc:9000  
  11. ......  
  12. DaemonSet/restic: created  
  13. Velero is installed! ⛵ Use 'kubectl logs deployment/velero -n velero' to view the status.  
  14. ➜  ~ kubectl get pods -n velero 
  15. NAME                      READY   STATUS      RESTARTS   AGE  
  16. minio-5b96ffddf8-x8s7p    1/1     Running     0          4m1s  
  17. minio-setup-rhc4d         0/1     Completed   1          4m1s  
  18. restic-2qn94              1/1     Running     0          22s  
  19. velero-664598d6cc-dpvxn   1/1     Running     0          22s 

由于我們這里準(zhǔn)備使用 minio 來作為對象存儲,minio 是兼容 S3 的,所以這里我們配置的 provider(聲明使用的 Velero 插件類型)是 aws,--secret-file 用來提供訪問 minio 的密鑰,--use-restic 表示使用開源免費(fèi)備份工具 restic 備份和還原持久卷數(shù)據(jù),啟用該參數(shù)后會部署一個名為 restic 的 DaemonSet 對象,--plugins 使用的 velero 插件,我們使用 AWS S3 兼容插件。

安裝完成后 velero 的服務(wù)端就部署成功了。

測試

比如現(xiàn)在我們部署一個 mysql 應(yīng)用,資源清單文件如下所示: 

  1. # mysql-deployment.yaml  
  2. apiVersion: v1  
  3. kind: PersistentVolumeClaim  
  4. metadata:  
  5.   name: mysql-pv-claim  
  6.   labels:  
  7.     app: mysql  
  8. spec:  
  9.   accessModes:  
  10.     - ReadWriteOnce  
  11.   resources:  
  12.     requests:  
  13.       storage: 20Gi  
  14. ---  
  15. apiVersion: apps/v1  
  16. kind: Deployment  
  17. metadata:  
  18.   name: mysql  
  19.   labels:  
  20.     app: mysql  
  21. spec:  
  22.   selector:  
  23.     matchLabels:  
  24.       app: mysql  
  25.   template:  
  26.     metadata:  
  27.       labels:  
  28.         app: mysql  
  29.     spec:  
  30.       containers:  
  31.       - image: mysql:5.6  
  32.         name: mysql  
  33.         env:  
  34.         - name: MYSQL_ROOT_PASSWORD  
  35.           valueFrom:  
  36.             secretKeyRef:  
  37.               name: mysql-pass  
  38.               key: password  
  39.         livenessProbe:  
  40.           tcpSocket:  
  41.             port: 3306  
  42.         ports:  
  43.         - containerPort: 3306  
  44.           name: mysql  
  45.         volumeMounts:  
  46.         - name: mysql-persistent-storage  
  47.           mountPath: /var/lib/mysql  
  48.       volumes:  
  49.       - name: mysql-persistent-storage  
  50.         persistentVolumeClaim:  
  51.           claimName: mysql-pv-claim 

直接部署上面的應(yīng)用: 

  1. ➜  ~ kubectl create namespace kube-demo  
  2. ➜  ~ kubectl create secret generic mysql-pass --from-literal=password=password321 -n kube-demo  
  3. ➜  ~ kubectl apply -f mysql-deployment.yaml -n kube-demo  
  4. ➜  ~ kubectl get pods -n kube-demo  
  5. NAME                    READY   STATUS    RESTARTS   AGE  
  6. mysql-c57f676dd-gw5pf   1/1     Running   0          51s 

比如現(xiàn)在我們創(chuàng)建一個新的數(shù)據(jù)庫 velero: 

  1. ➜  ~ kubectl exec -it -n kube-demo mysql-c57f676dd-gw5pf -- /bin/bash  
  2. mysql-c57f676dd-gw5pf:/# mysql -uroot -p  
  3. Enter password:  
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  5. Your MySQL connection id is 7  
  6. Server version: 5.6.51 MySQL Community Server (GPL)  
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.  
  8. Oracle is a registered trademark of Oracle Corporation and/or its  
  9. affiliates. Other names may be trademarks of their respective  
  10. owners.  
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  12. mysql> show databases;  
  13. +--------------------+  
  14. | Database           |  
  15. +--------------------+  
  16. | information_schema |  
  17. | mysql              |  
  18. | performance_schema |  
  19. +--------------------+  
  20. 3 rows in set (0.00 sec)  
  21. mysql> create database velero;  
  22. Query OK, 1 row affected (0.00 sec) 
  23. mysql> show databases;  
  24. +--------------------+  
  25. | Database           |  
  26. +--------------------+  
  27. | information_schema |  
  28. | mysql              |  
  29. | performance_schema |  
  30. | velero             |  
  31. +--------------------+  
  32. 4 rows in set (0.00 sec)  
  33. mysql> 

現(xiàn)在我們來執(zhí)行一個備份: 

  1. ➜  ~ velero backup create mysql-backup --include-namespaces kube-demo --default-volumes-to-restic  
  2. Backup request "mysql-backup" submitted successfully.  
  3. Run `velero backup describe mysql-backup` or `velero backup logs mysql-backup` for more details. 

其中我們指定的 --default-volumes-to-restic 參數(shù)表示使用 restic 備份持久卷到 minio,--include-namespaces 用來備份該命名空間下的所有資源,不包括集群資源,此外還可以使用 --include-resources 指定要備份的資源類型 ,--include-cluster-resources 指定是否備份集群資源。

該命令請求創(chuàng)建一個對項(xiàng)目(命名空間)的備份,備份請求發(fā)送之后可以用命令查看備份狀態(tài),等到 STATUS 列變?yōu)?Completed 表示備份完成。 

  1. ➜  ~ velero backup get  
  2. NAME           STATUS      ERRORS   WARNINGS   CREATED                         EXPIRES   STORAGE LOCATION   SELECTOR  
  3. mysql-backup   Completed   0        0          2021-09-16 16:58:51 +0800 CST   29d       default            <none>  
  4. ➜  ~ velero backup describe mysql-backup  
  5. Name:         mysql-backup  
  6. Namespace:    velero 
  7. abels:       velero.io/storage-location=default  
  8. Annotations:  velero.io/source-cluster-k8s-gitversion=v1.18.4-tke.6  
  9.               velero.io/source-cluster-k8s-major-version=1  
  10.               velero.io/source-cluster-k8s-minor-version=18 
  11. Phase:  Completed  
  12. Errors:    0  
  13. Warnings:  0  
  14. Namespaces:  
  15.   Included:  kube-demo  
  16.   Excluded:  <none>  
  17. Resources:  
  18.   Included:        *  
  19.   Excluded:        <none>  
  20.   Cluster-scoped:  auto  
  21. Label selector:  <none>  
  22. Storage Location:  default  
  23. Velero-Native Snapshot PVs:  auto  
  24. TTL:  720h0m0s  
  25. Hooks:  <none>  
  26. Backup Format Version:  1.1.0  
  27. Started:    2021-09-16 16:58:51 +0800 CST  
  28. Completed:  2021-09-16 16:59:08 +0800 CST   
  29. Expiration:  2021-10-16 16:58:51 +0800 CST  
  30. Total items to be backed up:  30  
  31. Items backed up:              30   
  32. Velero-Native Snapshots: <none included> 
  33. Restic Backups (specify --details for more information):  
  34.   Completed:  1 

備份完成后可以去 minio 的 bucket 上查看是否有對應(yīng)的備份數(shù)據(jù):

現(xiàn)在我們刪除應(yīng)用所在的命名空間來模擬生產(chǎn)環(huán)境發(fā)生災(zāi)難或運(yùn)維錯誤導(dǎo)致應(yīng)用失敗的場景: 

  1. ➜  ~ kubectl delete namespace kube-demo 

這個時候我們肯定訪問不了我們的 MySQL 數(shù)據(jù)庫了,這時候我們可以用一條命令,使用 velero 從 minio 中來恢復(fù)應(yīng)用和數(shù)據(jù): 

  1. ➜  ~ velero restore create --from-backup mysql-backup  
  2. Restore request "mysql-backup-20210916172100" submitted successfully.  
  3. Run `velero restore describe mysql-backup-20210916172100` or `velero restore logs mysql-backup-20210916172100` for more details.  

同樣我們可以使用 velero restore get 來查看還原的進(jìn)度,等到 STATUS 列變?yōu)?Completed 表示還原完成: 

  1. ➜  ~ velero restore get  
  2. NAME                          BACKUP         STATUS      STARTED                         COMPLETED                       ERRORS   WARNINGS   CREATED                       SELECTOR 
  3. mysql-backup-20210916172100   mysql-backup   Completed   2021-09-16 17:21:00 +0800 CST   2021-09-16 17:21:22 +0800 CST   0        0          2021-09-16 17:21:00 +0800 CST   <none> 
  4. ➜  ~ velero restore describe mysql-backup-20210916172100  
  5. Name:         mysql-backup-20210916172100  
  6. Namespace:    velero  
  7. Labels:       <none>  
  8. Annotations:  <none> 
  9. Phase:                       Completed  
  10. Total items to be restored:  10  
  11. Items restored:              10  
  12. Started:    2021-09-16 17:21:00 +0800 CST  
  13. Completed:  2021-09-16 17:21:22 +0800 CST  
  14. Backup:  mysql-backup 
  15. Namespaces:  
  16.   Included:  all namespaces found in the backup  
  17.   Excluded:  <none>  
  18. Resources:  
  19.   Included:        *  
  20.   Excluded:        nodes, events, events.events.k8s.io, backups.velero.io, restores.velero.io, resticrepositories.velero.io  
  21.   Cluster-scoped:  auto  
  22. Namespace mappings:  <none>  
  23. Label selector:  <none>  
  24. Restore PVs:  auto  
  25. Restic Restores (specify --details for more information):  
  26.   Completed:  1  
  27. Preserve Service NodePorts:  auto 

還原完成后我們再去查看之前我們的 kube-demo 命名空間下面的應(yīng)用數(shù)據(jù)是否正確: 

  1. ➜  ~ kubectl get ns kube-demo  
  2. NAME        STATUS   AGE  
  3. kube-demo   Active   3m29s  
  4. ➜  ~ kubectl get pods -n kube-demo  
  5. NAME                    READY   STATUS    RESTARTS   AGE  
  6. mysql-c57f676dd-gw5pf   1/1     Running   0          6m28s  
  7. ➜  ~ kubectl exec -it -n kube-demo mysql-c57f676dd-gw5pf -- /bin/bash  
  8. root@mysql-c57f676dd-gw5pf:/# mysql -uroot -p  
  9. Enter password: 
  10. elcome to the MySQL monitor.  Commands end with ; or \g.  
  11. Your MySQL connection id is 42 
  12. Server version: 5.6.51 MySQL Community Server (GPL)  
  13. Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.  
  14. Oracle is a registered trademark of Oracle Corporation and/or its  
  15. affiliates. Other names may be trademarks of their respective  
  16. owners.  
  17. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  18. mysql> show databases;  
  19. +---------------------+  
  20. | Database            |  
  21. +---------------------+  
  22. | information_schema  |  
  23. | #mysql50#lost+found |  
  24. | mysql               |  
  25. | performance_schema  |  
  26. | velero              |  
  27. +---------------------+  
  28. 5 rows in set (0.00 sec)  
  29. mysql> 

可以看到我們的創(chuàng)建的 velero 數(shù)據(jù)庫依然存在,證明已經(jīng)完全恢復(fù)了。

只要我們將每個 velero 實(shí)例指向相同的對象存儲,velero 就能將資源從一個群集遷移到另一個群集。此外還支持定時備份,觸發(fā)備份 Hooks 等操作,更多資料請查閱官方文檔:https://velero.io/docs/ 

 

責(zé)任編輯:龐桂玉 來源: 奇妙的Linux世界
相關(guān)推薦

2021-09-27 05:42:44

VeleroKubernetes 公有云

2022-09-23 17:26:04

VeleroKubernetes

2019-11-28 10:15:35

TimeShiftUbuntuLinux

2022-08-05 08:48:33

KubernetesEtcd數(shù)據(jù)

2021-12-21 15:17:53

Kubernetes緩存Linux

2020-03-19 11:12:21

云計算KubernetesDocker

2022-08-11 08:41:31

CrossplaneVCluster

2022-08-10 10:46:13

?CrossplanKubernete插件

2021-12-29 17:24:16

Kubernetes集群事件

2021-06-25 15:53:25

Kubernetes程序技巧

2024-05-23 13:49:00

Kuberneteetcd集群

2015-08-21 10:40:10

SQL Server備份還原

2022-09-23 09:28:11

KubeSealKubernetesSecret

2023-10-19 19:42:25

IstioPodkubernetes

2010-12-31 14:42:51

ExchangeSer

2021-07-01 11:29:45

KubernetesGrafana監(jiān)控

2022-01-21 09:45:42

Mozilla SOKubernetesLinux

2011-09-15 09:49:57

HBase

2017-07-10 16:00:47

Mysql數(shù)據(jù)備份數(shù)據(jù)還原

2022-02-17 11:08:00

KubernetesMySQL運(yùn)維
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號