1、介绍
Velero 为您提供了备份和恢复 Kubernetes 集群资源和持久卷的工具。您可以使用公共云平台或本地运行 Velero。
功能如下;
- 备份集群并在丢失时恢复。
- 将集群资源迁移到其他集群。
- 将您的生产集群复制到开发和测试集群。
2、安装
目前velero有两种安装方式,一种是通过 velero cli 工具来安装,另一种是通过 helm 方式来安装,如果你们公司使用了标准化的流程,例如 Gitops,则推荐使用 helm 方式来安装,其他情况下推荐使用 velero cli 工具来安装,更简单
2.1、Velero cli 部署
velero cli 部署方式,后端存储使用 minio
创建 velero 连接 minio 的认证文件 credentials-velero,内容如下:
[default]
aws_access_key_id = <YOUR_MINIO_USERNAME>
aws_secret_access_key = <YOUR_MINIO_PASSWORD>
部署 velero
velero install \
--provider aws \
--plugins velero/velero-plugin-for-aws:v1.6.1 \
--bucket velero-backup \
--secret-file credentials-velero \
--use-node-agent \
--default-volumes-to-fs-backup \
--use-volume-snapshots=false \
--uploader-type=kopia \
--backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.0.100:9000
2.2、Helm 部署
helm部署方式,后端存储使用 s3
在 s3 上面创建一个bucket,名字为 velero-backup
为 Velero 设置权限:
有两种方式可以为 velero 设置权限,一种是为用户设置 IAM 权限,另一种则是为 Pod 设置 IAM权限,从安全性的角度考虑,建议使用第二种方式
一、创建 IAM 用户:
aws iam create-user --user-name velero
- 为 velero 用户创建授权文件
cat > velero-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeVolumes",
"ec2:DescribeSnapshots",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:CreateSnapshot",
"ec2:DeleteSnapshot"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:DeleteObject",
"s3:PutObject",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Resource": [
"arn:aws:s3:::${BUCKET}/*"
]
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::${BUCKET}"
]
}
]
}
EOF
- 授权操作
aws iam put-user-policy \
--user-name velero \
--policy-name velero \
--policy-document file://velero-policy.json
- 为用户创建访问密钥
aws iam create-access-key --user-name velero
结果如下:
{
"AccessKey": {
"UserName": "velero",
"Status": "Active",
"CreateDate": "2017-07-31T22:24:41.576Z",
"SecretAccessKey": <AWS_SECRET_ACCESS_KEY>,
"AccessKeyId": <AWS_ACCESS_KEY_ID>
}
}
- 创建 Velero 的凭据文件 credentials-velero
[default]
aws_access_key_id=<AWS_ACCESS_KEY_ID>
aws_secret_access_key=<AWS_SECRET_ACCESS_KEY>
二、使用 kube2iam 设置权限
Kube2iam是一个 Kubernetes 应用程序,它允许通过注释而不是对 API 密钥进行操作来管理 Pod 的 AWS IAM 权限。
- 创建一个信任策略文档以允许角色用于 EC2 管理并承担 kube2iam 角色
cat > velero-trust-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<AWS_ACCOUNT_ID>:role/<ROLE_CREATED_WHEN_INITIALIZING_KUBE2IAM>"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
- 创建 IAM 角色
aws iam create-role --role-name velero --assume-role-policy-document file://./velero-trust-policy.json
- 为 velero 授权文件
BUCKET=<YOUR_BUCKET>
cat > velero-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeVolumes",
"ec2:DescribeSnapshots",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:CreateSnapshot",
"ec2:DeleteSnapshot"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:DeleteObject",
"s3:PutObject",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Resource": [
"arn:aws:s3:::${BUCKET}/*"
]
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::${BUCKET}"
]
}
]
}
EOF
- 为 velero 授权
aws iam put-role-policy \
--role-name velero \
--policy-name velero-policy \
--policy-document file://./velero-policy.json
三、部署 velero
通过 kustomiz 的方式来部署 velero 的 helm chat
namespace: velero
helmCharts:
- name: velero-crds
releaseName: velero-crds
version: 3.1.2
repo: https://wiremind.github.io/wiremind-helm-charts
- name: velero
releaseName: velero
version: 3.1.2
repo: https://vmware-tanzu.github.io/helm-charts
valuesInline:
serviceAccount:
server:
create: true
name: velero-server
annotations:
# 为 velero 添加访问 s3 的权限注解
eks.amazonaws.com/role-arn: "arn:aws:iam::<AWS_ACCOUNT_ID>:role/<VELERO_ROLE_NAME>"
credentials:
useSecret: false
configuration:
provider: aws
backupStorageLocation:
name: aws
# provider
provider: velero.io/aws
bucket: velero-backup
default: true
config:
# 设置你的 eks region
region: ap-southeast-1
volumeSnapshotLocation:
name: aws
provider: velero.io/aws
config:
region: ap-southeast-1
# 设置备份的文件系统为 kopia,不设置默认为 restic
# kopia 相比较 restic 更高效
uploaderType: kopia
initContainers:
- name: velero-plugin-for-aws
image: velero/velero-plugin-for-aws:v1.6.1
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /target
name: plugins
# 定义每天备份的定时任务
schedules:
eks-bakcup:
disabled: false
# UTC时间,每天 0 点执行备份任务
schedule: "0 0 * * *"
template:
# 备份的失效时间
ttl: "168h"
includedNamespaces:
# 设置需要备份的命名空间
- default
# 开启文件系统备份
deployNodeAgent: true
两种方式部署结果是一样的,如下:
kubectl get po -n velero
NAME READY STATUS RESTARTS AGE
node-agent-stw79 1/1 Running 0 10m
node-agent-tdhvp 1/1 Running 0 10m
node-agent-x2ftq 1/1 Running 0 10m
velero-7dbd7475c9-zh7lc 1/1 Running 0 10m
4、备份恢复
4.1、备份
创建测试应用 nginx
kubectl create deployment nginx --image=nginx
备份 nginx
velero backup create nginx-bak --include-namespaces default
查看 minio 备份
4.2、恢复
模拟故障,删除 nginx 服务
kubectl delete deploy nginx
恢复 nginx
velero restore create --from-backup nginx-bak
4.3、定时任务
velero cli 配置定时任务
# 每10分钟备份一次
velero create schedule nginx-bak --schedule="@every 10m" --include-namespaces default
# 每10分钟备份一次,备份保留24小时
velero create schedule nginx-bak --schedule="@every 10m" --ttl 24h --include-namespaces default
# 每天零点备份一次
velero create schedule nginx-bak --schedule="0 0 * * *" --include-namespaces default
# # 每天零点备份一次,备份保留7天
velero create schedule nginx-bak --schedule="0 0 * * *" --ttl 168h --include-namespaces default
helm 配置定时任务
velero:
# 定义每天备份的定时任务
schedules:
eks-bakcup:
disabled: false
# UTC时间,每天 0 点执行备份任务
schedule: "0 0 * * *"
template:
# 备份的失效时间
ttl: "168h"
includedNamespaces:
# 设置需要备份的命名空间
- default
MINIO中的备份结果如下:
S3中备份的结果如下:
评论区