2016-12-01 34 views
8

我正试图解决如何在Docker中正确使用群集模式。首先,我尝试在我的2个工作人员和经理机器上运行容器,而没有指定自定义网络(所以我使用默认的入口覆盖网络)。但是,如果我使用入口网络,出于某种原因,我无法解析tasks.myservice具有自定义网络的Docker群体

所以,我想配置自定义网络是这样的:

docker network create -d overlay elasticnet 

所以现在,当我bash到容器之一,我能够成功地解决tasks.myservice但我不能再访问我定义的端口在我的服务创建--publish外部(我可以当我使用入口网络)。

有没有什么办法之一:

  1. 使用入口网络和能够解决tasks.myservice或将直接向我的所有服务容器的任何其他DNS记录?

  2. 或者,使用自定义网络,但--publish端口正确,所以我可以从外部访问它们?

编辑

这是我创造我的服务,

没有自定义网络:

docker service create --replicas 3 --label elasticsearch --endpoint-mode vip --name elastic -e ES_HOSTS="tasks.elastic" --publish 9200:9200 --mount type=bind,source=/tmp/es,destination=/usr/share/elasticsearch/config --update-delay 10s es:latest 

通过自定义网络:

docker service create --replicas 3 --network elasticnet --label elasticsearch --endpoint-mode vip --name elastic -e ES_HOSTS="tasks.elastic" --publish 9200:9200 --mount type=bind,source=/tmp/es,destination=/usr/share/elasticsearch/config --update-delay 10s es:latest 
+0

你可以发布你正在运行的所有命令的列表吗?而用户定义的覆盖网络就是要走的路。 – johnharris85

+0

@ johnharris85请参阅我的编辑。 –

回答

0

我们在这里缺少的是您的服务定义,或者您在定义容器时只使用优质的旧时装码头运行程序。

如果你使用的泊坞窗服务功能(在搬运工1.12可用),你仍然可以被服务暴露你的端口

docker create --name nodejs1 --network anti-spam -p 1230:123 --replicas 1 image:version 

您的服务会得到反垃圾邮件网络上创建,如果你创建另一个服务nodejs2,他们将能够使用服务名称(如主机名)到达对方。

发布仍然可以在群集上运行,但每个主机都会侦听端口1230,然后将其路由到其中一个容器。

+1

请参阅我的编辑。问题是,例如,当我使用'-p 1230:123'时,是不是意味着端口1230应该在外部可用?如果是,我不能让这部分工作与自定义网络。 –

1

请看下面的例子:

1,创建用户定义的覆盖网络:

sudo docker network create overlay1 --driver overlay 
9g4ipjn513iy  overlay1   overlay    swarm 

2.Run与暴露的端口服务和3个副本:

sudo docker service create --name nginx --replicas 3 --publish 80:80 --network overlay1 nginx 

你不必须指定endpoint-mode,如果你要使用VIP,它是默认值。

sudo docker service ps nginx 
ID       NAME  IMAGE NODE DESIRED STATE CURRENT STATE   ERROR 
dbz8b4jjfp6xg3vqunt1x8shx nginx.1 nginx dg1 Running  Running 13 minutes ago 
9d8zr6zka0sp99vadr8eqq2t2 nginx.2 nginx dg3 Running  Running 13 minutes ago 
cwbcegunuxz5ye9a8ghdrc4fg nginx.3 nginx dg3 Running  Running 12 minutes ago 

3.Verification: 测试暴露于来自节点的一个端口:

[email protected]:~$ telnet localhost 80 
Trying ::1... 
Connected to localhost. 
Escape character is '^]'. 

测试从外部主机暴露端口:

[email protected] /home/balrog% telnet dg1 80 
Trying 172.30.135.101... 
Connected to 172.30.135.101. 
Escape character is '^]'. 

从容器的内部测试DNS查找:

sudo docker exec -it 05d05f934c68 /bin/bash 
[email protected]:/# ping nginx                                   
PING nginx (10.0.0.3): 56 data bytes 
64 bytes from 10.0.0.3: icmp_seq=0 ttl=64 time=0.050 ms 
64 bytes from 10.0.0.3: icmp_seq=1 ttl=64 time=0.121 ms 

[email protected]:/# ping tasks.nginx 
PING tasks.nginx (10.0.0.5): 56 data bytes 
64 bytes from 10.0.0.5: icmp_seq=0 ttl=64 time=0.037 ms 
64 bytes from 10.0.0.5: icmp_seq=1 ttl=64 time=0.149 ms 

ElasticSearch具体建议

Elasticseach有自己的集群,提供FailoverLoadbalancing功能。

在elasticsearch集群的elasticsearch主机中,您可以使用shardsreplicas/index

话虽如此,我建议你创建3 各,然后再加入然后在elasticsearch集群,然后用3 shards和3 replicas创建indexes。在elasticsearch集群中您将有loadbalancingfailover

要了解更多关于shardsUse this