简体   繁体   English

Google Cloud Container:无法连接到mongodb服务

[英]Google Cloud Container: Can not connect to mongodb service

I created a mongodb replication controller and a mongo service. 我创建了一个mongodb复制控制器和一个mongo服务。 I tried to connect to it from a different mongo pod just to test the connection. 我试图从另一个mongo pod连接到它,只是为了测试连接。 But that does not work 但这不起作用

root@mongo-test:/# mongo mongo-service/mydb
MongoDB shell version: 3.2.0
connecting to: mongo-service/mydb
2015-12-09T11:05:55.256+0000 E QUERY    [thread1] Error: network error while attempting to run command 'isMaster' on host 'mongo-service:27017'  :
connect@src/mongo/shell/mongo.js:226:14
@(connect):1:6

exception: connect failed

I am not sure what I have done wrong in the configuration. 我不确定我在配置中做错了什么。 I may miss something here 我可能在这里想念什么

kubectl get rc                                                                                                                        
CONTROLLER          CONTAINER(S)     IMAGE(S)                                 SELECTOR              REPLICAS   AGE
mongo               mongo            mongo:latest                             name=mongo            1          9s

kubectl get pods                                                                                                                      
NAME                      READY     STATUS             RESTARTS   AGE
mongo-6bnak               1/1       Running            0          1m
mongo-test                1/1       Running            0          21m

kubectl get services                                                                                                                  
NAME                     CLUSTER_IP       EXTERNAL_IP     PORT(S)     SELECTOR                AGE
kubernetes               10.119.240.1     <none>          443/TCP     <none>                  23h
mongo-service            10.119.254.202   <none>          27017/TCP   name=mongo,role=mongo   1m

I configured the RC and Service with the following configs 我使用以下配置配置了RC和服务

mongo-rc mongo-rc

{
  "metadata": {
    "name": "mongo",
    "labels": { "name": "mongo" }
  },
  "kind": "ReplicationController",
  "apiVersion": "v1",
  "spec": {
    "replicas": 1,
    "template": {
      "metadata": {
        "labels": { "name": "mongo" }
      },
      "spec": {
        "volumes": [
          {
            "name": "mongo-disk",
            "gcePersistentDisk": {
             "pdName": "mongo-disk",
            "fsType": "ext4"
            }
           }
        ],
        "containers": [
          {
            "name": "mongo",
            "image": "mongo:latest",
            "ports": [{
                "name":"mongo",
              "containerPort": 27017
            }],
            "volumeMounts": [
              {
                "name": "mongo-disk",
                "mountPath": "/data/db"
                }
            ]
          }
        ]
      }
    }
  }
}

mongo-service: mongo服务:

{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
    "name": "mongo-service"
  },
  "spec": {
    "ports": [
      {
        "port": 27017,
        "targetPort": "mongo"
      }
    ],
    "selector": {
      "name": "mongo",
      "role": "mongo"
    }
  }
}

Almost a bit embarrassing. 有点尴尬。

The issue was that I used the selector "role" in the service but did not define it on the RC. 问题是我在服务中使用了选择器“角色”,但未在RC上定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM