我有一个kubernetes单节点设置(参见https://coreos.com/kubernetes/docs/latest/kubernetes-on-vagrant-single.html)
我有一个服务和一个复制控制器创建pod.这些pod需要连接到同一服务中的其他pod(注意:这最终是因为我可以让mongo运行w /副本集(非localhost),但这个简单的例子演示了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
这是我的复制控制器
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.2468080/TCP entity=echo-server-1,role=echo-server 39m
执行其中一个pod
kubectl exec -t -i echo-server-1-jp0aj /bin/bash
现在多次连接到服务...它将为我提供所有pod的app-message,除了它自己何时到达,然后它挂起.
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
如何配置事物以便服务的所有成员可以连接到所有其他成员,包括其自身?