前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Longhorn+K8S+KubeSphere云端数据管理,实战 Sentry PostgreSQL 数据卷增量快照/备份与还原

Longhorn+K8S+KubeSphere云端数据管理,实战 Sentry PostgreSQL 数据卷增量快照/备份与还原

作者头像
为少
发布2023-02-22 14:34:10
6250
发布2023-02-22 14:34:10
举报
文章被收录于专栏:黑客下午茶

云端实验环境配置

VKE K8S Cluster

  1. Vultr 托管集群
  • https://vultr.com/
  1. 3worker 节点,kubectl get nodes
代码语言:javascript
复制
k8s-paas-71a68ebbc45b   Ready    <none>   12d   v1.23.14
k8s-paas-dbbd42d034e6   Ready    <none>   12d   v1.23.14
k8s-paas-f7788d4f4a38   Ready    <none>   12d   v1.23.14

Kubesphere v3.3.1 集群可视化管理

全栈的 Kubernetes 容器云 PaaS 解决方案。

  • https://kubesphere.io/

Longhorn 1.14

Kubernetes 的云原生分布式块存储。

  • https://longhorn.io/

Sentry Helm Charts

非官方 k8s helm charts,大规模吞吐需建设微服务集群/中间件集群/边缘存储集群

  • https://github.com/sentry-kubernetes/charts
代码语言:javascript
复制
helm repo add sentry https://sentry-kubernetes.github.io/charts

kubectl create ns sentry
helm install sentry sentry/sentry -f values.yaml -n sentry
# helm install sentry sentry/sentry -n sentry

为 Sentry PostgreSQL 数据卷不同状态下创建快照

创建快照

这里我们创建 3 个 PostgreSQL 数据卷快照,分别对应 Sentry 后台面板的不同状态。

Sentry 后台面板状态-1

Sentry 后台面板状态-2

Sentry 后台面板状态-3

分别创建 3 个快照

创建备份

配置备份目标服务器

用于访问备份存储的端点。支持 NFS 和 S3 协议的服务器。

针对快照 2 创建备份

查看备份卷

备份卷创建时间取决于你的卷大小和网络带宽。

Longhorn 为 K8S StatefulSets 恢复卷的示例

官方文档:https://longhorn.io/docs/1.4.0/snapshots-and-backups/backup-and-restore/restore-statefulset/

Longhorn 支持恢复备份,此功能的一个用例是恢复用于 Kubernetes StatefulSet 的数据,这需要为备份的每个副本恢复一个卷。

要恢复,请按照以下说明进行操作。下面的示例使用了一个 StatefulSet,其中一个卷附加到每个 Pod 和两个副本。

  1. 在您的 Web 浏览器中连接到 Longhorn UI 页面。在 Backup 选项卡下,选择 StatefulSet 卷的名称。单击卷条目的下拉菜单并将其还原。将卷命名为稍后可以轻松引用的 Persistent Volumes
  • 对需要恢复的每个卷重复此步骤。
  • 例如,如果恢复一个有两个副本的 StatefulSet,这些副本的卷名为 pvc-01apvc-02b,则恢复可能如下所示:

Backup Name

Restored Volume

pvc-01a

statefulset-vol-0

pvc-02b

statefulset-vol-1

  1. 在 Kubernetes 中,为创建的每个 Longhorn 卷创建一个 Persistent Volume。将卷命名为以后可以轻松引用的 Persistent Volume Claims。下面必须替换 storage 容量、numberOfReplicasstorageClassNamevolumeHandle。在示例中,我们在 Longhorn 中引用 statefulset-vol-0statefulset-vol-1,并使用 longhorn 作为我们的 storageClassName
代码语言:javascript
复制
apiVersion: v1
kind: PersistentVolume
metadata:
  name: statefulset-vol-0
