kubernetes StorageClass不保留现有数据

时间:2019-04-10 19:57:15

标签: kubernetes

当吊舱被删除并与我的postgresql数据库一起部署后,我的Kubernetes StorageClass卷不保留现有数据。删除吊舱时,将创建新的吊舱,但数据库为空。

我遵循了教程的不同版本(https://kubernetes.io/docs/concepts/storage/persistent-volumes/)的变化,但是似乎没有任何效果。

我粘贴了所有YAML文件,因为问题可能出在组合中。

storage-google.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: spingular-pvc
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 7Gi
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: standard
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard
  zone: us-east4-a

jhipsterpress-postgresql.yml

apiVersion: v1
kind: Secret
metadata:
  name: jhipsterpress-postgresql
  namespace: default
  labels:
    app: jhipsterpress-postgresql
type: Opaque
data:
  postgres-password: NjY0NXJxd24=
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jhipsterpress-postgresql
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: jhipsterpress-postgresql
    spec:
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: spingular-pvc
      containers:
      - name: postgres
        image: postgres:10.4
        env:
        - name: POSTGRES_USER
          value: jhipsterpress
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: jhipsterpress-postgresql
              key: postgres-password
        ports:
        - containerPort: 5432
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/
---
apiVersion: v1
kind: Service
metadata:
  name: jhipsterpress-postgresql
  namespace: default
spec:
  selector:
    app: jhipsterpress-postgresql
  ports:
  - name: postgresqlport
    port: 5432
  type: LoadBalancer

jhipsterpress-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jhipsterpress
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jhipsterpress
      version: "v1"
  template:
    metadata:
      labels:
        app: jhipsterpress
        version: "v1"
    spec:
      initContainers:
        - name: init-ds
          image: busybox:latest
          command:
            - '/bin/sh'
            - '-c'
            - |
                while true
                do
                  rt=$(nc -z -w 1 jhipsterpress-postgresql 5432)
                  if [ $? -eq 0 ]; then
                    echo "DB is UP"
                    break
                  fi
                  echo "DB is not yet reachable;sleep for 10s before retry"
                  sleep 10
                done
      containers:
      - name: jhipsterpress-app
        image: galore/jhipsterpress
        env:
        - name: SPRING_PROFILES_ACTIVE
          value: prod
        - name: SPRING_DATASOURCE_URL
          value: jdbc:postgresql://jhipsterpress-postgresql.default.svc.cluster.local:5432/jhipsterpress
        - name: SPRING_DATASOURCE_USERNAME
          value: jhipsterpress
        - name: SPRING_DATASOURCE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: jhipsterpress-postgresql
              key: postgres-password
        - name: JAVA_OPTS
          value: " -Xmx256m -Xms256m"
        resources:
          requests:
            memory: "256Mi"
            cpu: "500m"
          limits:
            memory: "512Mi"
            cpu: "1"
        ports:
        - name: http
          containerPort: 8080
        readinessProbe:
          httpGet:
            path: /management/health
            port: http
          initialDelaySeconds: 20
          periodSeconds: 15
          failureThreshold: 6
        livenessProbe:
          httpGet:
            path: /management/health
            port: http
          initialDelaySeconds: 120

jhipsterpress-service.yml

apiVersion: v1
kind: Service
metadata:
  name: jhipsterpress
  namespace: default
  labels:
    app: jhipsterpress
spec:
  selector:
    app: jhipsterpress
  type: LoadBalancer
  ports:
  - name: http
    port: 8080

当我加入保留政策时,出现此错误:

@cloudshell:~ (academic-veld-230622)$ kubectl apply -f storage-google.yaml
error: error validating "storage-google.yaml": error validating data: 
ValidationError(PersistentVolumeClaim.spec): unknown field "persistentVolumeReclaimPolicy" in io.k8s.api.core.v1.PersistentVolumeClaimSpec; if you choose to ignore these errors, turn validation off with --validate=false

请,如果您知道一个有效的公共图像的完整示例(在PostgreSQL中,我可以使其与Mongo一起使用),我将非常感谢。

感谢所有人。

1 个答案:

答案 0 :(得分:2)

请注意,要实现此目的,您需要让PVC动态地设置PV以满足其要求,然后PVC和PV之间将存在永久的binding,并且每次工作负载使用PVC时,将使用相同的PV。此摘录特别指出:

  

如果为新的PVC动态设置了PV,则循环将始终将该PV绑定到PVC

如果在您的情况下,PVC设置了Google永久磁盘,并且您可以验证在GCP上每次使用的PV是否相同,则pod启动过程可能会出现问题,因为该过程会删除所有数据。 (您使用/var/lib/postgresql//var/lib/postgresql还是有什么原因?)

此外,persistentVolumeReclaimPolicy: Retain适用于PV,而不适用于PVC。对于动态预配置的PV,该值为Delete。在您的情况下,它将不适用,因为您应将动态预配置的卷绑定到PVC。换句话说,您不是在回收该卷。

说了一切,推荐的部署数据库的方法就是使用StatefulSets,类似于使用volumeClaimTemplate的此mysql示例。