【问题标题】:How do I expose my service to the world?我如何向世界公开我的服务?
【发布时间】:2015-09-15 04:38:46
【问题描述】:

我决定在我自己的机器上设置 Kuberenetes,而不是使用谷歌云。我为我的 hello-world web 服务器制作了一个 docker 镜像。我设置了hello-controller.yaml:

apiversion: v1
kind: ReplicationController
metadata:
    name: hello
    labels:
        name: hello
spec:
    replicas: 1
    selector:
        name: hello
    template:
        metadata:
            labels:
                name: hello
        spec:
            containers:
            - name: hello
              image: flaggy/hello
              ports:
              - containerPort: 8888

现在我想向全世界公开服务。我认为 vagrant provider 没有负载均衡器(这似乎是最好的方法)。所以我尝试了 NodePort 服务类型。但是,新创建的 NodePort 似乎没有在我尝试的任何 IP 上被监听。这是 hello-service.yaml:

apiversion: v1
kind: Service
metadata:
    name: hello
    labels:
        name: hello
spec:
    type: NodePort
    selector:
        name: hello
    ports:
    - port: 8888

如果我登录我的 minion,我可以访问端口 8888

$ curl 10.246.1.3:8888
Hello!

当我描述我的服务时,这就是我得到的:

$ kubectl.sh describe service/hello
W0628 15:20:45.049822    1245 request.go:302] field selector: v1 - events - involvedObject.name - hello: need to check if this is versioned correctly.
W0628 15:20:45.049874    1245 request.go:302] field selector: v1 - events - involvedObject.namespace - default: need to check if this is versioned correctly.
W0628 15:20:45.049882    1245 request.go:302] field selector: v1 - events - involvedObject.kind - Service: need to check if this is versioned correctly.
W0628 15:20:45.049887    1245 request.go:302] field selector: v1 - events - involvedObject.uid - 2c0005e7-1dc2-11e5-8369-0800279dd272: need to check if this is versioned correctly.
Name:           hello
Labels:         name=hello
Selector:       name=hello
Type:           NodePort
IP:         10.247.5.87
Port:           <unnamed>   8888/TCP
NodePort:       <unnamed>   31423/TCP
Endpoints:      10.246.1.3:8888
Session Affinity:   None
No events.

我找不到任何人在侦听端口 31423,据我所知,该端口应该是我的服务的外部端口。我也对 IP 10.247.5.87 感到困惑。

我注意到了这一点

$ kubectl.sh get nodes
NAME         LABELS                              STATUS
10.245.1.3   kubernetes.io/hostname=10.245.1.3   Ready

为什么该 IP 与我在服务描述中看到的不同?我尝试访问主机上的两个 IP:

$ curl 10.245.1.3:31423
curl: (7) Failed to connect to 10.245.1.3 port 31423: Connection refused
$ curl 10.247.5.87:31423
curl: (7) Failed to connect to 10.247.5.87 port 31423: No route to host
$ 

所以 IP 10.245.1.3 是可以访问的,尽管端口 31423 没有绑定到它。我尝试将 10.247.5.87 路由到 vboxnet1,但它没有改变任何东西:

$ sudo route add -net 10.247.5.87 netmask 255.255.255.255 vboxnet1
$ curl 10.247.5.87:31423
curl: (7) Failed to connect to 10.247.5.87 port 31423: No route to host

如果我在 minion 上执行sudo netstat -anp | grep 31423,则什么也不会出现。奇怪的是,如果我这样做 sudo netstat -anp | grep 8888 也没有任何反应。 一定是某些 iptables 魔法或某些处于混杂模式的接口被滥用了。

在裸机上进行工作会这么难吗?我也没有尝试过 AWS 提供商,但我越来越担心。

【问题讨论】:

  • 10.247.5.87 是集群内部服务的 IP 地址。它不会在集群之外工作。你的$ curl 10.245.1.3:31423 真的应该有效。我已请其他团队成员发表评论。

标签: vagrant kubernetes


【解决方案1】:

一些事情。

您的单个​​ pod 是 10.246.1.3:8888 - 这似乎有效。

您的服务是 10.247.5.87:8888 - 只要您在集群中,它就应该可以工作(它是虚拟的 - 您不会在 netstat 中看到它)。这是首先要验证的。

您的节点是 10.245.1.3,您的服务也应该在 10.245.1.3:31423 上 - 这是似乎无法正常工作的部分。与服务 IP 一样,此绑定是虚拟的 - 它应该显示在 iptables-save 而不是 netstat。如果你登录到你的节点(minion),你能curl localhost:31423吗?

您可能会发现此文档很有用:https://github.com/thockin/kubernetes/blob/docs-debug-svcs/docs/debugging-services.md

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多