如何连接到 kubernetes 端点?

How to connect to kubernetes endpoint?

我创建了一个 kubernetes 服务:

[root@Infra-1 kubernetes]# kubectl describe service gitlab 
Name:           gitlab
Namespace:      default
Labels:         name=gitlab
Selector:       name=gitlab
Type:           NodePort
IP:         10.254.101.207
Port:           http    80/TCP
NodePort:       http    31982/TCP
Endpoints:      172.17.0.4:80
Port:           ssh 22/TCP
NodePort:       ssh 30394/TCP
Endpoints:      172.17.0.4:22
Session Affinity:   None
No events.

但是,我无法连接到端点,甚至无法从节点主机上的 shell 连接:

 [root@Infra-2 ~]# wget 172.17.0.4:80
 --2015-12-08 20:22:27--  http://172.17.0.4:80/
 Connecting to 172.17.0.4:80... failed: Connection refused.

在 NodePort 上调用 wget localhost:31982 也会给出 Recv failure: Connection reset by peer 并且 kube-proxy 会记录错误消息:

 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973209   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973294   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973376   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973482   26410 proxysocket.go:100] Dial failed: dial tcp 172.17.0.4:80: connection refused
 Dec 08 20:13:41 Infra-2 kube-proxy[26410]: E1208 20:13:41.973494   26410 proxysocket.go:134] Failed to connect to balancer: failed to connect to an endpoint.

这次失败的原因可能是什么?

这是我的服务配置文件http://pastebin.com/RriYPRg7, a slight modification of https://github.com/sameersbn/docker-gitlab/blob/master/kubernetes/gitlab-service.yml

实际上是 Pod 或 Replication Controller 有问题,因为它没有转发到服务。也许 post 配置或确保它指定了端口并且其容器进程正在侦听正确的端口

原创

真正暴露在pod外面的是NodePortPort 是节点内 NAT 网络上的端口,Port 是容器内的进程应该绑定到的端口,通常使用服务发现。其他 pods 将与 NodePort 上的那个 pod 交谈。如果您想为 Web 服务器显式设置 NodePort,那么在您的 Pod 定义或复制控制器或服务定义中,将 NodePort 显式设置为所需的端口。

Port: 80 会说容器内的 nginx 侦听端口 80,然后 NodePort: 4980 将是暴露的端口。所以你会 wget <Node IP>:4980.

就解决您的特定情况而言,我建议不要将其复杂化并明确设置 TargetPortNodePort

除了 "NodePort" 类型的服务之外,还有一些其他方法可以从集群外部与 kubernetes 服务进行交互。也许他们会更 "natural" 和容易:

  • 使用服务类型"LoadBalancer"。它仅适用于某些云提供商,不适用于 virtualbox,但我认为了解该功能会很好。在这种情况下,您不仅会获得服务的 "internal cluster-only" IP 地址,还会获得外部配置的负载均衡器来访问它(在 aws/gce 等中)Link to the documentation
  • 使用名为 "ingress" 的最新功能之一。这是手册 "An Ingress is a collection of rules that allow inbound connections to reach the cluster services. It can be configured to give services externally-reachable urls, load balance traffic, terminate SSL, offer name based virtual hosting etc." 中的描述。 Link to the documentation
  • 如果 kubernetes 不是严格要求并且您可以切换到最新的 openshift 源(即 "kubernetes on steroids"),您可以使用称为 "router" 的源功能。