kubernetes 无法从其他pod访问mongodb Replicaset pod|连接EHOSTUNREACH 10.1.231.87:27017

svmlkihl  于 2022-12-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(93)

我正在microk8s kubernetes集群中运行mongodb statefulset,我可以使用pod的ip地址通过主机终端连接到主副本,如下所示:
mongosh "mongodb://10.1.231.87:27017/test"这里10.1.231.87是mongo副本pod的ip地址。我有另一个Nodejs pod(在相同的默认名称空间和集群中),它使用URI字符串连接到mongo副本,但我得到以下错误:
MongoServerSelectionError: connect EHOSTUNREACH 10.1.231.87:27017我的statefulset配置文件是:

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
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
        replicaset: MainRepSet
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongod-container
          image: mongo
          command:
            - "mongod"
            - "--bind_ip"
            - "0.0.0.0"
            - "--replSet"
            - "MainRepSet"
          resources:
            requests:
              cpu: 0.2
              memory: 200Mi
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-volume
              mountPath: /data/db
  volumeClaimTemplates:
   - metadata:
       name: mongo-volume
     spec:
       accessModes: ["ReadWriteOnce"]
       storageClassName: microk8s-hostpath
       resources:
         requests:
           storage: 1Gi

NodeJS pod进行以下调用:

mongoose.connect('mongodb://mongod-0.mongodb-service:27017/authDB?replicaSet=MainRepSet&readPreference=primaryPreferred', { useNewUrlParser: true })

节点窗格的日志:

error in connecting to database :  MongooseServerSelectionError: connect EHOSTUNREACH 10.1.231.87:27017
    at NativeConnection.Connection.openUri (/auth_service/node_modules/mongoose/lib/connection.js:824:32)
    at /auth_service/node_modules/mongoose/lib/index.js:412:10
    at /auth_service/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/auth_service/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (/auth_service/node_modules/mongoose/lib/index.js:1265:10)
    at Mongoose.connect (/auth_service/node_modules/mongoose/lib/index.js:411:20)
    at connectAuthDB (/auth_service/dbconnect.js:6:6)
    at Server.<anonymous> (/auth_service/App.js:43:7)
    at Object.onceWrapper (events.js:519:28) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'mongod-0.mongodb-service:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

描述这个魔神复制品说明:

Name:             mongod-0
Namespace:        default
Priority:         0
Service Account:  default
Node:             mynodename/10.0.208.58
Start Time:       Mon, 28 Nov 2022 13:54:55 +0000
Labels:           controller-revision-hash=mongod-5b69c4bf54
                  environment=test
                  replicaset=MainRepSet
                  role=mongo
                  statefulset.kubernetes.io/pod-name=mongod-0
Annotations:      cni.projectcalico.org/containerID: e1e55e7b6468b60076ae2c76b344732142c8852838b5b354cd6bb06b09f21387
                  cni.projectcalico.org/podIP: 10.1.231.87/32
                  cni.projectcalico.org/podIPs: 10.1.231.87/32
Status:           Running
IP:               10.1.231.87
IPs:
  IP:           10.1.231.87
Controlled By:  StatefulSet/mongod
Containers:
  mongod-container:
    Container ID:  containerd://9c8594df7491036157b7958fdc77ca4e152dbfcf6f12a2ae5c6954bb8c63f637
    Image:         mongo
    Image ID:      docker.io/library/mongo@sha256:8bed0be3e86595283d67836e8d4f3f08916184ea6f2aac7440bda496083ab0c8
    Port:          27017/TCP
    Host Port:     0/TCP
    Command:
      mongod
      --bind_ip
      0.0.0.0
      --replSet
      MainRepSet
    State:          Running
      Started:      Mon, 28 Nov 2022 13:54:56 +0000
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        200m
      memory:     200Mi
    Environment:  <none>
    Mounts:
      /data/db from mongo-volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-75tsr (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  mongo-volume:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mongo-volume-mongod-0
    ReadOnly:   false
  kube-api-access-75tsr:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

我在集群中只有一个节点,我所做的一切都在“default”命名空间中。令人惊讶的是,我能够从我的主机终端连接到mongodb statefulset。

xt0899hw

xt0899hw1#

您是否尝试过疑难解答页中的提示?
https://microk8s.io/docs/troubleshooting
确保通过iptables工具可以将pod网络接口的数据包转发到主机上的默认接口,也可以从主机上的默认接口转发数据包。通过安装iptables-persistent软件包可以使这些更改永久生效:

sudo iptables -P FORWARD ACCEPT
   sudo apt-get install iptables-persistent

或者,如果使用UFW:

sudo ufw default allow routed

MicroK 8 s inspect命令可用于检查防火墙配置:

microk8s inspect

如果防火墙未转发流量,则会显示警告。

相关问题