4

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

 1 year ago
source link: https://www.cnblogs.com/hacker-linner/p/17064398.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

image

云端实验环境配置

VKE K8S Cluster

  1. Vultr 托管集群

image
  1. 3worker 节点,kubectl get nodes
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 解决方案。

image

Longhorn 1.14

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

image

Sentry Helm Charts

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

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

image

Sentry 后台面板状态-2

image

Sentry 后台面板状态-3

image

分别创建 3 个快照

image

配置备份目标服务器

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

image

针对快照 2 创建备份

image

image

查看备份卷

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

image

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
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 的名称必须遵循以下命名方案:
<name of Volume Claim Template>-<name of StatefulSet>-<index>

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

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:
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

# 删除 release
helm uninstall sentry -n sentry
# 删除 namespace
kubectl delete ns sentry

查看当前 namespace

kubectl get ns,已无 sentry。

image

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

还原最新的备份

image

设置不同机器间多个卷副本, 高可用

  1. 卷名设置为 statefulset-vol-sentry-postgresql-0
  2. 副本设置为至少 2,卷副本会被自动调度到不同节点,保证卷高可用。

image

为 Longhorn 备份卷创建 PV/PVC

image

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

kubectl create ns sentry

image

image

重新安装 sentry

helm install sentry sentry/sentry -f values.yaml -n sentry

查看 statefulset-vol-sentry-postgresql-0 副本

image

重新访问 Sentry

image

ok,成功恢复。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK