k8s 上的 postgres 以 glusterfs 作为存储

postgres on k8s with glusterfs as storage

我在 k8sglusterfs 上部署了一个 postgres 数据库作为 volume.But 每次我重新启动我的 pod 时所有数据 losses.Why 是吗?

apiVersion: apps/v1       
kind: Deployment               
metadata:     
  name: postgres-deployment
  namespace: gitlab
  labels:                                                                  
    app: postgres 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:13.1
        ports:
        - containerPort: 5432
        volumeMounts:
        - mountPath: /var/lib/postgresql
          name: postgres
        env:
        - name: POSTGRES_USERNAME
          valueFrom:
            secretKeyRef:
              name: gitlab
              key: postgres_username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: gitlab
              key: postgres_password
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: gitlab
              key: postgres_db 
      volumes:
      - name: postgres
        glusterfs:
          endpoints: glusterfs-cluster
          path: gv


你可以这样做:

1.For数据库等有状态集合服务,应该使用StatefulSet控制器来部署;

2.The存储数据资源应该是共享类型的,而不是使用本地卷作为存储,创建POD对象时可能会调度到其他节点;

https://docs.gitlab.com/charts/

https://artifacthub.io/packages/helm/bitnami/postgresql

https://artifacthub.io/packages/helm/bitnami/postgresql-ha

定义 PVC 和 PV 对象。见下文以供参考。

---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10GB
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app: postgres
  name: postgres-pv-claim
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10GB

然后如下所示将PVC绑定到pod

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        ...
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-pv-claim
      volumes:
      - name: postgres-pv-claim
        persistentVolumeClaim:
          claimName: postgres-pv-claim