如何使用NFS部署具有持久卷的单实例mongodb



我的笔记本电脑上有一个微服务。然而,我使用的是docker compose。我正在努力部署到我已经设置好的kubernetes集群。我一直致力于使数据持久化。E.g这是我在docker撰写的mongodb

systemdb:
container_name: system-db
image: mongo:4.4.1
restart: always
ports:
- '9000:27017'
volumes:
- ./system_db:/data/db
networks:
- backend

由于这是一个内部部署解决方案,所以我选择了NFS服务器。我创建了一个持久卷和持久卷声明(pvc-nfs-pv1(,在使用nginx进行测试时,它似乎工作得很好。然而,我不知道如何部署mongodb状态集来使用pvc。我没有实现复制集。

这是我的yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongod
spec:
serviceName: mongodb-service
replicas: 1
selector:
matchLabels:
role: mongo
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongod-container
image: mongo
resources:
requests:
cpu: "0.2"
memory: 200Mi
ports:
- containerPort: 27017
volumeMounts:
- name: pvc-nfs-pv1
mountPath: /data/db
volumeClaimTemplates:
- metadata:
name: pvc-nfs-pv1
annotations:
volume.beta.kubernetes.io/storage-class: "standard"
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 500Mi

我该怎么做?

volumeClaimTemplates用于动态卷配置。因此,您定义了一个卷声明模板,该模板将用于为每个pod创建一个PersistentVolumeClaim

volumeClaimTemplates将使用持久卷由PersistentVolume Provisioner 提供

因此,对于您的用例,您需要使用nfs provisioner创建storageclass。NFS Subdir外部预配置器是一个自动预配置器,它使用现有和已配置的NFS服务器,通过持久卷声明支持Kubernetes持久卷的动态预配置。永久卷被配置为${namespace}-${pvcName}-${pvName}

下面是一个如何定义存储类的示例:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
pathPattern: "${.PVC.namespace}/${.PVC.annotations.nfs.io/storage-path}" # waits for nfs.io/storage-path annotation, if not specified will accept as empty string.
onDelete: delete

好的,我有一个解决方案。它只需使用matchLabels选择器选择音量即可工作。

apiVersion: v1
kind: PersistentVolume
metadata:
name: mongodb-data-volume
labels:
app: moderetic
type: mongodb
role: data
spec:
storageClassName: hcloud-volumes
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
csi:
volumeHandle: "11099996"
driver: csi.hetzner.cloud
fsType: ext4
---
---
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
name: system-mongodb
labels:
app: moderetic
type: mongodb
spec:
members: 1
type: ReplicaSet
version: "4.2.6"
logLevel: INFO
security:
authentication:
modes: ["SCRAM"]
users:
- name: moderetic
db: moderetic
passwordSecretRef:
name: mongodb-secret
roles:
- name: clusterAdmin
db: moderetic
- name: userAdminAnyDatabase
db: moderetic
scramCredentialsSecretName: moderetic-scram-secret
additionalMongodConfig:
storage.wiredTiger.engineConfig.journalCompressor: zlib
persistent: true
statefulSet:
spec:
template:
spec:
containers:
- name: mongod
resources:
requests:
cpu: 1
memory: 1Gi
limits:
memory: 8Gi
- name: mongodb-agent
resources:
requests:
memory: 50Mi
limits:
cpu: 500m
memory: 256Mi
volumeClaimTemplates:
- metadata:
name: data-volume
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: hcloud-volumes
resources:
requests:
storage: 10Gi
selector:
matchLabels:
app: moderetic
type: mongodb
role: data
- metadata:
name: logs-volume
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: hcloud-volumes
resources:
requests:
storage: 10Gi
selector:
matchLabels:
app: moderetic
type: mongodb
role: logs

您的问题是mongo StatefulSet将如何使用您创建的pvc?默认情况下,它不会。它将通过volumeClaimTemplates自动创建新pvc的数量(取决于复制集的数量(,其名称如下:pvc-nfs-pv1-mongod-0、pvc-nfs-pv1-mongod-1等。。因此,如果您想使用您创建的pvc,请更改名称以匹配pvc-nfs-pv1-mongod-0像这个

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
role: mongo
name: pvc-nfs-pv1-mongod-0
namespace: default
spec:
...
volumeName: nfs-pv1
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
...

然而,我不建议使用这种方法(问题:当你有很多其他复制集时…你必须手动创建所有的pvc和对应的pv吗(。。这里有类似的问题,在这里,我建议使用动态NFS资源调配

希望我能帮助

我不使用NFS,而是在运行我的开发服务器的hetzner.com上使用卷。但我也有同样的问题:由于这是我的开发系统,我会定期销毁和重建它。通过这样做,我希望我的卷上的数据在整个集群删除后仍然有效。当我重建它时,所有的卷都将安装到正确的吊舱上。

对于我的后期工作来说,这很好。但是使用mongodb kubernetes运算符,我无法运行此操作。一个mongodb pod永远处于";挂起";因为我手动创建并绑定到卷的PVC已经绑定到卷。或者在我看来是这样。

我感谢你的帮助,Tobias

我能看到的确切信息是:

0/1 nodes are available: 1 pod has unbound immediate PersistentVolumeClaims

PVC和PV:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-volume-system-mongodb-0
labels:
app: moderetic
type: mongodb
spec:
storageClassName: hcloud-volumes
volumeName: mongodb-data-volume
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongodb-data-volume
labels:
app: moderetic
type: mongodb
spec:
storageClassName: hcloud-volumes
claimRef:
name: data-volume-system-mongodb-0
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
csi:
volumeHandle: "11099996"
driver: csi.hetzner.cloud
fsType: ext4

和mongodb StatefulSet:

apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
name: system-mongodb
labels:
app: moderetic
type: mongodb
spec:
members: 1
type: ReplicaSet
version: "4.2.6"
security:
authentication:
modes: ["SCRAM"]
users:
- name: moderetic
db: moderetic
passwordSecretRef:
name: mongodb-secret
roles:
- name: clusterAdmin
db: moderetic
- name: userAdminAnyDatabase
db: moderetic
scramCredentialsSecretName: moderetic-scram-secret
additionalMongodConfig:
storage.wiredTiger.engineConfig.journalCompressor: zlib
persistent: true
statefulSet:
spec:
template:
spec:
containers:
- name: mongod
resources:
requests:
cpu: 1
memory: 1Gi
limits:
memory: 8Gi
- name: mongodb-agent
resources:
requests:
memory: 50Mi
limits:
cpu: 500m
memory: 256Mi
volumeClaimTemplates:
- metadata:
name: data-volume
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: hcloud-volumes
resources:
requests:
storage: 10Gi
- metadata:
name: logs-volume
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: hcloud-volumes
resources:
requests:
storage: 10Gi

相关内容

  • 没有找到相关文章

最新更新