如何在部署在 kubernetes 上的 mongodb 中保存数据
How to persist data in mongodb deployed on kubernetes
我已经在 kubernetes 上部署了 mongodb pod,并且能够连接并将数据插入数据库。但是当 pod 重新启动时,我所有的数据都丢失了。我也为 mongodb 创建了 persistentvolume,但数据仍然丢失。以下是我的 yaml。有人可以解释一下我在这里做错了什么吗?
Statefulset.yaml
---
apiVersion: "apps/v1"
kind: "StatefulSet"
metadata:
name: "mongo-development"
namespace: "development"
spec:
serviceName: "mongo-development"
replicas: 1
selector:
matchLabels:
app: "mongo-development"
template:
metadata:
labels:
app: "mongo-development"
spec:
containers:
-
name: "mongo-development"
image: "mongo"
imagePullPolicy: "Always"
env:
-
name: "MONGO_INITDB_ROOT_USERNAME"
value: "xxxx"
-
name: "MONGO_INITDB_ROOT_PASSWORD"
value: "xxxx"
ports:
-
containerPort: 27017
name: "mongodb"
volumeMounts:
-
name: "mongodb-persistent-storage"
mountPath: "/var/lib/mongodb"
volumes:
-
name: "mongodb-persistent-storage"
persistentVolumeClaim:
claimName: "mongodb-pvc-development"
pvc.yaml
---
apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
name: "mongodb-pvc-development"
namespace: "development"
labels:
app: "mongo-development"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: gp2
pv.yaml
---
apiVersion: "v1"
kind: "PersistentVolume"
metadata:
name: "mongo-pv-development"
namespace: "development"
labels:
type: local
app: "mongo-development"
spec:
storageClassName: "local-storage"
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
claimRef:
namespace: "development"
name: "mongodb-pvc-development"
nfs:
path: /mnt
server: xxxx
service.yaml
---
apiVersion: "v1"
kind: "Service"
metadata:
name: "mongo-development"
namespace: "development"
labels:
app: "mongo-development"
spec:
ports:
-
name: "mongodb"
port: 27017
targetPort: 27017
clusterIP: "None"
selector:
app: "mongo-development"
问题已解决我定义的挂载路径不正确应该是 statefulset.yaml 文件中的 /data/db
我已经在 kubernetes 上部署了 mongodb pod,并且能够连接并将数据插入数据库。但是当 pod 重新启动时,我所有的数据都丢失了。我也为 mongodb 创建了 persistentvolume,但数据仍然丢失。以下是我的 yaml。有人可以解释一下我在这里做错了什么吗?
Statefulset.yaml
---
apiVersion: "apps/v1"
kind: "StatefulSet"
metadata:
name: "mongo-development"
namespace: "development"
spec:
serviceName: "mongo-development"
replicas: 1
selector:
matchLabels:
app: "mongo-development"
template:
metadata:
labels:
app: "mongo-development"
spec:
containers:
-
name: "mongo-development"
image: "mongo"
imagePullPolicy: "Always"
env:
-
name: "MONGO_INITDB_ROOT_USERNAME"
value: "xxxx"
-
name: "MONGO_INITDB_ROOT_PASSWORD"
value: "xxxx"
ports:
-
containerPort: 27017
name: "mongodb"
volumeMounts:
-
name: "mongodb-persistent-storage"
mountPath: "/var/lib/mongodb"
volumes:
-
name: "mongodb-persistent-storage"
persistentVolumeClaim:
claimName: "mongodb-pvc-development"
pvc.yaml
---
apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
name: "mongodb-pvc-development"
namespace: "development"
labels:
app: "mongo-development"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: gp2
pv.yaml
---
apiVersion: "v1"
kind: "PersistentVolume"
metadata:
name: "mongo-pv-development"
namespace: "development"
labels:
type: local
app: "mongo-development"
spec:
storageClassName: "local-storage"
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
claimRef:
namespace: "development"
name: "mongodb-pvc-development"
nfs:
path: /mnt
server: xxxx
service.yaml
---
apiVersion: "v1"
kind: "Service"
metadata:
name: "mongo-development"
namespace: "development"
labels:
app: "mongo-development"
spec:
ports:
-
name: "mongodb"
port: 27017
targetPort: 27017
clusterIP: "None"
selector:
app: "mongo-development"
问题已解决我定义的挂载路径不正确应该是 statefulset.yaml 文件中的 /data/db