大綱:
velero簡介
minio環(huán)境準(zhǔn)備
velero安裝论咏,備份與恢復(fù)
一呢袱、velero簡介
velero是一個(gè)有Vmware開源的工具瓢姻,是一個(gè)云原生的災(zāi)難恢復(fù)和遷移工具暴氏,采用Go語言編寫延塑,用于安全的備份、恢復(fù)和遷移Kubernetes集群資源數(shù)據(jù)答渔。
velero官網(wǎng):https://velero.io/
velero-github:https://github.com/vmware-tanzu/velero
1.1关带、velero的特性:
備份可以按集群資源的子集,按命名空間沼撕、資源類型標(biāo)簽選擇器進(jìn)行過濾宋雏,從而為備份和恢復(fù)的內(nèi)容提供高度的靈活性
支持復(fù)制當(dāng)前 Kubernetes 集群的資源到其它 Kubernetes 集群
通過聚合 API 服務(wù)器公開的資源可以輕松備份和恢復(fù)芜飘,即使它們存儲(chǔ)在單獨(dú)的 etcd 數(shù)據(jù)庫中
Velero 可以幫助你:
對(duì)集群進(jìn)行備份并在丟失的情況下進(jìn)行恢復(fù)。
將集群資源遷移到其他集群好芭。
將生產(chǎn)集群復(fù)制到開發(fā)和測(cè)試集群燃箭。
1.2、velero的組件:
velero由一個(gè)客戶端和一個(gè)服務(wù)端組成
客戶端:運(yùn)行在本地的命令行工具舍败,只要配置好kubectl和kubeconfig認(rèn)證文件就可使用,非常簡單
服務(wù)端:運(yùn)行在Kubernetes集群之上敬拓,負(fù)責(zé)執(zhí)行具體的備份和恢復(fù)操作
整體架構(gòu):
支持的對(duì)象存儲(chǔ):AWS S3 以及兼容 S3 的存儲(chǔ)邻薯,比如:ceph存儲(chǔ)、Google Cloud 存儲(chǔ) 乘凸、Aliyun OSS 存儲(chǔ)
1.3厕诡、工作流程:
基本工作原理:
Velero client 調(diào)用Kubernetes API服務(wù)器以創(chuàng)建Backup。
BackupController檢測(cè)到新的backup营勤,并驗(yàn)證灵嫌。
BackupController開始backup,通過請(qǐng)求ApiServer獲取資源來收集數(shù)據(jù)以進(jìn)行備份葛作。
BackupController將要備份的數(shù)據(jù)上傳到一個(gè)對(duì)象存儲(chǔ)服務(wù)器寿羞,如AMS S3。
默認(rèn)情況下赂蠢,velero backup create會(huì)生產(chǎn)每一個(gè)PV的磁盤快照绪穆。您可以通過指定其他參數(shù)來調(diào)整快照。運(yùn)行velero backup create --help以查看可用的參數(shù)虱岂【猎海可以使用選項(xiàng)禁用快照--snapshot-volumes=false。
思考:與其他k8s備份方式的差別
二第岖、minio環(huán)境準(zhǔn)備
minio在這里是用來保存velero的備份數(shù)據(jù)难菌,如果你有其它對(duì)象存儲(chǔ)服務(wù),也可以用它們來替換minio蔑滓。
minio是一款高性能郊酒、分布式的對(duì)象存儲(chǔ)系統(tǒng),采用Go語言實(shí)現(xiàn)烫饼,兼容Amazon S3接口猎塞,客戶端與存儲(chǔ)服務(wù)器之間采用http/https通信協(xié)議。
minio官網(wǎng):https://min.io/
minio中文網(wǎng)站:http://www.minio.org.cn/
minio支持單機(jī)部署和分布式部署杠纵,這里選擇容器方式單機(jī)部署荠耽。
2.1、安裝minio:(要先安裝好docker環(huán)境)
root@harbor01:~# mkdir -p /data/minio
root@harbor01:~# docker pull minio/minio:RELEASE.2022-04-12T06-55-35Z
RELEASE.2022-04-12T06-55-35Z: Pulling from minio/minio
a9e23b64ace0: Pull complete
38b71301a1d9: Pull complete
160197c068e7: Pull complete
ff6e4f6f8c20: Pull complete
e95e0f7339c1: Pull complete
17f004107a95: Pull complete
cb8e56fa8e8a: Pull complete
Digest: sha256:e7983a527e9e5e7158c3644ef6cdc51dca453819f95facc25eb110478dd57b43
Status: Downloaded newer image for minio/minio:RELEASE.2022-04-12T06-55-35Z
docker.io/minio/minio:RELEASE.2022-04-12T06-55-35Z
# 創(chuàng)建minio容器,如果不指定,則默認(rèn)用戶名與密碼為 minioadmin/minioadmin,可以通過環(huán)境變量自定義
root@harbor01:~# docker run --name minio \
> -p 9000:9000 \
> -p 9999:9999 \
> -d --restart=always \
> -e "MINIO_ROOT_USER=admin" \
> -e "MINIO_ROOT_PASSWORD=12345678" \
> -v /data/minio/data:/data \
> minio/minio:RELEASE.2022-04-12T06-55-35Z server /data \
> --console-address '0.0.0.0:9999'
0a7f1752b59b9285df518bb048ab3b12bbd3829fbc51d87c21db5478aa6e1f59
root@harbor01:~# docker ps
CONTAINER ID IMAGE COMMAND CRE STATUS PORTS NAMES
0a7f1752b59b minio/minio:RELEASE.2022-04-12T06-55-35Z "/usr/bin/docker-ent…" 26 Up 11 seconds 0.0.0.0:9000->9000/tcp, 0.0.0.0:9999->9999/tcp minio
2.2比藻、訪問minio并創(chuàng)建bucket
瀏覽器訪問:http://ip:9000
三铝量、部署velero
3.1倘屹、下載velero
wget https://github.com/vmware-tanzu/velero/releases/download/v1.9.3/velero-v1.9.3-linux-amd64.tar.gz
tar -xvf velero-v1.9.3-linux-amd64.tar.gz
cp velero /usr/local/bin/
3.2、配置velero認(rèn)證環(huán)境
# 認(rèn)證文件
root@ansible:/data/velero# cat velero-auth.txt
[default]
aws_access_key_id = admin
aws_secret_access_key = 12345678
# 準(zhǔn)備user-csr文件
root@ansible:/data/velero# cat awsuser-csr.json
{
"CN": "awsuser",
"hosts": [],
"key":{
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "BeiJing",
"L": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
準(zhǔn)備證書答發(fā)環(huán)境(cfssl):
# 安裝cfssl
apt install golang-cfssl
# 下載簽發(fā)所需命令 cfssl cfssl-certionfo cfssljson重命名并cp到/usr/local/bin/
https://github.com/cloudflare/cfssl/releases/download/v1.6.3/cfssljson_1.6.3_linux_amd64
https://github.com/cloudflare/cfssl/releases/download/v1.6.3/cfssl_1.6.3_linux_amd64
https://github.com/cloudflare/cfssl/releases/download/v1.6.3/cfssl-certinfo_1.6.3_linux_amd64
3.3慢叨、證書簽發(fā)
velero --kubeconfig ./awsuser.kubeconfig install --provider aws --plugins lero-plugin-for-aws:v1.3.1 --bucket velerodata --secret-file ./velero-authe-snapshots=false --namespace velero --backup-location-config region=minioe="true",s3Url=http://192.168.50.104:9000
分發(fā)證書到apiserver證書路徑
cp *.pem /etc/kubernetes/ssl/
3.4纽匙、生成k8s集群認(rèn)證config文件
export KUBE_APISERVER="https://192.168.50.101:6443"
kubectl config set-cluster kubernetes --certificate-authority=/etc/kubernetes/ssl/ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=./awsuser.kubeconfig
設(shè)置客戶端證書認(rèn)證:
kubectl config set-credentials awsuser --client-certificate=/etc/kubernetes/ssl/awsuser.pem --client-key=/etc/kubernetes/ssl/awsuser-key.pem --embed-certs=true --kubeconfig=./awsuser.kubeconfig
設(shè)置上下文參數(shù):
kubectl config set-context kubernetes --cluster=kubernetes --user=awsuser --kubeconfig=./awsuser.kubeconfig
設(shè)置默認(rèn)上下文
kubectl config use-context kubernetes --kubeconfig=./awsuser.kubeconfig
在k8s集群中創(chuàng)建awsuser賬戶:
kubectl create clusterrolebinding awsuser --clusterrole=cluster-admin --user=awsuser
驗(yàn)證config文件可用性:
kubectl --kubeconfig awsuser.kubeconfig get node
3.5、部署velero
創(chuàng)建namespace:
kubectl create ns velero
執(zhí)行安裝:
velero --kubeconfig ./awsuser.kubeconfig install --provider aws --plugins velero/velero-plugin-for-aws:v1.3.1 --bucket velerodata --secret-file ./velero-auth.txt --use-volume-snapshots=false --namespace velero --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.50.104:9000
驗(yàn)證部署:
kubectl get pod -n velero
kubectl logs velero-578458fb6-9n7h9 -n velero --tail=1000 -f
四拍谐、velero 備份與恢復(fù)
基本工作流程:
備份數(shù)據(jù):
本地velero客戶端發(fā)送備份命令烛缔,就會(huì)調(diào)用API Server創(chuàng)建Backup資源對(duì)象
服務(wù)端收到通知有新的Backup對(duì)象創(chuàng)建并執(zhí)行驗(yàn)證
服務(wù)端開始執(zhí)行備份過程,向API Server查詢需要備份的數(shù)據(jù)
服務(wù)端調(diào)用對(duì)象存儲(chǔ)服務(wù)轩拨,將備份數(shù)據(jù)保存到對(duì)象對(duì)象存儲(chǔ)上
恢復(fù)數(shù)據(jù):
本地velero客戶端發(fā)送恢復(fù)指令践瓷,就會(huì)調(diào)用API Server創(chuàng)建Restore資源對(duì)象
服務(wù)端收到通知有新的Restore對(duì)象創(chuàng)建并執(zhí)行驗(yàn)證
服務(wù)端調(diào)用對(duì)象存儲(chǔ),將指定的備份文件下載下來
服務(wù)端開始執(zhí)行恢復(fù)過程亡蓉,根據(jù)備份數(shù)據(jù)調(diào)用API Server重新創(chuàng)建相關(guān)資源對(duì)象
4.1晕翠、對(duì)default ns進(jìn)行備份:
root@ansible:/data/velero# kubectl get pod
NAME READY STATUS RESTARTS AGE
net-test1 1/1 Running 0 2m30s
net-test2 1/1 Running 0 2m18s
net-test3 1/1 Running 0 2m14s
# 備份
root@ansible:/data/velero# DATE=`date +%Y%m%d%H%M%S`
root@ansible:/data/velero# echo $DATE
20221125144852
root@ansible:/data/velero# velero backup create default-backup-${DATE} \
> --include-namespaces default \
> --kubeconfig=./awsuser.kubeconfig \
> --namespace velero
Backup request "default-backup-20221125144852" submitted successfully.
Run `velero backup describe default-backup-20221125144852` or `velero backup logs default-backup-20221125144852` for more details.
#驗(yàn)證備份
root@ansible:/data/velero# velero backup describe default-backup-20221125144852 --kubeconfig=./awsuser.kubeconfig --namespace velero
Name: default-backup-20221125144852
Namespace: velero
Labels: velero.io/storage-location=default
Annotations: velero.io/source-cluster-k8s-gitversion=v1.21.0
velero.io/source-cluster-k8s-major-version=1
velero.io/source-cluster-k8s-minor-version=21
Phase: Completed
Errors: 0
Warnings: 0
Namespaces:
Included: default
Excluded: <none>
Resources:
Included: *
Excluded: <none>
Cluster-scoped: auto
Label selector: <none>
Storage Location: default
Velero-Native Snapshot PVs: auto
TTL: 720h0m0s
Hooks: <none>
Backup Format Version: 1.1.0
Started: 2022-11-25 14:49:17 +0800 CST
Completed: 2022-11-25 14:49:22 +0800 CST
Expiration: 2022-12-25 14:49:17 +0800 CST
Total items to be backed up: 25
Items backed up: 25
Velero-Native Snapshots: <none included>
minio驗(yàn)證備份數(shù)據(jù):
4.2、恢復(fù)備份數(shù)據(jù):
模擬刪除數(shù)據(jù):
root@ansible:/data/velero# kubectl get pod
NAME READY STATUS RESTARTS AGE
net-test1 1/1 Running 0 8m41s
net-test2 1/1 Running 0 8m29s
net-test3 1/1 Running 0 8m25s
root@ansible:/data/velero# kubectl delete pod net-test3
pod "net-test3" deleted
root@ansible:/data/velero# kubectl get pod
NAME READY STATUS RESTARTS AGE
net-test1 1/1 Running 0 9m43s
net-test2 1/1 Running 0 9m31s
執(zhí)行恢復(fù):
root@ansible:/data/velero# velero restore create --from-backup default-backup-20221125144852 --wait --kubeconfig=./awsuser.kubeconfig --namespace velero
Restore request "default-backup-20221125144852-20221125145705" submitted successfully.
Waiting for restore to complete. You may safely press ctrl-c to stop waiting - your restore will continue in the background.
.
Restore completed with status: Completed. You may check for more information using the commands `velero restore describe default-backup-20221125144852-20221125145705` and `velero restore logs default-backup-20221125144852-20221125145705`.
root@ansible:/data/velero# kubectl get pod
NAME READY STATUS RESTARTS AGE
net-test1 1/1 Running 0 11m
net-test2 1/1 Running 0 11m
net-test3 1/1 Running 0 5s
注:velero在恢復(fù)時(shí)會(huì)先檢查數(shù)據(jù)是否存在砍濒,不存在的恢復(fù)數(shù)據(jù)(只恢復(fù)net-test3,不影響到其他的pod)
思考:怎么備份pv? 與etcd備份的差別
五淋肾、Velero常用命令
備份Backup
velero backup get :查看已備份的
velero backup create <backupname>:創(chuàng)建一個(gè)backup包含所有資源
velero backup create <backupname> --include-namespaces ns1,ns2:為ns1,ns2命名空間下的資源備份
velero backup create <backupname> --exclude-namespaces ns1,ns2:排除掉ns1爸邢,ns2的命名空間樊卓,創(chuàng)建備份
velero backup create <backupname> --include-resources resource1,resource2:為指定資源備份
velero backup create --exclude-resources resource1,resource2:不備份指定資源
--storage-location <localpath>:將創(chuàng)建的備份保存到本地路徑下
-l, --selector:通過指定label來匹配要back up的資源
除此以外還包括:delete、describe甲棍、logs
恢復(fù)Restore
velero restore get:查看已經(jīng)restore的資源
velero restore create restore-1 --from-backup backup-1:從backup-1恢復(fù)
velero restore create --from-backup backup-2 --include-resources persistentvolumeclaims,persistentvolumes:僅恢復(fù)指定資源简识,同樣使用--exclude-resources:不恢復(fù)某資源
velero restore create --from-schedule schedule-1:從創(chuàng)建的schedule恢復(fù)
除此以外還包括:delete、describe感猛、logs
六七扰、Schedule定時(shí)備份
Schedule是針對(duì)backup的,是獨(dú)立于backup之外的一種資源陪白,但他本身也是一個(gè)backup只不過是具有了定時(shí)的功能颈走,符合Cron規(guī)則。
velero create schedule NAME --schedule="0 */6 * * *":每6小時(shí)自動(dòng)備份一次
velero create schedule NAME --schedule="@every 24h" --include-namespaces web:因?yàn)閟chedule也是一種backup咱士,所以創(chuàng)建backup指定的參數(shù)這里也都可以使用
除此以外還包括:delete立由、describe、logs