
云端实验环境配置
VKE K8S Cluster
Vultr托管集群

3个worker节点,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 解决方案。

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

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

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 和两个副本。
- 在您的 Web 浏览器中连接到
Longhorn UI页面。在Backup选项卡下,选择 StatefulSet 卷的名称。 单击卷条目的下拉菜单并将其还原。将卷命名为稍后可以轻松引用的Persistent Volumes。
- 对需要恢复的每个卷重复此步骤。
- 例如,如果恢复一个有两个副本的
StatefulSet,这些副本的卷名为pvc-01a和pvc-02b,则恢复可能如下所示:
| Backup Name | Restored Volume |
|---|---|
| pvc-01a | statefulset-vol-0 |
| pvc-02b | statefulset-vol-1 |
- 在 Kubernetes 中,为创建的每个 Longhorn 卷创建一个
Persistent Volume。将卷命名为以后可以轻松引用的Persistent Volume Claims。下面必须替换storage容量、numberOfReplicas、storageClassName和volumeHandle。在示例中,我们在Longhorn中引用statefulset-vol-0和statefulset-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
- 在将部署
StatefulSet的namespace中,为每个Persistent Volume创建PersistentVolume Claims。Persistent Volume Claim的名称必须遵循以下命名方案:
<name of Volume Claim Template>-<name of StatefulSet>-<index>
StatefulSet Pod 是零索引的。在这个例子中,Volume Claim Template 的名称是 data,StatefulSet 的名称是 webapp,并且有两个副本,分别是索引 0 和 1。
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
- 创建 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。

从备份服务器恢复 PostgreSQL 数据卷
还原最新的备份

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

为 Longhorn 备份卷创建 PV/PVC

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


重新安装 sentry
helm install sentry sentry/sentry -f values.yaml -n sentry
查看 statefulset-vol-sentry-postgresql-0 副本

重新访问 Sentry

ok,成功恢复。
- 更多,K8S PaaS 云原生中间件实战教程
- 关注公众号:黑客下午茶