首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kubernetes部署在重新启动后不会持久保存数据,即使在部署yaml附加到云文件存储nfs服务器之后也是如此。

Kubernetes部署在重新启动后不会持久保存数据,即使在部署yaml附加到云文件存储nfs服务器之后也是如此。
EN

Stack Overflow用户
提问于 2021-03-20 08:25:51
回答 1查看 123关注 0票数 0

我在GCP上创建了一个Cloud Filestore实例,标准实例,并将其放入运行Kubernetes的集群所在的同一个VPC中。使用本指南https://cloud.google.com/filestore/docs/accessing-fileshares,我尝试访问fileshare实例作为我的部署的持久存储。我的部署是一个名为Apache OFBiz的set应用程序,这是一组主要用于记账的业务工具。它的演示和文档可以在线获得,因为它是开源的。因此,为了测试当我删除pod时数据是否仍然存在,我在将部署公开到公网IP后在应用程序上创建了一个用户,并将我拥有的域名附加到该公网IP。用户被创建,然后当我使用云shell删除集群上的用户时,当pod再次被创建时,我访问了webapp,它不再有用户,它回到了它的基本形式。我不确定出了什么问题,是访问filestore实例,还是从实例中存储和提取数据。webapp有一个嵌入的Apache Derby数据库,只是作为说明。我想我的问题也是,指南是否足够,或者我是否必须做其他事情才能使这项工作生效,以及是否有其他我需要查看的东西。

下面是我的部署yaml

代码语言:javascript
复制
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "2"
  creationTimestamp: "2021-03-19T21:08:27Z"
  generation: 2
  labels:
    app: ofbizvpn
  managedFields:
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:app: {}
      f:spec:
        f:progressDeadlineSeconds: {}
        f:replicas: {}
        f:revisionHistoryLimit: {}
        f:selector:
          f:matchLabels:
            .: {}
            f:app: {}
        f:strategy:
          f:rollingUpdate:
            .: {}
            f:maxSurge: {}
            f:maxUnavailable: {}
          f:type: {}
        f:template:
          f:metadata:
            f:labels:
              .: {}
              f:app: {}
          f:spec:
            f:containers:
              k:{"name":"ofbizvpn"}:
                .: {}
                f:image: {}
                f:imagePullPolicy: {}
                f:name: {}
                f:resources: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
            f:terminationGracePeriodSeconds: {}
    manager: kubectl-create
    operation: Update
    time: "2021-03-19T21:08:27Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:spec:
        f:template:
          f:spec:
            f:containers:
              k:{"name":"ofbizvpn"}:
                f:volumeMounts:
                  .: {}
                  k:{"mountPath":"ofbiz/data"}:
                    .: {}
                    f:mountPath: {}
                    f:name: {}
            f:volumes:
              .: {}
              k:{"name":"mypvc"}:
                .: {}
                f:name: {}
                f:persistentVolumeClaim:
                  .: {}
                  f:claimName: {}
    manager: GoogleCloudConsole
    operation: Update
    time: "2021-03-19T22:11:44Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:deployment.kubernetes.io/revision: {}
      f:status:
        f:availableReplicas: {}
        f:conditions:
          .: {}
          k:{"type":"Available"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
          k:{"type":"Progressing"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
        f:observedGeneration: {}
        f:readyReplicas: {}
        f:replicas: {}
        f:updatedReplicas: {}
    manager: kube-controller-manager
    operation: Update
    time: "2021-03-19T23:19:35Z"
  name: ofbizvpn
  namespace: default
  resourceVersion: "3004167"
  selfLink: /apis/apps/v1/namespaces/default/deployments/ofbizvpn
  uid: b2e10550-eabe-47fb-8f51-4e9e89f7e8ea
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: ofbizvpn
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: ofbizvpn
    spec:
      containers:
      - image: gcr.io/lithe-joy-306319/ofbizvpn
        imagePullPolicy: Always
        name: ofbizvpn
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: ofbiz/data
          name: mypvc
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: mypvc
        persistentVolumeClaim:
          claimName: fileserver-claim
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2021-03-19T21:08:28Z"
    lastUpdateTime: "2021-03-19T22:11:53Z"
    message: ReplicaSet "ofbizvpn-6d458f54cf" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  - lastTransitionTime: "2021-03-19T23:19:35Z"
    lastUpdateTime: "2021-03-19T23:19:35Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  observedGeneration: 2
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

这是我的持久卷yaml

代码语言:javascript
复制
apiVersion: v1
kind: PersistentVolume
metadata:
 name: fileserver
spec:
 capacity:
  storage: 10Gi
 accessModes:
 - ReadWriteMany
 nfs:
  path: /fileshare1
  server: 10.249.37.194

这是我的永久卷申请yaml

代码语言:javascript
复制
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: fileserver-claim
spec:
# Specify "" as the storageClassName so it matches the PersistentVolume's StorageClass.
# A nil storageClassName value uses the default StorageClass. For details, see
# https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1
 accessModes:
 - ReadWriteMany
 storageClassName: ""
 volumeName: fileserver
 resources:
  requests:
   storage: 10Gi
EN

回答 1

Stack Overflow用户

发布于 2021-03-20 17:19:42

如果你想要数据持久化,为什么不用Deployment代替StatefulSet呢?你最好使用StatefulSet

Deployment主要用于无状态应用程序,StatefulSet用于有状态应用程序。在Deployment中,pod的唯一性不被维护,因此当pod重新创建时,它基本上不会获得以前pod的标识,它会获得新的名称和身份。

StatefulSet是用于管理有状态应用程序的工作负载应用程序接口对象。管理一组Pod的部署和伸缩,并为这些Pod的顺序和唯一性提供保证。请参阅k8s doc

来自k8s文档的StatefulSet yaml示例:

代码语言:javascript
复制
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # 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: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi

在上面的示例中:

  • 名为nginx的无头服务用于控制网络域。

  • StatefulSet名为web,其Spec表示将在唯一的Pods.

中启动nginx容器的3个副本

  • volumeClaimTemplates将使用由PersistentVolume Provisioner.

调配的PersistentVolumes提供稳定的存储

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66717144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档