docker - kubernetes pod 无法(通过服务)连接到自己,只能连接到其他 pod 容器

标签 docker kubernetes coreos

我有一个 Kubernetes 单节点设置(参见 https://coreos.com/kubernetes/docs/latest/kubernetes-on-vagrant-single.html)

我有一个服务和一个创建 pod 的复制 Controller 。这些 pod 需要连接到同一服务中的其他 pod(注意:这最终是为了让 mongo 运行带有副本集(非本地主机),但这个简单的示例演示了 mongo 的问题)。

当我从任何节点连接到服务时,它将(如预期)分发到其中一个 pod。这将一直有效,直到它对自身(我所在的容器)进行负载平衡。然后连接失败。

很抱歉,我很抱歉,但我将附上我的所有文件,以便您可以看到我在这个小示例中所做的事情。

Dockerfile:

FROM ubuntu
MAINTAINER Eric H
RUN apt-get update; apt-get install netcat
EXPOSE 8080
COPY ./entry.sh /
ENTRYPOINT ["/entry.sh"]

这里是入口点

#!/bin/bash
# wait for a connection, then tell them who we are 
while : ; do 
    echo "hello, the date=`date`; my host=`hostname`" | nc -l 8080 
    sleep .5
done

构建 dockerfile

docker build -t echoserver .

标记并上传到我的 k8s 集群的注册表

docker tag -f echoserver:latest 127.0.0.1:5000/echoserver:latest
docker push 127.0.0.1:5000/echoserver:latest

这是我的复制 Controller

apiVersion: v1
kind: ReplicationController
metadata:
  labels:
    role: echo-server
    app: echo
  name: echo-server-1
spec:
  replicas: 3
  template:
    metadata:
      labels:
        entity: echo-server-1
        role: echo-server
        app: echo
    spec:
      containers:
      - 
        image: 127.0.0.1:5000/echoserver:latest
        name: echo-server-1

        ports:
          - containerPort: 8080

最后,这是我的服务

kind: Service
metadata:
  labels:
    app: echo
    role: echo-server
    name: echo-server-1
  name: echo-server-1
spec:
  selector:
    entity: echo-server-1
    role: echo-server
  ports:
    - port: 8080
      targetPort: 8080

创建我的服务 kubectl create -f echo.service.yaml

创建我的 rc kubectl create -f echo.controller.yaml

获取我的 POD

kubectl get po
NAME                  READY     STATUS    RESTARTS   AGE
echo-server-1-jp0aj   1/1       Running   0          39m
echo-server-1-shoz0   1/1       Running   0          39m
echo-server-1-y9bv2   1/1       Running   0          39m

获取服务IP

kubectl get svc
NAME            CLUSTER_IP   EXTERNAL_IP   PORT(S)    SELECTOR                                AGE
echo-server-1   10.3.0.246   <none>        8080/TCP   entity=echo-server-1,role=echo-server   39m

执行到其中一个 pod kubectl exec -t -i echo-server-1-jp0aj/bin/bash

现在多次连接到该服务...它将为我提供所有 pod 的应用程序消息,但当它到达自身时,它会挂起。

root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
hello, the date=Mon Jan 11 22:02:38 UTC 2016; my host=echo-server-1-y9bv2
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
^C
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
hello, the date=Mon Jan 11 22:02:43 UTC 2016; my host=echo-server-1-shoz0
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
^C
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
hello, the date=Mon Jan 11 22:31:19 UTC 2016; my host=echo-server-1-y9bv2
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
hello, the date=Mon Jan 11 22:31:23 UTC 2016; my host=echo-server-1-shoz0
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
hello, the date=Mon Jan 11 22:31:26 UTC 2016; my host=echo-server-1-y9bv2
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080
hello, the date=Mon Jan 11 22:31:27 UTC 2016; my host=echo-server-1-shoz0
root@echo-server-1-jp0aj:/# nc 10.3.0.246 8080

我该如何配置,以便服务的所有成员都可以连接到所有其他成员,包括它自己?

最佳答案

感谢所有在 GitHub 上提供帮助的人。
解决方法结果如下:

tanen01 commented on Feb 4 Seeing the same problem here on k8s v1.1.7 stable

Issue occurs with:

kube-proxy --proxy-mode=iptables 

Once I changed it to:

           --proxy-mode=userspace 

(also the default), then it works again.

所以,如果您遇到这种情况,请尝试在启动 kube-proxy 时关闭 --proxy-mode

关于docker - kubernetes pod 无法(通过服务)连接到自己,只能连接到其他 pod 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34732597/

相关文章:

bash -/bin/sh : apt-get: not found

docker - 将 ARG 传递给 ENTRYPOINT

kubernetes - 使用 topologyKey : kubernetes. io/hostname 的反关联性规则部署失败 - 必需值:不能为空

kubernetes - k8s 入口 Controller 中的 Traefik 前向认证

kubernetes - 如何通过传递像kubectl这样的yaml文件来创建kubernetes对象

docker - 在docker中,如何将所有找到的nvidia设备传递到容器?

amazon-ec2 - Docker隐藏调用容器的IP

bash - Docker 中的 Logstash - 通过卷安装时找不到配置文件

vagrant - CoreOS:flecttl list-machines 显示错误

docker - 如何显示Nunit从Windows容器到TeamCity的结果?