服务 host/port 未定义,Kubernetes/Google 容器引擎

Service host/port undefined, Kubernetes/Google Container Engine

我有一个名为 mongodb 的服务。根据文档,服务主机和端口应该通过 $MONGODB_SERVICE_HOST 和 $MONGODB_SERVICE_PORT.

对同一集群中的其他 pods 可用

然而,这些都没有在我的前端设置 pods。这个工作的要求是什么?

前端-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

豆荚:

$ 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

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

如果您想了解更多信息,可以在 the documentation.

中找到有关服务工作原理的更多说明

或者,容器引擎(版本 0.9.2 及更高版本)中所有新创建的集群在集群中都有 a SkyDNS service 运行,您可以使用它们从 pods 访问服务,即使是那些没有环境变量的。