服务主机/端口undefined,Kubernetes / Google Container Engine

时间:2015-02-03 02:58:32

标签: docker kubernetes google-kubernetes-engine

我有一个名为 mongodb 的服务。根据文档,服务主机和端口应该通过$ MONGODB_SERVICE_HOST和$ MONGODB_SERVICE_PORT可用于同一群集中的其他pod。

但是,这些都没有设置在我的前端窗格中。这有什么要求?

前端-controller.json

{
  "id": "frontend",
  "kind": "ReplicationController",
  "apiVersion": "v1beta1",
  "desiredState": {
    "replicas": 1,
    "replicaSelector": {"name": "spatula", "role": "frontend"},
    "podTemplate": {
      "desiredState": {
        "manifest": {
          "version": "v1beta1",
          "id": "frontend",
          "containers": [{
            "name": "frontend",
            "image": "gcr.io/crafty_apex_841/spatula_frontend",
            "cpu": 100,
            "ports": [{"name": "spatula-server", "containerPort": 80}]
          }]
        }
      },
      "labels": { "name": "spatula", "role": "frontend" }
    }
  },
  "labels": { "name": "spatula", "role": "frontend" }
}

前端-service.json

{
  "apiVersion": "v1beta1",
  "kind": "Service",
  "id": "frontend",
  "port": 80,
  "containerPort": "spatula-server",
  "labels": { "name": "spatula", "role": "frontend" },
  "selector": { "name": "spatula", "role": "frontend" },
  "createExternalLoadBalancer": true
}

的mongodb-service.json

{
  "apiVersion": "v1beta1",
  "kind": "Service",
  "id": "mongodb",
  "port": 27017,
  "containerPort": "mongodb-server",
  "labels": { "name": "spatula", "role": "mongodb" },
  "selector": { "name": "spatula", "role": "mongodb" }
}

的mongodb-controller.json

{
  "id": "mongodb",
  "kind": "ReplicationController",
  "apiVersion": "v1beta1",
  "desiredState": {
    "replicas": 1,
    "replicaSelector": {"name": "spatula", "role": "mongodb"},
    "podTemplate": {
      "desiredState": {
        "manifest": {
          "version": "v1beta1",
          "id": "mongodb",
          "containers": [{
            "name": "mongodb",
            "image": "dockerfile/mongodb",
            "cpu": 100,
            "ports": [{"name": "mongodb-server", "containerPort": 27017}]
          }]
        }
      },
      "labels": { "name": "spatula", "role": "mongodb" }
    }
  },
  "labels": { "name": "spatula", "role": "mongodb" }
}

服务:

$ gcloud preview container services list

NAME                LABELS                                    SELECTOR                    IP                  PORT
mongodb             name=spatula,role=mongodb                 name=spatula,role=mongodb   10.111.240.154      27017

pod:

$ gcloud preview container pods list

POD                                    IP                  CONTAINER(S)        IMAGE(S)                           HOST                                                           LABELS                      STATUS
9ffd980f-ab56-11e4-ad76-42010af069b6   10.108.0.11         mongodb             dockerfile/mongodb                 k8s-spatula-node-1.c.crafty-apex-841.internal/104.154.44.77    name=spatula,role=mongodb   Running

1 个答案:

答案 0 :(得分:1)

由于pod的环境变量仅在启动pod时创建,因此该服务必须存在于给定的pod之前,以便该pod可以查看服务的环境变量。您应该能够从您创建的所有新pod中看到它们。

如果您想了解更多信息,可以在the documentation找到有关服务如何运作的其他说明。

或者,Container Engine中的所有新创建的集群(版本0.9.2及更高版本)都在集群中运行a SkyDNS service,您可以使用它来访问来自pod的服务,甚至是那些没有环境变量的服务。