54

Docker群与自定义网络

 5 years ago
source link: https://codeday.me/bug/20190112/503433.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

所以我尝试像这样配置自定义网络:

docker network create -d overlay elasticnet

所以现在,当我打入其中一个容器时,我可以成功解析tasks.myservice但是我无法再访问我在服务创建中定义的端口 – 在外部–publish(当我使用入口网络时我可以).

有没有办法:

>使用入口网络并能够解析将指向我所有服务容器的tasks.myservice或任何其他DNS记录?

>或者,使用自定义网络,但是 – 正确发布端口以便我可以在外部访问它们?

编辑

这就是我创建服务的方式,

没有自定义网络:

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

看下面的例子:

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

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

2.运行具有暴露端口和3个副本的服务:

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

如果你要使用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:

从其中一个节点测试Exposed端口:

administrator@dg1:~$telnet localhost 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.

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

user@externalhost /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
root@05d05f934c68:/# 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

root@05d05f934c68:/# 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有自己的集群,可提供故障转移和负载平衡功能.

您可以在属于elasticsearch集群的elasticsearch主机中为每个索引使用分片和副本.

话虽这么说,我建议你创建3个服务,每个服务有1个副本,然后在弹性搜索集群中加入,然后创建包含3个分片和3个副本的索引.您将在elasticsearch集群中进行负载平衡和故障转移.

要阅读有关分片的更多信息,请访问 Use this .

翻译自:https://stackoverflow.com/questions/40911124/docker-swarm-with-a-custom-network


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK