简体   繁体   中英

Service host/port undefined, Kubernetes/Google Container Engine

I have a service with the name mongodb . According to the documentation, the service host and port should be available to other pods in the same cluster through $MONGODB_SERVICE_HOST and $MONGODB_SERVICE_PORT.

However, neither of these are set in my frontend pods. What are the requirements for this to work?

frontend-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" }
}

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" }
}

The service:

$ gcloud preview container services list

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

The 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

Because environment variables for pods are only created when the pod is started, the service has to exist before a given pod in order for that pod to see the service's environment variables. You should be able to see them from all new pods you create.

If you'd like to learn more, additional explanation of how services work can be found in the documentation .

Alternatively, all newly created clusters in Container Engine (version 0.9.2 and above) have a SkyDNS service running in the cluster that you can use to access services from pods, even those without the environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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