spec:
  capacity:
    storage: <size> # must match size of Longhorn volume
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  csi:
    driver: driver.longhorn.io # driver must match this
    fsType: ext4
    volumeAttributes:
      numberOfReplicas: <replicas> # must match Longhorn volume value
      staleReplicaTimeout: '30' # in minutes
    volumeHandle: statefulset-vol-0 # must match volume name from Longhorn
  storageClassName: longhorn # must be same name that we will use later
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: statefulset-vol-1
spec:
  capacity:
    storage: <size>  # must match size of Longhorn volume
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  csi:
    driver: driver.longhorn.io # driver must match this
    fsType: ext4
    volumeAttributes:
      numberOfReplicas: <replicas> # must match Longhorn volume value
      staleReplicaTimeout: '30'
    volumeHandle: statefulset-vol-1 # must match volume name from Longhorn
  storageClassName: longhorn # must be same name that we will use later
  1. 在将部署 StatefulSetnamespace 中,为每个 Persistent Volume 创建 PersistentVolume ClaimsPersistent Volume Claim 的名称必须遵循以下命名方案:
代码语言:javascript
复制
<name of Volume Claim Template>-<name of StatefulSet>-<index>

StatefulSet Pod 是零索引的。在这个例子中,Volume Claim Template 的名称是 dataStatefulSet 的名称是 webapp,并且有两个副本,分别是索引 01

代码语言:javascript
复制
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-webapp-0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi # must match size from earlier
storageClassName: longhorn # must match name from earlier
volumeName: statefulset-vol-0 # must reference Persistent Volume
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-webapp-1
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi # must match size from earlier
storageClassName: longhorn # must match name from earlier
volumeName: statefulset-vol-1 # must reference Persistent Volume
  1. 创建 StatefulSet:
代码语言:javascript
复制
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: webapp # match this with the PersistentVolumeClaim naming scheme
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 2 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: data
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: data # match this with the PersistentVolumeClaim naming scheme
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: longhorn # must match name from earlier
      resources:
        requests:
          storage: 2Gi # must match size from earlier

结果: 现在应该可以从 StatefulSet Pod 内部访问恢复的数据。

通过 Longhorn UI 恢复 Sentry PostgreSQL 数据卷

卸载 sentry 命名空间下一切资源并自删除 namespace

代码语言:javascript
复制
# 删除 release
helm uninstall sentry -n sentry
# 删除 namespace
kubectl delete ns sentry

查看当前 namespace

kubectl get ns,已无 sentry。

从备份服务器恢复 PostgreSQL 数据卷

还原最新的备份
设置不同机器间多个卷副本, 高可用
  1. 卷名设置为 statefulset-vol-sentry-postgresql-0
  2. 副本设置为至少 2,卷副本会被自动调度到不同节点,保证卷高可用。
为 Longhorn 备份卷创建 PV/PVC

注意:这里我们需要重新创建 namespace:sentry

代码语言:javascript
复制
kubectl create ns sentry
重新安装 sentry
代码语言:javascript
复制
helm install sentry sentry/sentry -f values.yaml -n sentry
查看 statefulset-vol-sentry-postgresql-0 副本
重新访问 Sentry

ok,成功恢复。

K8S-PaaS 云原生中间件 https://k8s-paas.hacker-linner.com/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 黑客下午茶 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 云端实验环境配置
    • VKE K8S Cluster
      • Kubesphere v3.3.1 集群可视化管理
        • Longhorn 1.14
          • Sentry Helm Charts
          • 为 Sentry PostgreSQL 数据卷不同状态下创建快照
            • 创建快照
              • Sentry 后台面板状态-1
                • Sentry 后台面板状态-2
                  • Sentry 后台面板状态-3
                    • 分别创建 3 个快照
                    • 创建备份
                      • 配置备份目标服务器
                        • 针对快照 2 创建备份
                          • 查看备份卷
                          • Longhorn 为 K8S StatefulSets 恢复卷的示例
                          • 通过 Longhorn UI 恢复 Sentry PostgreSQL 数据卷
                            • 卸载 sentry 命名空间下一切资源并自删除 namespace
                              • 查看当前 namespace
                                • 从备份服务器恢复 PostgreSQL 数据卷
                                  • 还原最新的备份
                                  • 设置不同机器间多个卷副本, 高可用
                                  • 为 Longhorn 备份卷创建 PV/PVC
                                  • 重新安装 sentry
                                  • 查看 statefulset-vol-sentry-postgresql-0 副本
                                  • 重新访问 Sentry
                              相关产品与服务
                              容器服务
                              腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档