Mongo 持久性在重启后不会在 K8S 集群上保留数据
我使用 Kubeadm 和两个 Ubuntu 虚拟服务器创建了自己的 k8s 集群 - 一个主节点和一个工作节点。我还部署了一个带有 MongoDB 持久性的 Springboot 应用程序,它工作得非常好。以下是我的 k8s 集群中的节点
下面是我的 yaml 文件的内容,该文件创建存储类、持久卷、持久卷声明、mongo 部署和 mongo 服务
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
reclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongo-pv
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
hostPath:
path: "/home/moviepopcorn/mongodb/data"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongo-pvc
spec:
volumeName: mongo-pv
volumeMode: Filesystem
storageClassName: local-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo
labels:
app: mongo
spec:
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongo
image: mongo:4.4.11
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /home/moviepopcorn/mongodb/data
volumes:
- name: mongo-persistent-storage
persistentVolumeClaim:
claimName: mongo-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mongo-service
spec:
selector:
app: mongo
ports:
- protocol: TCP
port: 27017
targetPort: 27017
一旦我重新启动 k8s 集群中的节点,从 Springboot 存储到 mongodb 的数据就不会保留。你能帮我理解我犯了什么错误吗?
I have created my own k8s cluster using Kubeadm with two Ubuntu virtual servers - one master and one worker node. I also deployed a Springboot app with MongoDB persistence and it works absolutely fine. Below are the nodes in my k8s cluster
Below are the contents of my yaml file which creates storage class, persistence volume, persistence volume claim, mongo deployment and mongo service
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
reclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongo-pv
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
hostPath:
path: "/home/moviepopcorn/mongodb/data"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongo-pvc
spec:
volumeName: mongo-pv
volumeMode: Filesystem
storageClassName: local-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo
labels:
app: mongo
spec:
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongo
image: mongo:4.4.11
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /home/moviepopcorn/mongodb/data
volumes:
- name: mongo-persistent-storage
persistentVolumeClaim:
claimName: mongo-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mongo-service
spec:
selector:
app: mongo
ports:
- protocol: TCP
port: 27017
targetPort: 27017
Data stored from my Springboot into mongodb is not retained once I reboot the nodes in my k8s cluster. Could you please help me understand on what mistake I am doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能您已经更新了 PVC 挂载路径,并且由于 mongo 不知道在哪里存储或保存数据,
请尝试上面的示例或检查 arg 和 挂载路径供参考。
May you have updated the PVC mount path and due to that mongo not aware of where to store or save the data
Try the above example or check the arg and mount path for reference.