无法连接到Kubernetes中的Mongodb Pod(拒绝连接) [英] Cannot connect to a Mongodb pod in Kubernetes (Connection refused)

查看:678
本文介绍了无法连接到Kubernetes中的Mongodb Pod(拒绝连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些远程虚拟机,我想在这些虚拟机上部署一些Mongodb实例,然后使其可以远程访问,但是由于某些原因,我似乎无法使它工作.

I have a few remote virtual machines, on which I want to deploy some Mongodb instances and then make them accessible remotely, but for some reason I can't seem to make this work.

这些是我采取的步骤:

  • 我启动了在远程虚拟机上运行Mongodb的Kubernetes pod.
  • 然后,我通过Kubernetes NodePort服务公开了它.
  • 然后,我尝试从笔记本电脑连接到Mongodb实例,但是 没用.
  • I started a Kubernetes pod running Mongodb on a remote virtual machine.
  • Then I exposed it through a Kubernetes NodePort service.
  • Then I tried to connect to the Mongodb instance from my laptop, but it didn't work.

这是我用来尝试连接的命令:

Here is the command I used to try to connect:

$ mongo host:NodePort   

(主机"是指Kubernetes主机).

(by "host" I mean the Kubernetes master).

这是它的输出:

MongoDB shell version v4.0.3
connecting to: mongodb://host:NodePort/test
2018-10-24T21:43:41.462+0200 E QUERY    [js] Error: couldn't connect to server host:NodePort, connection attempt failed: SocketException:
Error connecting to host:NodePort :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

从Kubernetes主机中,我确保Mongodb pod处于运行状态.然后,我在容器中运行了一个shell,并检查了Mongodb服务器是否正常运行.此外,我以前通过在Yaml描述中指定"--bind-ip = 0.0.0.0"选项来授予对Mongodb服务器的远程访问权限.为了确保已应用此选项,我在同一外壳的Mongodb实例中运行了此命令:

From the Kubernetes master, I made sure that the Mongodb pod was running. Then I ran a shell in the container and checked that the Mongodb server was working properly. Moreover, I had previously granted remote access to the Mongodb server, by specifying the "--bind-ip=0.0.0.0" option in its yaml description. To make sure that this option had been applied, I ran this command inside the Mongodb instance, from the same shell:

db._adminCommand( {getCmdLineOpts: 1}

这是输出:

{
"argv" : [
    "mongod",
    "--bind_ip",
    "0.0.0.0"
],
"parsed" : {
    "net" : {
        "bindIp" : "0.0.0.0"
    }
},
"ok" : 1
}

因此,Mongodb服务器实际上应该可以远程访问.

So the Mongodb server should actually be accessible remotely.

我不知道问题是由Kubernetes还是Mongodb引起的.

I can't figure out whether the problem is caused by Kubernetes or by Mongodb.

作为测试,我通过使用MySQL遵循了完全相同的步骤,并且可以正常工作(即,我运行一个MySQL pod并使用Kubernetes服务将其公开,使其可以远程访问,然后成功连接到从我的笔记本电脑上下载).这会让我认为罪魁祸首是这里的Mongodb,但我不确定.也许我只是在某个地方犯了一个愚蠢的错误.

As a test, I followed exactly the same steps by using MySQL instead, and that worked (that is, I ran a MySQL pod and exposed it with a Kubernetes service, to make it accessible remotely, and then I successfully connected to it from my laptop). This would lead me to think that the culprit is Mongodb here, but I'm not sure. Maybe I'm just making a silly mistake somewhere.

有人可以帮我澄清一下吗?还是告诉我如何调试此问题?

Could someone help me shed some light on this? Or tell me how to debug this problem?

根据您的请求,这是kubectl describe deployment <mongo-deployment>命令的输出:

Here is the output of the kubectl describe deployment <mongo-deployment> command, as per your request:

Name:                   mongo-remote
Namespace:              default
CreationTimestamp:      Thu, 25 Oct 2018 06:31:24 +0000
Labels:                 name=mongo-remote
Annotations:            deployment.kubernetes.io/revision=1
Selector:               name=mongo-remote
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  name=mongo-remote
  Containers:
   mongocontainer:
    Image:      mongo:4.0.3
    Port:       5000/TCP
    Host Port:  0/TCP
    Command:
      mongod
      --bind_ip
      0.0.0.0
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   mongo-remote-655478448b (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  15m   deployment-controller  Scaled up replica set mongo-remote-655478448b to 1

为了完整起见,这是部署的yaml描述:

For the sake of completeness, here is the yaml description of the deployment:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo-remote
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: mongo-remote
    spec:
      containers:
        - name: mongocontainer
          image: mongo:4.0.3
          imagePullPolicy: Always
          command:
          - "mongod"
          - "--bind_ip"
          - "0.0.0.0"
          ports:
          - containerPort: 5000
            name: mongocontainer
      nodeSelector:
        kubernetes.io/hostname: xxx

推荐答案

我发现了错误(而且我怀疑这是一个愚蠢的错误).
问题出在部署的yaml描述中.由于在mongod命令中未指定任何端口,因此mongodb在侦听默认端口(27017),但是容器在侦听另一个指定的端口(5000).

I found the mistake (and as I suspected, it was a silly one).
The problem was in the yaml description of the deployment. As no port was specified in the mongod command, mongodb was listening on the default port (27017), but the container was listening on another specified port (5000).

因此解决方案是将containerPort设置为mongodb的默认端口,如下所示:

So the solution is to either set the containerPort as the default port of mongodb, like so:

       command:
      - "mongod"
      - "--bind_ip"
      - "0.0.0.0"
      ports:
      - containerPort: 27017
        name: mongocontainer

或者将mongodb的端口设置为containerPort之一,如下所示:

Or to set the port of mongodb as the one of the containerPort, like so:

      command:
      - "mongod"
      - "--bind_ip"
      - "0.0.0.0"
      - "--port"
      - "5000"
      ports:
      - containerPort: 5000
        name: mongocontainer

这篇关于无法连接到Kubernetes中的Mongodb Pod(拒绝连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