如何从外部访问pod,即将localhost:8888绑定到群集IP

时间:2016-09-14 20:54:38

标签: kubernetes

我有一个在localhost:8888上运行的服务,并尝试将其绑定到群集的公共IP,以便我可以从我的Web浏览器打开它。我使用以下yaml文件创建了另一个服务:

{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
    "name": "example-service"
  },
  "spec": {
    "ports": [{
      "port": 8888,
      "targetPort": 8888
    }],
    "selector": {
      "app": "example"
    },
    "type": "LoadBalancer"
  }
}

然后我做kubectl describe services example-service

Name:           example-service
Namespace:      spark-cluster
Labels:         <none>
Selector:       app=example
Type:           LoadBalancer
IP:         10.3.0.66
LoadBalancer Ingress:   a123b456c789.us-west-1.elb.amazonaws.com
Port:           <unset> 8888/TCP
NodePort:       <unset> 32767/TCP
Endpoints:      <none>
Session Affinity:   None
Events:
  FirstSeen LastSeen    Count   From            SubobjectPath   Type        Reason          Message
  --------- --------    -----   ----            -------------   --------    ------          -------
  14s       14s     1   {service-controller }           Normal      CreatingLoadBalancer    Creating load balancer
  11s       11s     1   {service-controller }           Normal      CreatedLoadBalancer Created load balancer

当我在网络浏览器中打开a123b456c789.us-west-1.elb.amazonaws.com:8888时,它无法加载。从外部访问我的pod的正确步骤是什么?

1 个答案:

答案 0 :(得分:0)

通过您的设置,如果您想自己提供NodePort,则可以在端口32767(NodePort参数)上的某个节点的ip地址上使用该应用程序,您需要更改以下代码:

{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
    "name": "example-service"
  },
  "spec": {
    "ports": [{
      "port": 8888,
      "targetPort": 8888
      "nodePort": 8888

    }],
    "selector": {
      "app": "example"
    },
    "type": "LoadBalancer"
  }
}

http://kubernetes.io/docs/user-guide/services/#type-nodeport