Hướng dẫn quản lý dữ liệu PostgreSQL cho Sentry trên Kubernetes với Longhorn và KubeSphere

Môi trường thực nghiệm trên Cloud

VKE K8S Cluster

  1. Cluster được quản lý bởi Vultr
  2. Địa chỉ: https://vultr.com/
  3. 3 node worker, kiểm tra bằng lệnh 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 - Giao diện quản lý cluster

Giải pháp PaaS container cloud toàn diện cho Kubernetes.

  • https://kubesphere.io/

Longhorn 1.14

Hệ thống lưu trữ block phân tán cloud-native cho Kubernetes.

  • https://longhorn.io/

Sentry Helm Charts

Biểu đồ Helm không chính thức cho Sentry, phù hợp với kiến trúc microservices.

  • https://github.com/sentry-kubernetes/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
# Lưu ý: tùy chỉnh values.yaml nếu cần

Tạo snapshot cho volume PostgreSQL của Sentry

Quy trình tạo snapshot

Trong hướng dẫn này, chúng tôi tạo 3 snapshot cho volume PostgreSQL, mỗi snapshot tương ứng với trạng thái khác nhau của bảng điều khiển Sentry.

Trạng thái 1 của Sentry dashboard

[Hình ảnh minh họa trạng thái 1 - nếu cần]

Trạng thái 2 của Sentry dashboard

[Hình ảnh minh họa trạng thái 2 - nếu cần]

Trạng thái 3 của Sentry dashboard

[Hình ảnh minh họa trạng thái 3 - nếu cần]

Tạo các snapshot tương ứng

Thực hiện backup

Cấu hình máy chủ backup

Đặt endpoint cho các bản backup. Hỗ trợ máy chủ dùng giao thức NFS hoặc S3.

Tạo backup từ snapshot thứ 2

  1. Truy cập Longhorn UI
  2. Chọn snapshot cần backup
  3. Thực hiện thao tác backup

Kiểm tra volume backup

Thời gian tạo backup phụ thuộc vào kích thước volume và băng thông mạng.

Khôi phục volume cho StatefulSet trên Longhorn

Tài liệu tham khảo: https://longhorn.io/docs/1.4.0/snapshots-and-backups/backup-and-restore/restore-statefulset/

Longhorn hỗ trợ khôi phục backup, áp dụng cho các StatefulSet có nhiều replica. Dưới đây là ví dụ với StatefulSet có 2 replica.

Bước 1: Khôi phục volume từ Longhorn UI

  1. Mở giao diện Longhorn UI, chọn tab Backup.
  2. Tìm tên volume của StatefulSet cần khôi phục.
  3. Nhấp vào dropdown của volume và chọn Restore, đặt tên mới cho volume (ví dụ: statefulset-vol-0).
  4. Lặp lại cho các volume khác (ví dụ: statefulset-vol-1).
Tên backupVolume đã khôi phục
pvc-01astatefulset-vol-0
pvc-02bstatefulset-vol-1

Bước 2: Tạo PersistentVolume (PV)

Tạo PV cho mỗi volume Longhorn đã khôi phục. Cập nhật các giá trị phù hợp như storage, numberOfReplicas, storageClassName, và volumeHandle.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: statefulset-vol-0
spec:
  capacity:
    storage: 2Gi # khớp với kích thước volume Longhorn
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  csi:
    driver: driver.longhorn.io
    fsType: ext4
    volumeAttributes:
      numberOfReplicas: '2'
      staleReplicaTimeout: '30'
    volumeHandle: statefulset-vol-0 # tên volume trong Longhorn
  storageClassName: longhorn
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: statefulset-vol-1
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  csi:
    driver: driver.longhorn.io
    fsType: ext4
    volumeAttributes:
      numberOfReplicas: '2'
      staleReplicaTimeout: '30'
    volumeHandle: statefulset-vol-1
  storageClassName: longhorn

Bước 3: Tạo PersistentVolumeClaim (PVC)

Tạo PVC cho từng PV trong namespace của StatefulSet.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-webapp-0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: longhorn
  volumeName: statefulset-vol-0
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-webapp-1
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: longhorn
  volumeName: statefulset-vol-1

Bước 4: Tạo StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: webapp
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: "nginx"
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    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
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: longhorn
      resources:
        requests:
          storage: 2Gi

Kết quả: Dữ liệu từ backup sẽ có sẵn trong Pod của StatefulSet.

Khôi phục volume PostgreSQL cho Sentry qua Longhorn UI

Xóa namespace Sentry cũ

# Xóa release Helm
helm uninstall sentry -n sentry
# Xóa namespace
kubectl delete ns sentry

Kiểm tra namespace

Dùng lệnh kubectl get ns, đảm bảo không còn namespace sentry.

Khôi phục volume PostgreSQL từ backup

Bước 1: Khôi phục bản backup mới nhất

Tại Longhorn UI, chọn backup gần nhất và tiến hành Restore.

Bước 2: Cấu hình replica cho khả năng chịu lỗi

  1. Đặt tên volume là statefulset-vol-sentry-postgresql-0.
  2. Thiết lập số replica tối thiểu là 2, đảm bảo volume được phân tán trên nhiều node.

Tạo PV/PVC cho volume Longhorn

Trước hết, tạo lại namespace sentry:

kubectl create ns sentry

Tạo PV và PVC tương tự các bước trên, với volumeHandle là statefulset-vol-sentry-postgresql-0.

Cài đặt lại Sentry

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

Kiểm tra trạng thái volume

Dùng Longhorn UI để xác nhận volume statefulset-vol-sentry-postgresql-0 có 2 replica và hoạt động bình thường.

Truy cập lại Sentry

Sau khi hoàn tất, đăng nhập Sentry để xác nhận dữ liệu được khôi phục thành công.

Thẻ: Longhorn Kubernetes KubeSphere sentry PostgreSQL

Đăng vào ngày 14 tháng 6 lúc 02:59