美文网首页paas管理平台
使用velero备份恢复k8s集群

使用velero备份恢复k8s集群

作者: 行者深蓝 | 来源:发表于2021-09-28 16:04 被阅读0次

    Velero 组件

    Velero 组件一共分两部分,分别是客户端和服务端。

    • 客户端:运行在本地的命令行的工具
    • 服务端:运行在 Kubernetes 集群中

    Velero客户端安装

    部署在k8s master节点,或者安装有kubectl命令,包含对应集群 kubeconfig 配置的机器上

    wget https://github.com/vmware-tanzu/velero/releases/download/v1.6.3/velero-v1.6.3-linux-amd64.tar.gz
    tar zxvf velero-v1.6.3-linux-amd64.tar.gz
    mv velero-v1.6.3-linux-amd64/velero /usr/local/bin/
    

    Velero服务端安装

    创建一个兼容S3协议的对象存储, 记录对应的

    1. bucket name
    2. region
    3. AK/SK
    4. endpoint
    • 使用Helm3部署
    kubectl create namespace velero
    kubectl create secret docker-registry registry-velero-secret \
            --namespace=velero \
            --docker-server=uhub.service.ucloud.cn/ucloud_pts \
            --docker-username='xxxxxx' \
            --docker-password='xxxxxx'
    
    helm repo add vmware-tanzu https://vmware-tanzu.github.io/helm-charts
    helm repo update
    
    cat > velero-values.yaml << EOF
    image:
      repository: uhub.service.ucloud.cn/ucloud_pts/velero
      tag: v1.6.3
      imagePullSecrets: 
        - name: registry-velero-secret
    credentials:
      useSecret: true
      secretContents:
        cloud: |
          [default]
          aws_access_key_id=<AWS_ACCESS_KEY_ID>
          aws_secret_access_key=<AWS_SECRET_ACCESS_KEY>
    configuration:
      features:
        - EnableCSI
      provider: aws
      backupStorageLocation:
        name: default
        bucket: velero
        config:
          region: <region_zone_name>
          s3ForcePathStyle: true
          s3Url: http://<bucket-endpoint-domain>
          publicUrl: http://<bucket-endpoint-domain>
      volumeSnapshotLocation:
        name: default
        config:
          region: <region_zone_name>
          s3ForcePathStyle: true
          s3Url: http://<bucket-endpoint-domain>
          publicUrl: http://<bucket-endpoint-domain>
    initContainers:
      - name: velero-plugin-for-aws
        image: velero/velero-plugin-for-aws:v1.2.0
        imagePullPolicy: IfNotPresent
        volumeMounts:
          - mountPath: /target
            name: plugins
    backupsEnabled: true
    snapshotsEnabled: true
    deployRestic: true
    configMaps:
      restic-restore-action-config:
        labels:
          velero.io/plugin-config: ""
          velero.io/restic: RestoreItemAction
        data:
          image: uhub.service.ucloud.cn/ucloud_pts/velero-restic-restore-helper:v1.6.3 
    metrics:
      enabled: true
      serviceMonitor:
        enabled: true
        additionalLabels:
          release: prometheus
    EOF
    
    helm upgrade --install velero vmware-tanzu/velero \
    --namespace velero \
    --create-namespace \
    -f velero-values.yaml
    

    备份参考

    • 手动备份

      • 备份集群全部资源 velero backup create cluster-full-backup --snapshot-volumes=false --exclude-namespaces kube-system
      • 备份除kube-system外的全部资源 velero backup create cluster-backup-without-kube-system --snapshot-volumes=false --exclude-namespaces kube-system
      • 备份dev-ns全部资源 velero backup create dev-ns-backup --snapshot-volumes=false --include-namespaces dev-ns
    • 备份带有PVC的Pod

    使用 Restic 给带有 PV 的 Pod 进行备份,必须先给 Pod 加上注解

    kubectl  get pvc -n monitor 
    kubectl  -n monitor annotate pod/grafana-674899bc9f-rnz5h backup.velero.io/backup-volumes=grafana-pv-xxxxx
    velero backup create gitlab-backup --snapshot-volumes=false --include-namespaces monitor
    
    • 定期备份

      • 每隔1小时进行备份 velero create schedule <SCHEDULE NAME> --schedule="0 */1 * * *" --snapshot-volumes=false
      • 每日1点进行备份,备份保留72小时 velero create schedule <SCHEDULE NAME> --schedule="0 1 * * *" --snapshot-volumes=false --ttl 72h
      • 每5小时进行一次备份 velero create schedule <SCHEDULE NAME> --schedule="@every 5h" --snapshot-volumes=false
      • 每日对指定namespace 进行一次备份velero create schedule <SCHEDULE NAME> --schedule="@every 24h" --snapshot-volumes=false --include-namespaces ns-dev
    • 资源查看

      • velero get backup 备份查看
      • velero get schedule 查看定时备份
      • velero get restore 查看已有的恢复
      • velero get plugins 查看插件
    • 恢复操作

      • velero restore create --from-backup all-ns-backup #恢复集群所有备份,(对已经存在的服务不会覆盖)
      • ` velero restore create --from-backup all-ns-backup --include-namespaces default,nginx-example #仅恢复 default nginx-example namespace
      • velero restore create restore-for-test --from-backup everyday-1-20210203131802 --namespace-mappings test-velero:test-velero-1 将test-velero 命名空间资源恢复到test-velero-1下面,可以将资源还原到与其备份来源不同的命名空间中( 使用--namespace-mappings标志)
    • 删除velero备份

      • 删除已创建备份 velero backup delete pvc-backup -n velero
      • 操作CRD删除备份 kubectl delete backups pvc-backup -n velero

    注意事项

    • 备份使用volumes 的Pod,需要给Pod加上注解
    • 备份时禁用快照,可指定参数--snapshot-volumes=false
    • 各云厂商Volumes快照插件: https://velero.io/plugins/
    • 使用 Velero 跨集群迁移资源,确保如下检查工作
      1. 确保镜像资源在迁移后可以正常拉取。
      2. 确保两个集群的 K8S 版本的 API 兼容,最好是相同版本
      3. 绑定集群外部资源的无法迁移, 例如 LoadBalancer 类型的service, 创建备份建议忽略

    参考

    相关文章

      网友评论

        本文标题:使用velero备份恢复k8s集群

        本文链接:https://www.haomeiwen.com/subject/kheviltx.html