Kubernetes StatefulSet-不会在Pod重新启动时恢复数据

时间:2019-04-17 12:27:47

标签: kubernetes

Kubernetes版本-1.8

  1. 使用pvc为Postgres数据库创建statefulset
  2. 向数据库添加了一些表
  3. 通过将statefulset缩放到0,然后再缩放1来重新启动pod
  4. 步骤2中创建的表不再可用

尝试了另一个针对docker-for-desktop集群k8s版本1.10的方案

  1. 使用pvc为Postgres数据库创建statefulset
  2. 向数据库添加了一些表
  3. 重新启动桌面版docker
  4. 步骤2中创建的表不再可用

k8清单

    apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: kong
  POSTGRES_USER: kong
  POSTGRES_PASSWORD: kong
  PGDATA: /var/lib/postgresql/data/pgdata
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/postgresql/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  app: postgres
spec:
  ports:
  - name: pgql
    port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: postgres
---
apiVersion: apps/v1beta2 #  for k8s versions before 1.9.0 use apps/v1beta2  and before 1.8.0 use extensions/v1beta1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: "postgres"
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:9.6
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pvc
---

1 个答案:

答案 0 :(得分:1)

如果您有多个节点-完全可以看到您看到的问题。因此,如果要将hostPath用作多节点群集中的持久卷,则必须使用某些共享文件系统(例如Glusterfs或Ceph),并将/ mnt / postgresql / data文件夹放置到该共享文件系统上。