在持久卷上运行mongo会引发错误-Kubernetes

时间:2019-08-14 14:48:50

标签: mongodb kubernetes

上下文

我想创建一个 mongodb 有状态部署,该部署与/mnt/nfs/data/myproject/production/permastore/mogno上的所有mongodb pod共享我主机的本地目录/data/db(网络文件系统目录)。我正在三个 VirtualMachines 上运行我的kubernetes集群。

问题

当我不使用持久卷声明时,我可以毫无问题地启动mongo!但是,当我使用持久的卷声明启动mongodb时,出现此错误。

Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

问题

有人/data/db使用持久卷进行挂载时,mongo为什么无法启动?如何解决?

代码

以下配置文件由于路径不同而在您的环境中不起作用。但是,您应该可以从我的设置中获得灵感。

持久卷 pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: phenex-mongo
  labels:
    type: local
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /mnt/nfs/data/phenex/production/permastore/mongo
  claimRef:
    name: phenex-mongo
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  volumeMode: Filesystem

永久数量声明 pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: phenex-mongo
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi

部署 deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo
  labels:
    run: mongo
spec:
  selector:
    matchLabels:
      run: mongo
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        run: mongo
    spec:
      containers:
      - image: mongo:4.2.0-bionic
        name: mongo
        ports:
        - containerPort: 27017
          name: mongo
         volumeMounts:
         - name: phenex-mongo
           mountPath: /data/db
       volumes:
       - name: phenex-mongo
         persistentVolumeClaim:
           claimName: phenex-mongo

应用配置

$ kubectl apply -f pv.yaml
$ kubectl apply -f pc.yaml
$ kubectl apply -f deployment.yaml

检查群集状态

$ kubectl get deploy,po,pv,pvc --output=wide
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES               SELECTOR
deployment.extensions/mongo   1/1     1            1           38m   mongo        mongo:4.2.0-bionic   run=mongo

NAME                         READY   STATUS    RESTARTS   AGE   IP          NODE    NOMINATED NODE   READINESS GATES
pod/mongo-59f669657d-fpkgv   1/1     Running   0          35m   10.44.0.2   web01   <none>           <none>

NAME                            CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS   REASON   AGE    VOLUMEMODE
persistentvolume/phenex-mongo   1Gi        RWO            Retain           Bound    phenex/phenex-mongo   manual                  124m   Filesystem

NAME                                 STATUS   VOLUME         CAPACITY   ACCESS MODES   STORAGECLASS   AGE    VOLUMEMODE
persistentvolumeclaim/phenex-mongo   Bound    phenex-mongo   1Gi        RWO            manual         122m   Filesystem

正在运行的mongo pod

$ kubectl exec -it mongo-59f669657d-fpkgv mongo
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-08-14T14:25:25.452+0000 E  QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2019-08-14T14:25:25.453+0000 F  -        [main] exception: connect failed
2019-08-14T14:25:25.453+0000 E  -        [main] exiting with code 1
command terminated with exit code 1

日志

$ kubectl logs mongo-59f669657d-fpkgv 
2019-08-14T14:00:32.287+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=mongo-59f669657d-fpkgv
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] db version v4.2.0
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] modules: none
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] build environment:
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten]     distarch: x86_64
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
2019-08-14T14:00:32.291+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: "*" } }
root@mongo-59f669657d-fpkgv:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
mongodb      1  0.0  2.7 208324 27920 ?        Dsl  14:00   0:00 mongod --bind_ip_all
root        67  0.0  0.2  18496  2060 pts/1    Ss   15:12   0:00 bash
root        81  0.0  0.1  34388  1536 pts/1    R+   15:13   0:00 ps aux

1 个答案:

答案 0 :(得分:2)

我找到了原因和解决方案!在我的设置中,我使用NFS通过网络共享目录。这样,我所有的集群节点(仆从)都可以访问位于/mnt/nfs/data/的公共目录。

原因

mongo无法启动的原因是由于无效的Persistent Volumes。即,我正在使用持久卷 HostPath 类型-这将适用于单节点测试,或者如果您在所有群集节点上手动创建目录结构(例如/tmp/your_pod_data_dir/。但是,如果您尝试将nfs目录挂载为hostPath,则会引起问题-如我所愿!

解决方案

对于通过网络文件系统共享的目录,请使用 NFS 持久卷类型(NFS Example)!在下面,您将找到我的设置和两种解决方案。

设置

/ etc / hosts -我的群集节点。

# Cluster nodes
192.168.123.130 master
192.168.123.131 web01
192.168.123.132 compute01
192.168.123.133 compute02

已导出的NFS目录列表

[vagrant@master]$ showmount -e
Export list for master:
/nfs/data compute*,web*
/nfs/www  compute*,web*

第一个解决方案

此解决方案显示通过装入 nfs目录部署-看一下volumes和{{1} }部分。

volumeMounts

第二个解决方案

此解决方案显示通过批量声明装入 nfs目录部署-看一下apiVersion: extensions/v1beta1 kind: Deployment metadata: name: mongo labels: run: mongo spec: selector: matchLabels: run: mongo strategy: type: Recreate template: metadata: labels: run: mongo spec: containers: - image: mongo:4.2.0-bionic name: mongo ports: - containerPort: 27017 name: mongo volumeMounts: - name: phenex-nfs mountPath: /data/db volumes: - name: phenex-nfs nfs: # IP of master node server: 192.168.123.130 path: /nfs/data/phenex/production/permastore/mongo 持久量持久量声明如下所示。

persistentVolumeClaim

持久卷-NFS

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo
  labels:
    run: mongo
spec:
  selector:
    matchLabels:
      run: mongo
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        run: mongo
    spec:
      containers:
      - image: mongo:4.2.0-bionic
        name: mongo
        ports:
        - containerPort: 27017
          name: mongo
        volumeMounts:
        - name: phenex-nfs
          mountPath: /data/db
      volumes:
      - name: phenex-nfs
        persistentVolumeClaim:
          claimName: phenex-nfs

永久数量声明

apiVersion: v1
kind: PersistentVolume
metadata:
  name: phenex-nfs
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  nfs:
    # IP of master node
    server: 192.168.123.130
    path: /nfs/data
  claimRef:
    name: phenex-nfs
  persistentVolumeReclaimPolicy: Retain

预期产量

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: phenex-nfs
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi